In this Linux tutorial, you will learn the VI editor shortcuts, some tips, and a few productivity hacks that can be used by DevOps engineers & Developers.
If you are a DevOps engineer or someone who works on Linux systems, vi editor is often used for editing files. However, unlike GUI editors, first-time VI editor users get intimidated due to various keystrokes for controlling the editing.
However, you can save time and increase productivity with a little bit of practice and the vi editor shortcuts in your day-to-day activities. Once you get the hang of it, you will start loving the VI editor. Moreover, it is a very powerful text editor in the Linux ecosystem.
VI Editor Shortcuts For Beginners
In this article, I will cover the necessary VI editor shortcuts that you could use in day-to-day Linux activities.
I have also written an article on learning shell scripting for DevOps. VI editor hacks come in handy when you work with shell scripts as well.
VI editor Modes
Before getting into shortcuts, you should understand the five modes of Vim editor.
There are five modes in the VI editor.
- Normal Mode
- Insert Mode
- Visual Mode
- Command Mode
- Replace Mode
Normal Mode
When you open any file in the VI editor, the default mode is Normal Mode
. In this mode, you can cursor to browse through the file using different shortcuts.
Following are the shortcuts to move the cursor in different directions. If you add a number to the shortcut, the movement gets multiplied.
VI Shortcut | Action |
h | Move one character left (4h will move four characters left) |
j | Move one line down ( 4j -> 4 lines down) |
k | Move one line up (5k -> 5 lines up) |
l | Move one character right (8l -> 8 characters right) |
Next, we will learn the shortcuts to browse through words. Here the lower and uppercase characters have different functions.
VI Shortcut | Action |
w | Goes to the beginning of the word. W goes to the beginning of the next word after the whitespace. |
b | Goes to the beginning of the previous word. B goes to the beginning of the previous word before the whitespace. |
e | Goes to the end of the word. E goes to the end of the word before the whitespace. |
The next shortcut is required most of the time when you work with files.
VI Shortcut | Action |
0 | Goes to the beginning of the line |
$ | Goes to the end of the line |
Insert Mode
In insert mode, you can edit the file. Following are the shortcuts for editing. Again, the lower and uppercase characters have different functions here.
VI Shortcut | Action |
i | VI editor goes in to insert more. ( I –> Goes to the beginning for the line in insert mode) |
a | The cursor moves to the next character in insert mode ( A – moves to the end of the line in Insert mode) |
o | Inserts a new from the cursor’s current position in insert mode. (O – inserts a new line above the cursors current position in insert mode) |
Visual Mode
The visual mode makes it easy to select and modify text in the VI editor. To enter the visual mode just enter “v
“.
You will be able to select multiple lines.
Command Mode
In command mode, you can execute commands and manipulate text. For example, find and replace a text in the whole file.
You need to know the following to work with the command mode.
VI Shortcut | Action |
s | To start the command mode |
% | Considers all the lines in the file. |
s | for substitution. |
/g | global change |
We will look at some of the examples for command mode later in the article.
Replace Mode
Replace mode is to replace existing texts in a document.
place the cursor at the first character of a work and enter “R
” in normal mode. Once you start typing, the word will get replaced.
Setting up VI Editor Environment
Before diving into shortcuts and commands, you must understand the vi editor settings. You can set all the necessary vi editor parameters in the ~/.vimrc
file that will be loaded by default.
If you don’t have the vimrc
file, create one using touch ~/.vimrc
Following are the common parameters that you might need in the vimrc
file.
set number set autoindent set shiftwidth=4 set softtabstop=4 set expandtab
The set number
parameter will set the line number for your vi editor.
set number
You can set and unset this temporarily by running :set nonumber
from the editor
To jump to a specific line number, type :<line_number>
and press Enter.
For example, :10
will take you to line 10.
set relativenumber
To make navigation easier, you can set relativenumber option. It will show the line numbers relative to your cursor position.
You can set relativenumber temporarily by running :set relativenumber
from the editor.
To unset, use :set norelativenumber
Syntax Highlighting
If you are bored with the Vim editor unicolor, you can enable syntax highlighting.
Open vimrc
vi ~/.vimrc
Add :syntax on
to the file.
Cut Copy Paste in Vi Editor
Follow the steps given below to copy and paste using the visual mode.
- Enter the visual mode using
ESC + v
and move the cursor to select the string you want to copy. - Use the capital
V
to select the whole line. - Use
d
to cut ory
to copy. - Place the cursor in the desired location and press
p
to paste.
The following Gif shows the demo.
Deleting Lines and Words
Here is how you can delete lines and words in the VI editor.
- Place the cursor on the line you want to delete and press
dd
to delete that line - To delete a specific number of lines, you can use
d10d
. This will delete 10 lines starting from the cursor. You can give any number in place of 10 based on your needs. - To select and delete specific lines, use
Shift + V
(Visual Mode) and then use up and down arrows for the selection. Once you selected the lines, pressd
for deleting all the selected lines.
To delete words, enable the insert mode (ESC + I) and you can do the following.
Ctrl + w
will delete the last word where the cursor is in.Ctrl + u
will delete all the words which are immediately to the left of the cursor.
Undo and Redo
You need to undo and redo quite often when you work on the vi editor. Here is how you can do that.
- Undo: Press u to undo the last change. If you made a mistake, simply press
u
to revert it - Redo: Press Ctrl + r to redo the change that you just undid.
Searching and Replacing Texts
You can search through your files by pressing /
in normal mode.
For example, if you want to search for a keyword data
, you should do the following.
/data
To find the next occurrence, just press n
Note: Searches are case-sensitive. If you want a case-insensitive search, you should set
:set ignorecase
in the editor.
Enter the command mode (ESC + :
) and use the following syntax for replacing a pattern.
:%s/pattern/replace/g
Use the following syntax for replacing every occurrence with a prompt. It will highlight all the occurrences. You can choose which occurrence to replace and which one to leave as it is.
:%s/pattern/replace/gc
Convert to Upper and Lowercase
For any case text conversions, keep the cursor on the line and use the following shortcuts in normal mode (ESC
).
- To convert a line to uppercase use
gUU
- To convert a line to Lowercase, use
guu
Copying Contents From Another File in Vi Editor
This is not something that you do very often. However, if you need to add the contents of another file to the current file you are editing, place the cursor on the desired line and you can use the following syntax in the command mode. The contents of the specified file will be copied from the next line of the cursor.
:r /path/to/file
For example, if you copy CPU info to an existing file, you would use the following command.
:r /proc/cpuinfo
Executing/Copying Contents From Command
You can execute other Linux commands inside the editor in the command mode (ESC + :
)
To execute commands from the editor, you can use :! <command>
. For example,
:! pwd
If you want the output of a command to be copied to the editing file, you can use :r! <command>.
For example,
to get and copy the eth0
IP address, you could do the following.
:r! ip addr | grep eth0 | grep inet | awk '{print $2}' | cut -d / -f1
Split Windows
There are situations you might want a split window to access other files or create new files.
- Horizontal Split: Type
:sp <filename>
to split the window horizontally and open a specific file. If you just use:sp
, it will split the window with the current file. - Vertical Split: Type
:vs <filename>
to split the window vertically and open a specific file. If you just use:vs
, it will split the window with the current file.
When you have different split windows, you might want to move between different windows.
Use Ctrl + w followed by w
to move the cursor to the next window.
Vi Editor Help
There are so many things you can do with VI editor.
If you want to become a VI editor power user, you should make use of the VIM tutor functionality.
vimtutor
It opens up a command line tutorial in your terminal. And you can use all the normal vi commands to browse through the help document.
Wrapping Up
VI is a very powerful editor.
Understanding the full functionality and all the vi editor shortcuts takes time and constant practice.
I have explained some commands, tips, and vi editor shortcuts that will save some time while working with the vi editor.
If you think you have some tips, please share them with us in the comment section. It could help others.
If you are getting started as a DevOps Engineer, check out the important Linux Troubleshooting commands as well.
2 comments
There is a correction
VI Shortcut Action
0 Goes to start of the line
$ Goes to the end of the line
Thanks, Hitesh. Corrected 🙂