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 GNU/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, or you intend to use a script obtained from this site, review the Conventions page before proceeding.

Vim Modes

Vim has three foundational modes:

  1. Normal
  2. Insert
  3. Command-line

Normal Mode

Normal mode is the default mode for Vim, i.e., when you first start Vim, you will be placed in this mode. In Normal Mode, key presses are interpreted as normal editor commands.

You can always get to Normal 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).

Command-line Mode

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

Command-line commands start with a : (Ex commands), //? (search patterns), or ! (filter commands).

Vim Concepts

When working with Vim, there are several key concepts that it helps to internalize:

Buffer
The in-memory text of a file. Each opened file gets its own buffer.
Window
A viewport on a buffer.
Tab Page
A collection of windows. Windows are comparable to layouts or templates.

It may help to think of windows and buffers as an X-Y-Z coordinate system. The X- and Y-axis create a window, and the buffers are stacked on the Z-axis.

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 it is a command that you enter at the GNU/Linux command line. If an entry does not start with a $, it is a command that is entered inside of the Vim program.

Some entries have actual examples to help clarify what a real command looks like.

Starting/Stopping Vim

vim
Start Vim.
$ vim ex_file...
Start Vim and edit a file.
$ vim -p ex_file...
Start Vim with files in tabs.
$ vim -S ex_session_file
Start Vim and source ex_session_file after loading the first file.
$ vim +/ex_string ex_file
Open file with a search string (ex_string) and place cursor on first line with an instance of ex_string (e.g., $ vim +/mangoes 'fruit.txt').
$ vim +ex_line_num ex_file
Open file and place cursor on specific line (e.g., $ vim +5 'fruit.txt').
$ vim +ex_vim_option ex_file...
Set a Vim option from the command line.
$ vim -r ex_file
Start Vim and edit a file in recovery mode after a system crash.
$ vimdiff ex_file_1 ex_file_2
Compare two files to make edits.
:wq
Write the current file and close the window.
:wqa
Write all changed buffers and exit Vim.
:q
Quit the current window.
:qa
Exit Vim, unless there are some buffers that have been changed.
:q!
Quit without writing, also when the current buffer has changes.
:qa!
Exit Vim. Any changes to buffers are lost.

Setting Options

:set
Show all options that differ from their default value.
:set all
Show all, except terminal options.
:set ex_option
For a Toggle option, switch it on. For a Number amd String option, show its value.
:set number
Print the line number in front of each line.
:set nonumber
Do not print the line number in front of each line.
:set showmode
If in Insert, Replace, or Visual mode, put a message on the last line.
:set spell
When on, spell checking will be done.
:set nospell
Disable spell checking.

Working With Files

:e ex_file
Edit ex_file.
:r ex_file
Insert the file ex_file below the cursor.
Ctrl+g
Prints the current file name, the cursor position, and the file status (readonly, modified, read errors, new file).
:w
Write the whole buffer to the current file.
:w ex_file
Write the specified lines to ex_file.
:w! ex_file
Write the specified lines to ex_file. Overwrite an existing file.
:wa
Write all changed buffers.
:syntax on
Turn syntax checking on.
:syntax off
Turn syntax checking off.
:buffers or :ls
Show all buffers.
Each buffer has a unique number that will not change.
Indicators include (characters in the same list are mutually exclusive):
  • u - An unlisted buffer.
  • % - The buffer in the current window.
  • # - The alternate buffer for :e # and Ctrl+^
    • a - An active buffer that is loaded and visible.
    • h - A hidden buffer that is loaded, but not currently displayed in a window.
      • - - A buffer with modifiable off
      • = - A readonly buffer.
      • R - A terminal buffer with a running job.
      • F - A terminal buffer with a finished job.
      • ? - A terminal buffer without a job.
        • + - A modified buffer.
        • x - A buffer with read errors.
:Nb (where N is the unique buffer number) or :b ex_buffer_name
Edit buffer from the buffer list.
:bn
Go to next buffer in the buffer list.
:bp
Go to previous buffer in the buffer list.
:bd
Unload current buffer and delete it from the buffer list.
A buffer number can be used with this command to unload/delete a specific buffer (:Nbd).
:mks ex_session_file
Write a Vim script that restores the current editing session.
:so ex_session_file
Read Ex commands from ex_session_file. Restores an editing session.

Moving Around

Space or l
Move cursor one character right.
Backspace or h
Move cursor one character left.
or j
Move cursor down one line.
3↓ or 3j
Use a force multiplier to move down three lines.
or k
Move cursor up one line.
w
Move forward one word.
b
Move backward one word.
f{ex_char}
Move to the next {ex_char} character on the line.
End or $
Move to the end of the line.
Home or 0
Move to the first character of the line.
NG or :N (where N is the a line number)
Move to a specific line (e.g., 10G or :10).
G or :$
Move to the first non-blank character of the last line of the file.
1G or :0
Move to the first non-blank character of the first line of the file.
Page Down or Ctrl+f
Move forward a page.
Page Up or Ctrl+b
Move backward a page.
Ctrl+d
Move forward a half-page.
Ctrl+u
Move backward a half-page.

