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 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:
- Normal
- Insert
- 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_fileafter 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 ofex_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_filebelow 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.
:buffersor: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 #andCtrl+^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 withmodifiableoff=- 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(whereNis 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
Spaceorl- Move cursor one character right.
Backspaceorh- Move cursor one character left.
↓orj- Move cursor down one line.
3↓or3j- Use a force multiplier to move down three lines.
↑ork- 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. Endor$- Move to the end of the line.
Homeor0- Move to the first character of the line.
NGor:N(whereNis the a line number)- Move to a specific line (e.g.,
10Gor:10). Gor:$- Move to the first non-blank character of the last line of the file.
1Gor:0- Move to the first non-blank character of the first line of the file.
Page DownorCtrl+f- Move forward a page.
Page UporCtrl+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_patterncan 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_stringandex_new_stringcan 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
strawtostrawberry. :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
Ncharacters under the cursor into unnamed register. X- Delete character before the cursor into unnamed register.
NX- Delete
Ncharacters 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. Dord$- 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
Nlines 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
Nlines 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
abuffer with the current line. "Ayy- Append to the
abuffer with the current line. "ap- Put the contents of the
abuffer after the cursor. "aP- Put the contents of the
abuffer 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
dto cut a selection,yto copy a selection,pto paste a selection after the cursor, andPto 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
Ito 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_cmdwith 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_cmdand 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.