vim is a ubiquitous Command Line Interface (CLI) text editor on GNU/Linux systems. No matter what distribution you are using, vim is likely to be either installed on your system, or easy to obtain. It is probably in your best interest to become familiar with vim.
If you do not have experience with line editors, vim can be confusing to get started with. However, once you get past its significant learning curve and attain a higher level of adeptness, it will be a powerful tool in your systems administration arsenal.
A large part of getting better with vim involves two key aspects:
- Understanding vim's Modes
- Memorizing vim's myriad keyboard shortcuts
Keep in mind, most Linux distributions come with a version of vim that has a minimal feature set and is located at
/usr/bin/vi. To get the most from vim and this reference, install the full-featured vim from your distribution's repository. After installation, you will find this version of vim at
/usr/bin/vim. This is the vim version used in these examples.
Note: Throughout this post, code items that contain the word
example are placeholders for command arguments. You will need to replace them with the actual values you would like to use with the commands.
vim has three basic modes:
Command Mode is the default mode for vim, i.e., when you first start vim, you will be placed in this mode. In Command Mode, key presses are interpreted as commands.
You can always get to Command Mode by pressing
Insert Mode is used to enter text into a file. It is the mode that is most like how you would use a normal word processing program.
You can enter Insert Mode by pressing either
i or the
INS key on a keyboard (if applicable).
Line Mode is used to enter line editing commands that are inherited from older line editors, like ed. If you do not have experience with line editors, this mode will probably take the most getting used to.
Here, each key press is an external command. These commands can include operations like writing a file's contents to disk or exiting vim.
Line Mode commands start with a
The best way to learn vim is to start using it. This section has keyboard shortcuts for accomplishing useful tasks in vim.
If an entry starts with a
$, that means that it is a command that you enter at the command line. The
$ symbol is the representation of the command line prompt for non-root users.
If an entry does not start with a
$, it is a command that is entered inside the vim program.
Some entries have actual examples to help clarify what a real command looks like.
Starting/Stopping vim, Working With Files, Setting Options
$ vim example_file
- Start vim and edit a file
$ vim +/example_string example_file
- Open a file with a search string and place the cursor on the first line with an instance of that string (e.g.,
$ vim +/mangoes fruit.txt)
$ vim +example_line_number example_file
- Open a file and place the cursor on a specific line (e.g.,
$ vim +5 fruit.txt)
- Open a file in vim
This command opens
example_file in vim without altering the previous file you were working on.
- Re-open the previously opened file
You can use this command to go back to the previous file you were working on in vim after you opened a new file with
- Save file as
- Save file
- Overwrite a file
- Save a file and exit vim
- Quit vim
- Quit vim without saving modifications
- View currently set vim options
- View all vim options
- Set a vim option
$ vim +example_option example_file
- Set a vim option from the command line
- Move cursor one character left
- Use a force multiplier to move cursor three characters left
- Move cursor down one line
- Move cursor up one line
- Move cursor one character right
- Move forward one word
- Move backward one word
- Move to the beginning of the line
- Move to the end of the line
- Move to the beginning of the file
- Move to the beginning of the last line of the file
- Move to a specific line (e.g.,
- Move forward a page
- Move backward a page
Searching For Text
- Search forward for a pattern (e.g.,
- Search backward for a pattern:
- Move to the next occurrence of a search pattern
- Move to the previous occurrence of a search pattern
- Find and replace the first instance of a string on the current line (e.g.,
- Find and replace the first instance of a string on a specified set of lines (e.g.,
You can use a
. to represent the current line, and a
$ to represent the last line of the file (e.g.,
- Find and replace all instances of a string on the current line
- Find and replace all instances of a string in an entire file
Working With Text
- Append text after the cursor
- Append text at the end of the current line
- Insert text before the cursor
- Insert text at the beginning of the current line
- Start a new line below the current line, and insert text there
- Start a new line above the current line, and insert text there
- Substitute text at the cursor with new text
This command adds new text starting at the cursor and pushes existing text to the right of the cursor down.
- Substitute the current line with new text
This command wipes out the current line and lets you enter new text in its place.
- Replace character at the cursor
- Replace text starting with the cursor position
- Delete a single character at the cursor position
- Delete a single character before the cursor position
- Delete from the cursor to the end of the current word, deleting the space after the word
By default, deleted text is saved to vim's unnamed buffer. Essentially, you can think of vim deletion commands as cut commands as seen in typical word processors.
- Delete from the cursor to the end of the current word, but don't delete the space after the word
- Delete from the cursor to the end of the line
- Delete from the cursor to the beginning of the line
- Delete the current line
- Delete all text from the cursor to the end of the screen
- Delete all text from the cursor to the end of the file
- Delete all text from the cursor to the beginning of the file
- Join the line below with the line where the cursor is placed
- Undo the last operation
- Undo all changes since last disk write
- Copy the current line to the unnamed buffer
In vim, a copy operation is referred to as a yank, hence the
- Copy from the cursor position to the end of the word to the unnamed buffer
- Paste the unnamed buffer's contents to the right of the cursor position
- Paste the unnamed buffer's contents to the left of the cursor position
- Select text one character at a time
This command puts you into Visual Selection mode. Here, you can perform searches and other types of operations (e.g., press
d to delete a selection,
y to copy a selection,
p to paste a selection after the cursor, and
P to paste a selection before the cursor).
- Select a whole line
Also, vim has a special Visual Block Mode that you can use to insert text into multiple lines simultaneously. To do this:
- select the lines that you want to affect; the cursor should end on the last line to be affected
Ito enter a special version of Insert Mode
- enter your desired text
After a brief pause, your new text should be inserted into the lines you selected.
- Open an external command shell
- Execute a command from within vim, and return to vim after command execution (e.g.,
:! wc %; here, the
%is a placeholder representing the current file you are working on in vim)
- Insert the output of a command after the line containing the cursor
You can learn more about vim by examining its man page:
$ man vim
Also, vim.org has helpful accessible documentation. Specifically, the following are great resources:
If you want to learn vim while playing a challenging game, you might want to give VIM Adventures a try.