Vim is a powerful command-line code editor tool that’s an enhanced version of the venerable vi editor. Although most people use it with Linux, Vim is compatible with most of the commonly used operating systems, including macOS, Windows, and DOS.
Despite the abundance of graphical rich text editors, familiarity with Vim will help every user, from an experienced system administrator to a newbie programming a Raspberry Pi. However, one important thing to note when using Vim, is that the function of a key depends on the “mode” the editor is in. For example, pressing the alphabet “j” will move the cursor down one line in the “command mode”. You’ll have to switch to the “insert mode” to make the keys input the character they represent.
If you’re new to Vim, open a terminal and run vimtutor
to get you started with some initial commands before diving into the rest.
Download this Cheatsheet
Enter your email below to receive this PDF cheatsheet in your Inbox.
Here’s a cheatsheet to help you get the most out of Vim.
Shortcut Keys | Function |
---|---|
Main | |
Esc | Gets out of the current mode into the “command mode”. All keys are bound of commands. |
I | “Insert mode” for inserting text. Keys behave as expected. |
: | “Last-line mode” where Vim expects you to enter a command such as to save the document. |
:term | Open a terminal window. |
Navigation Keys | |
H | Move the cursor one character to the left. |
J or Ctrl + J | Move the cursor down one line. |
K or Ctrl + P | Move the cursor up one line. |
l | Move the cursor one character to the right. |
0 | Move the cursor to the beginning of the line. |
$ | Move the cursor to the end of the line. |
^ | Move the cursor to the first non-empty character of the line. |
w | Move forward one word (next alphanumeric word). |
W | Move forward one word (delimited by a white space). |
5W | Move forward five words. |
b | Move backward one word (previous alphanumeric word). |
B | Move backward one word (delimited by a white space). |
5B | Move backward five words. |
G | Move to the end of the file. |
GG | Move to the beginning of the file. |
Navigate Around The Document | |
( | Jump to the previous sentence. |
) | Jump to the next sentence. |
{ | Jumps to the previous paragraph. |
} | Jumps to the next paragraph. |
[[ | Jumps to the previous section. |
]] | Jumps to the next section. |
[] | Jump to the end of the previous section. |
][ | Jump to the end of the next section. |
Insert Text | |
a (lowercase A) | Insert text after the cursor. |
A (uppercase A) | Insert text at the end of the line. |
I | Insert text before the cursor. |
o (lowercase O) | Begin a new line below the cursor. |
O (uppercase A) | Begin a new line above the cursor. |
Special Inserts | |
:R [filename] | Insert the file [filename] below the cursor. |
:R ![command] | Execute [command] and insert its output below the cursor. |
Delete Text | |
X | Delete character at cursor. |
DW | Delete a word. |
D0 | Delete to the beginning of a line. |
D$ | Delete to the end of a line. |
D) | Delete to the end of sentence. |
DGG | Delete to the beginning of the file. |
DG | Delete to the end of the file. |
DD | Delete line. |
3DD | Delete three lines. |
Simple Replace Text | |
R{text} | Replace the character under the cursor with {text}. |
R | Replace characters instead of inserting them. |
Copy/Paste Text | |
YY | Copy current line into storage buffer. |
[“x]yy | Copy the current lines into register x. |
p (lowercase P) | Paste storage buffer after current line. |
P (uppercase A) | Paste storage buffer before current line. |
[“X]P | Paste from register x after current line. |
[“X]P | Paste from register x before current line. |
Text Modification | |
CC | Change the text of the current line. |
CE | Change the currently selected word at the current cursor point. |
C$ | Change the text from the cursor position until the end of the line. |
C0 | Change the text from the cursor position until the start of the line. |
G~ | Switch the case of the currently selected text as well as the line adjacent to it to its opposite. |
Gu | Change the case of the currently selected text as well as the line adjacent to it to lowercase. |
GU | Change the case of the currently selected text as well as the line adjacent to it to uppercase. |
Context-Specific Modification | |
CIW | Change the currently selected word regardless of cursor position. |
CIP | Change the currently selected paragraph or code block regardless of cursor position. |
CI< | Change the content of a code block enclosed in angle brackets (<>). |
CI{ | Change the content of a code block enclosed in curly brackets ({}). |
DIW | Delete the currently selected word regardless of cursor position. |
DIP | Delete the currently selected paragraph or code block regardless of cursor position. |
DAW | Delete the currently selected word as well as the spaces before and after it. |
DI< | Delete the content of a code block enclosed in angle brackets (<>). |
DI{ | Delete the content of a code block enclosed in curly brackets ({}). |
YIW | Copy the currently selected word regardless of cursor position. |
YIP | Copy the currently selected paragraph or code block regardless of cursor position. |
YAW | Copy the currently selected word as well as the spaces before and after it. |
YI< | Copy the content of a code block enclosed in angle brackets (<>). |
YI{ | Copy the content of a code block enclosed in curly brackets ({}). |
Undo/Redo Operation | |
U | Undo the last operation. |
Ctrl + R | Redo the last undo. |
:earlier 1m | Go back 1 minute in the history of the document. |
:later 1m | Go forward 1 minute in the history of the document. |
Search and Replace Keys | |
/search_text | Search document for search_text going forward. |
?search_text | Search document for search_text going backward. |
n (lowercase n) | Move to the next instance of the result from the search. |
N (uppercase A) | Move to the previous instance of the result. |
:%s/original/replacement | Search for the first occurrence of the string “original” and replace it with “replacement”. |
:%s/original/replacement/g | Search and replace all occurrences of the string “original” with “replacement”. |
:%s/original/replacement/gc | Search for all occurrences of the string “original” but ask for confirmation before replacing them with “replacement”. |
F | Search for the next occurrence of a character or go to the previous occurrence. |
Bookmarks | |
M {a-z A-Z} | Set bookmark {a-z A-Z} at the current cursor position. |
:marks | List all bookmarks. |
`{a-z A-Z} | Jumps to the bookmark {a-z A-Z}. |
Select Text | |
v (lowercase V) | Enter visual mode per character. |
V (uppercase A) | Enter visual mode per line. |
Esc | Exit visual mode. |
Modify Selected Text | |
~ | Switch case. |
D | Delete a word. |
C | Change a word. |
Y | Yank (copy) a word. |
> | Shift right. |
< | Shift left. |
! | Filter through an external command. |
Text Indentation | |
>> | Shift the currently selected line one Tab length to the right. |
<< | Shift the currently selected line one Tab length to the left. |
gg=G | Shift the current buffer by one Tab length. |
>% | Shift the currently selected code block to the right. |
<% | Shift the currently selected code block to the left. |
>IB | Shift the contents of the currently selected block to the right. |
<IB | Shift the contents of the currently selected block to the left. |
>AT | Shift the contents of a block that is enclosed in angle brackets (<>) to the right. |
<AT | Shift the contents of a block that is enclosed in angle brackets (<>) to the left. |
Macros | |
Q[character] | Record a Vim macro that is accessible under the [character] key. |
Q | Stop a Vim macro recording. |
@[character] | Execute a recorded Vim macro that was saved under the [character] key. |
@@ | Execute the last Vim macro that was run. |
Search and Execute | |
:G/[pattern]/[vim command] | Run the [vim command] in the current line for every instance of the [pattern]. |
:G!/[pattern]/[vim command] | Run the [vim command] in the current line for every instance that is not the [pattern]. |
:%G/[pattern]/[vim command] | Run the [vim command] on the entire document for every instance of the [pattern]. |
:%G!/[pattern]/[vim command] | Run the [vim command] on the entire document for every instance that is not the [pattern]. |
Tab Manipulation | |
:TABE [file path] | Open the [file path] in a separate tab in the current buffer. |
:TABN | Switch to the next active tab in the buffer. |
:TABP | Switch to the previous active tab in the buffer. |
:TABC | Close the currently active tab in the buffer. |
:TABO | Close all inactive tabs in the buffer. |
Buffer Manipulation | |
:SP [file path] | Open the [file path] as a horizontally split buffer. |
:VSP [file path] | Open the [file path] as a vertically split buffer. |
:LS | Show a list of all the buffers currently open in Vim. |
:BN | Display the contents of the next open buffer in the buffer list. |
:BP | Display the contents of the previous open buffer in the buffer list. |
Ctrl + W, then S | Split the current window horizontally, displaying the currently active buffer in both windows. |
Ctrl + W, then V | Split the current window vertically, displaying the currently active buffer in both windows. |
Ctrl + W, then W | Switch focus between the split buffers. |
Ctrl + W, then X | Swap the position of the currently focused buffer with an inactive one. |
Ctrl + W, then Q | Delete the currently selected buffer. |
Save and Quit | |
:Q | Quits Vim but fails when file has been changed. |
:W | Save the file. |
:W new_name | Save the file with the new_name filename. |
:WQ | Save the file and quit Vim. |
:Q! | Quit Vim without saving the changes to the file. |
ZZ | Write file, if modified, and quit Vim. |
ZQ | Same as :q! Quits Vim without writing changes. |
:sav file :saveas file | Save file as. |
:clo :close | Close the current pane. |
Image credit: Unsplash
Our latest tutorials delivered straight to your inbox