Searching For Text

/ex_pattern
Search forward for ex_pattern (e.g., /pineapples).
ex_pattern can be a regular expression.
?ex_pattern
Search backward for ex_pattern.
n
Move to next occurrence of search pattern.
N
Move to previous occurrence of search pattern.
/\<ex_pattern\>
Search for an exact, whole word.
Also, you can place the cursor anywhere on the word in question and press the asterisk key (*) to accomplish the same thing.
: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_old_string and ex_new_string can be regular expressions.
:{ex_line_start},{ex_line_end}s/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/).
The & character can also be used to refer back to the text matched by your regular expression.
Example:
:s/straw/&berry/
The example above changes straw to strawberry.
: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 line.
i
Insert text before the cursor.
I
Insert text before the first non-blank in the line.
o
Begin a new line below the cursor and insert text.
O
Begin a new line above the cursor and insert text.
s
Delete character into [unnamed register] and start insert.
This command adds new text at the cursor and pushes existing text to the right of the cursor down.
S
Delete line into unnamed register and start insert.
This command wipes out the entire line and lets you enter new text.
r
Replace the character under the cursor.
R
Enter Replace mode. Each character you type replaces an existing character, starting with the character under the cursor.
x
Delete character under the cursor into unnamed register.
Nx
Delete N characters under the cursor into unnamed register.
X
Delete character before the cursor into unnamed register.
NX
Delete N characters before the cursor into unnamed register.
dw
Delete characters under the cursor to the end of the current word into unnamed register, removing the space after the word.
de
Delete characters under the cursor to the end of the current word into unnamed register, keeping the space after the word.
df{ex_char}
Delete characters under the cursor to the next occurrence of the {ex_char} character into unnamed register.
D or d$
Delete the characters under the cursor until the end of the line into unnamed register.
d0
Delete the characters before the cursor to the beginning of the line into unnamed register.
dd
Delete line into unnamed register.
Ndd
Delete N lines into unnamed register.
dL
Delete the characters under the cursor to the end of the screen into unnamed register.
dG
Delete the characters under the cursor to the end of the file into unnamed register.
d1G
Delete the characters under the cursor to the beginning of the file.
J
Join line below with line where cursor is placed.
u
Undo the last operation.
Ctrl+r
Redo last undone change.
:e!
Edit the current file always. Discard any changes to the current buffer. This is useful if you want to start all over again.
yy
Yank (copy) line into unnamed register.
Nyy
Yank N lines into unnamed register.
yw
Yank characters under the cursor to the end of the word into unnamed register.
p
Put (paste) text from unnamed register after the cursor.
P
Put text from unnamed register before the cursor.
"ayy
Overwrite the a buffer with the current line.
"Ayy
Append to the a buffer with the current line.
"ap
Put the contents of the a buffer after the cursor.
"aP
Put the contents of the a buffer before the cursor.
v
Start Visual mode per character.
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
Start Visual mode linewise.

Also, Vim has a special Visual Block mode that you can use to insert text into multiple lines simultaneously. To do this:

  • Press Ctrl+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 Esc.

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

Window/Tab Management

:sp
Split current window horizontally.
A file can be passed to this command as an argument. The original file is placed on the bottom of the editor.
The height of the new window can be set by passing a number to the command like so, :{ex_num}sp.
:vs
Split current window vertically.
A file can be passed to this command as an argument. The original file is placed on the right side of the editor.
Ctrl+w
Activate window mode for split windows (move by using direction keys or pressing w).
After activating window mode, additional commands are available:
  • s - Open new horizontal split.
  • v - Open new vertical split.
  • c - Close window.
  • o - Make current window the only one on the screen. All other windows are closed.

Once you switch to a window, that window becomes active and ready for you to begin issuing commands.

:close
Close selected split window.
:redraw!
Clear and redraw the screen right now.
:tabnew
Open a new tab page with an empty window, after the current tab page.
:tabnew ex_file
Open a new tab page and edit ex_file.
gt
Go to the next tab page. A tab number can be used with this command to go to a specific tab (:Ngt).
gT
Go to the previous tab page.
:tabclose
Close current tab page.

External Commands

:sh
Start a shell.
:! ex_cmd
Execute ex_cmd with the shell (e.g., :! wc %; here, the % is a placeholder representing the current file you are working on in Vim).
:r! ex_cmd
Execute ex_cmd and insert its standard output below the cursor or the specified line.

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 :h command (:h | only to open help in its own window). 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.