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 copious 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: If you are not familiar with the GNU/Linux command line interface, review the Conventions page before proceeding.
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.
For the following examples, if an entry starts with a
$, that means that it is a command that you enter at the command line. 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.
$ vim ex_file...
- Start vim and edit a file.
$ vim +/ex_string ex_file
- Open file with a search string (
ex_string) and place cursor on first line with an instance of
$ vim +/mangoes 'fruit.txt').
$ vim +ex_line_number ex_file
- Open a file and place the cursor on a specific line (e.g.,
$ vim +5 'fruit.txt').
$ vim +ex_option ex_file...
- Set a vim option from the command line.
ex_fileinto the buffer.
- This command opens
ex_filein vim without altering the previous file that you were working on.
- Read the last-read file again.
- You can use this command to go back to the previous file that you were working on after using the
- Save a file as
- Save file.
- Save file and exit vim.
- Quit vim.
- Quit vim without saving modifications.
- View currently set vim options. Press Enter or enter a command to continue in vim.
- View all vim options. Press q to continue in vim.
- Set a vim option on-the-fly.
- If the option is already set, this command will display the option's status.
- Move cursor one character right.
- 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 forward one word.
- Move backward one word.
- Move to the end of the line.
- Move to the beginning of the line.
- Move to the beginning of the last line of the file.
- Move to the beginning of first line of the file.
- Move to a specific line (e.g.,
- Move forward a page.
- Move backward a page.
- Search forward for a pattern (e.g.,
ex_patterncan be a regular expression.
- 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.,
ex_new_stringcan be regular expressions.
- 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.
- 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.
- Cut a single character at the cursor position.
- Cut a single character before the cursor position.
- Cut from the cursor to the end of the current word, removing the space after the word.
- The cut text is placed in the unnamed buffer.
- Cut from the cursor to the end of the current word, keeping the space after the word.
- Cut from the cursor to the end of the line.
- Cut from the cursor to the beginning of the line.
- Cut the current line.
- Cut all text from the cursor to the end of the screen.
- Cut all text from the cursor to the end of the file.
- Cut 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.
- Reverse an undo operation.
- Undo all changes since last disk write.
- Copy (yank) the current line to the unnamed buffer.
- 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 (select the characters that you want to affect; the cursor should end on the last character to be affected). Here, you can perform searches and other types of operations (e.g., press
dto cut a selection,
yto copy a selection,
pto paste a selection after the cursor, and
Pto 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. Enter
- Execute a command from within vim (e.g.,
:! wc %; here, the
%is a placeholder representing the current file you are working on in vim). Press Enter or enter a command to continue.
- Insert the output of a command after the line containing the cursor.
You can learn more about vim by examining its man page:
man 1 vim
Inside the vim editor, you can access vim's help files by issuing the
:help command. These documents are available online at vimhelp.org, as well.
Also, vim.org has helpful accessible content. 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.