Page Body

vim Reference

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:

  1. Understanding vim's Modes
  2. 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 Modes

vim has three basic modes:

  1. Command
  2. Insert
  3. Line

Command Mode

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 Esc.

Insert Mode

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

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 :.

Keyboard Shortcuts

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.

Starting/Stopping vim, Working With Files, Setting Options

$ vim ex_file...
Start vim and edit a file.
$ vim +/ex_string ex_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 +ex_line_number ex_file
Open a file and place the cursor on a specific line (e.g., $ vim +5 'fruit.txt').
:e ex_file
Open a file in vim.
This command opens ex_file in vim without altering the previous file you were working on.
:e #
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 :e.
:w ex_file
Save file as.
:w
Save file.
:w! existing_ex_file
Overwrite a file.
:wq or :exit or :x or ZZ
Save a file and exit vim.
q
Quit vim.
q!
Quit vim without saving modifications.
:set
View currently set vim options. Press Enter or enter a command to continue.
:set all
View all vim options. Press q to continue.
:set ex_option
Set a vim option.
$ vim +ex_option ex_file...
Set a vim option from the command line.

Moving Around

l or Spacebar
Move cursor one character right.
h or Backspace
Move cursor one character left.
3h
Use a force multiplier to move cursor three characters left.
j or
Move cursor down one line.
k or
Move cursor up one line.
w
Move forward one word.
b
Move backward one word.
$
Move to the end of the line.
0
Move to the beginning of the line.
:$ or G
Move to the beginning of the last line of the file.
:0 or 1G
Move to the beginning of first line of the file.
:ex_line_number or ex_line_numberG
Move to a specific line (e.g., :10 or 10G).
Control+f or Page Down
Move forward a page.
Control+b or Page Up
Move backward a page.

Searching For Text

/ex_pattern
Search forward for a pattern (e.g., /pineapples).
?ex_pattern
Search backward for a pattern.
n
Move to the next occurrence of a search pattern.
N
Move to the previous occurrence of a search pattern.
:s/ex_old_string/ex_new_string/
Find and replace the first instance of a string on the current line (e.g., :s/plums/blueberries/).
:ex_line_start,ex_line_ends/ex_old_string/ex_new_string/
Find and replace the first instance of a string on a specified set of lines (e.g., :1,5s/plums/blueberries/).
You can use a . to represent the current line, and a $ to represent the last line of the file (e.g., :.,$s/plums/blueberries/).
:s/ex_old_string/ex_new_string/g
Find and replace all instances of a string on the current line.
:%s/ex_old_string/ex_new_string/g
Find and replace all instances of a string in an entire file.

Working With Text

a
Append text after the cursor.
A
Append text at the end of the current line.
i
Insert text before the cursor.
I
Insert text at the beginning of the current line.
o
Start a new line below the current line, and insert text there.
O
Start a new line above the current line, and insert text there.
s
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.
S
Substitute the current line with new text.
This command wipes out the current line and lets you enter new text in its place.
r
Replace character at the cursor.
R
Replace text starting with the cursor position.
x
Cut a single character at the cursor position.
X
Cut a single character before the cursor position.
dw
Cut from the cursor to the end of the current word, removing the space after the word.
de
Cut from the cursor to the end of the current word, keeping the space after the word.
d$ or D
Cut from the cursor to the end of the line.
d0
Cut from the cursor to the beginning of the line.
dd
Cut the current line.
dL
Cut all text from the cursor to the end of the screen.
dG
Cut all text from the cursor to the end of the file.
d1G
Cut all text from the cursor to the beginning of the file.
J
Join the line below with the line where the cursor is placed.
u
Undo the last operation.
:e!
Undo all changes since last disk write.
yy
Copy the current line to the unnamed buffer.
In vim, a copy operation is referred to as a yank, hence the yy command.
yw
Copy from the cursor position to the end of the word to the unnamed buffer.
p
Paste the unnamed buffer's contents to the right of the cursor position.
P
Paste the unnamed buffer's contents to the left of the cursor position.
v
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 d to cut a selection, y to copy a selection, p to paste a selection after the cursor, and P to paste a selection before the cursor).
V
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:

  • Press Control+v.
  • Select the lines that you want to affect. The cursor should end on the last line to be affected.
  • Press I to enter a special version of Insert Mode.
  • Enter your desired text.
  • Press Escape.

After a brief pause, your new text should be inserted into the lines you selected.

External Commands

:sh
Open an external command shell. Enter exit to continue.
:! ex_command
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.
:r! ex_command
Insert the output of a command after the line containing the cursor.

Documentation

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.

Enjoyed this post?

Subscribe to the feed for the latest updates.