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 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: 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 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')
:e example_file
Open a file in vim

This command opens example_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 example_file
Save file as
:w
Save file
:w! existing_example_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
:set all
View all vim options
:set example_option
Set a vim option
$ vim +example_option example_file
Set a vim option from the command line

Moving Around

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
l or Spacebar
Move cursor one character right
w
Move forward one word
b
Move backward one word
0
Move to the beginning of the line
$
Move to the end of the line
:0 or 1G
Move to the beginning of the file
:$ or G
Move to the beginning of the last line of the file
:example_line_number or example_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

/example_pattern
Search forward for a pattern (e.g., /pineapples)
?example_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/old_string/new_string/
Find and replace the first instance of a string on the current line (e.g., :s/plums/blueberries/)
:line_start,line_ends/old_string/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/old_string/new_string/g
Find and replace all instances of a string on the current line
:%s/old_string/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
Delete a single character at the cursor position
X
Delete a single character before the cursor position
dw
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.

de
Delete from the cursor to the end of the current word, but don't delete the space after the word
D or d$
Delete from the cursor to the end of the line
d0
Delete from the cursor to the beginning of the line
dd
Delete the current line
dL
Delete all text from the cursor to the end of the screen
dG
Delete all text from the cursor to the end of the file
d1G
Delete 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. 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).

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
:! example_command
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)
:r! example_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 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.

Avatar

Enjoyed this post?

Subscribe to the feed for the latest updates.