Vim is a highly configurable and powerful text editor that is commonly used in Unix and Linux environments. It is known for its efficiency and speed. Once you master the shortcut key, you can greatly enhance your productivity when working with text files. In this article, I am going to develop a Vim Cheat Sheet, by which you will learn about the shortcut keys and descriptions associated with it. So let’s start!
Download Vim Cheat Sheet
List of Some Useful Shortcuts to Operate Vim in Your Distribution
Vim operates in different modes: Normal mode, Insert mode, and Visual mode. In Normal mode, you can navigate the file, make edits, and execute various commands. Insert mode allows you to enter text, while Visual mode enables selecting and manipulating blocks of text. Given below, I have listed multiple shortcut keys that might help you while working with Vim.
Global
Vim is highly customizable, and you can define your own shortcuts and mappings to suit your needs. Let’s first understand the global shortcut of it.
Shortcuts |
Description |
:h[elp] keyword |
Display the details of the keyword followed by the help command. |
:sav[eas] file |
Save the current buffer with a new filename |
:clo[se] |
Close current pane |
:ter[minal] |
Open a terminal window |
K |
Open the man page for the word under the cursor |
Cursor Movement
Cursor movement is a fundamental aspect of editing in Vim. Vim provides a wide range of shortcuts and commands to navigate quickly within a file. Here are some of the basic cursor movement shortcuts in Vim.
Shortcuts |
Description |
h |
Bring the cursor to the left |
j |
Bring the cursor down |
k |
Bring the cursor up |
l |
Bring the cursor to the right |
gj |
Bring the cursor down (multi-line text) |
gk |
Bring the cursor up (multi-line text) |
H |
Bring the cursor to the top of screen |
M |
Bring the cursor to the middle of screen |
L |
Move to the bottom of screen |
w |
Jump forwards to the start of a word |
W |
Bring the cursor to the start of the next word. |
e |
Jump forwards to the end of a word |
E |
Jump forwards to the end of a word (words can contain punctuation) |
b |
Jump backward to the start of a word |
B |
Jump backward to the start of a word (words can contain punctuation) |
ge |
Jump backward to the end of a word |
gE |
Jump backward to the end of a word (words can contain punctuation) |
% |
Move the cursor between matching opening and closing brackets (parentheses, square brackets, or curly braces). |
0 |
Jump to the start of the line |
^ |
Jump to the first non-blank character of the line |
$ |
Jump to the end of the line |
g_ |
Jump to the last non-blank character of the line |
gg |
Go to the first line of the document |
G |
Pressing ‘G’ (in normal mode) moves the cursor to the last line of the file. It is equivalent to typing ‘gg’ followed by ‘G’. |
gd |
Move to the local declaration |
gD |
Move to the global declaration |
fx |
Jump to the next occurrence of character x |
tx |
Jump to before the next occurrence of character x |
Fx |
Jump to the previous occurrence of character x |
Tx |
Jump to after the previous occurrence of character x |
; |
Repeat the previous f, t, F or T movement |
, |
Repeat the previous f, t, F or T movement, backward |
} |
Jump to the next paragraph (or function/block, when editing code) |
{ |
Jump to the previous paragraph (or function/block, when editing code)
Ctrl + u – move back 1/2 a screen |
zz |
Centre cursor on the screen |
zt |
Position the cursor on top of the screen |
zb |
Position the cursor on the bottom of the screen |
CTRL + E |
Move the screen down one line (without moving the cursor) |
CTRL + Y |
Move the screen up one line (without moving the cursor) |
CTRL + B |
Move back one fullscreen |
CTRL + F |
Move forward one full screen |
CTRL + D |
Move forward 1/2 a screen |
Note: Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.
Insert Mode – Inserting/Appending Text
In Vim, the Insert mode is where you can directly insert or append text into your file. Here’s an overview of how to enter Insert mode and perform text insertion and appending
Shortcuts |
Description |
i |
Moves the cursor to the beginning of the current line and switches to Insert mode. |
I |
Insert at the beginning of the line |
a |
Moves the cursor one character to the right |
A |
Moves the cursor to the end of the current line. |
o |
Creates a new line below the current line. |
O |
Creates a new line above the current line. |
ea |
Insert (append) at the end of the word |
CTRL + H |
Deletes the character before the cursor |
CTRL + W |
Delete word before the cursor |
CTRL + J |
Begin new line |
CTRL + T |
Indent (move right) line one shiftwidth |
CTRL + D |
De-Indent (move left) line one shiftwidth |
CTRL + N |
Insert (auto-complete) next match before the cursor |
CTRL + P |
Insert (auto-complete) previous match before the cursor |
CTRL + RX |
Insert the contents of register x |
CTRL + OX |
Temporarily enter normal mode to issue one normal-mode command x. |
ESC OR CTRL + C |
Exit insert mode |
Cut and Paste
In Vim, cutting and pasting text involves using commands such as d for delete and p for paste. Here are some shortcut keys that you can use while cutting and pasting text in Vim.
Shortcuts |
Description |
yy |
Yank (copy) a line |
2yy |
Yank (copy) 2 lines |
yw |
Yank (copy) the characters of the word from the cursor position to the start of the next word |
yiw |
Yank (copy) word under the cursor |
yaw |
Yank (copy) word under the cursor and the space after or before it |
y$ or Y |
Yank (copy) to end of line |
p |
Put (paste) the clipboard after cursor |
P |
Put (paste) before cursor |
gp |
Put (paste) the clipboard after cursor and leave cursor after the new text |
gP |
Put (paste) before cursor and leave cursor after the new text |
dd |
Delete (cut) a line |
2dd |
Delete (cut) 2 lines |
dw |
Delete (cut) the characters of the word from the cursor position to the start of the next word |
diw |
Delete (cut) word under the cursor |
daw |
Delete (cut) word under the cursor and the space after or before it |
:3,5d |
Delete lines starting from 3 to 5 |
Search and Replace
Searching and replacing text is a powerful feature in Vim that allows you to find specific patterns and replace them with desired strings.
Shortcuts |
Description |
/pattern |
Search for pattern |
?pattern |
Search backward for pattern |
\vpattern |
‘very magic’ pattern: Non-Alphanumeric characters are interpreted as special regex symbols (no escaping needed) |
n |
Repeat search in the same direction |
N |
Repeat search in opposite direction |
:%s/old/new/g |
Replace all old with new throughout file |
:%s/old/new/gc |
Replace all old with new throughout file with confirmations |
:noh[lsearch] |
Remove highlighting of search matches |
Editing
Editing in Vim involves various commands and techniques for modifying text within a file. Here’s an introduction to some essential editing operations in Vim.
Shortcuts |
Description |
r |
Replace a single character. |
R |
Replace more than one character until ESC is pressed. |
J |
Join line below to the current one with one space in between |
gJ |
Join line below to the current one without space in between |
gwip |
Reflow paragraph |
g~ |
Switch case up to motion |
gu |
Change to lowercase up to motion |
gU |
Change to uppercase up to motion |
cc |
Change (replace) entire line |
c$ or C |
Change (replace) to the end of the line |
ciw |
Change (replace) entire word |
cw or ce |
Change (replace) to the end of the word |
s |
Delete character and substitute text |
S |
Delete line and substitute text (same as cc) |
xp |
Transpose two letters (delete and paste) |
u |
Undo |
U |
Restore (undo) last changed line |
CTRL + R |
Redo |
. |
Repeat last command |
Marking Text (Visual Mode)
In Vim, marking text using Visual mode allows you to select and manipulate a specific range of text. Visual mode provides different ways to select text, including character-wise, line-wise, and block-wise selection.
Shortcuts |
Description |
v |
Start visual mode, mark lines, then do a command (like y-yank) |
V |
Start line wise visual mode |
o |
Move to other end of marked area |
CTRL + V |
Start visual block mode |
O |
Move to other corner of block |
aw |
Mark a word |
ab |
A block with () |
aB |
A block with {} |
at |
A block with <> tags |
ib |
Inner block with () |
iB |
Inner block with {} |
it |
Inner block with <> tags |
ESC OR CTRL + C |
Exit visual mode |
Visual Commands
Visual commands in Vim allow you to perform various operations on visually selected text. When in Visual mode, you can highlight and manipulate specific ranges of text using a combination of key commands.
Shortcuts |
Description |
> |
Shift text right |
< |
Shift text left |
y |
Yank (copy) marked text |
d |
Delete marked text |
~ |
Switch case |
u |
Change marked text to lowercase |
U |
Change marked text to uppercase |
Registers
In Vim, registers are used to store and manipulate text. They act as temporary storage spaces where you can copy, cut, or yank text for later use. Vim provides several registers, each identified by a letter or a special symbol.
Shortcuts |
Description |
:reg[isters] |
Show registers content |
“xy |
Yanks (copies) the selected text and stores it in the register x |
“xp |
Retrieves the text from register x and inserts it in the current location. |
“+y |
Yank into the system clipboard register |
“+p |
puts the contents of the system clipboard register after the cursor position |
0 |
moves the cursor to the beginning of the current line. |
“ |
Unnamed register, last delete or yank |
% |
Current file name |
# |
Alternate file name |
Marks and Positions
In Vim, marks and positions are used to navigate and reference specific locations within a file. They allow you to quickly jump to marked locations or recall specific positions for later use.
Shortcuts |
Description |
:marks |
List of marks |
ma |
Set current position for mark A |
`a |
Jump to position of mark A |
y`a |
Yank text to position of mark A |
`0 |
Go to the position where Vim was previously exited |
`” |
Go to the position when last editing this file |
`. |
Go to the position of the last change in this file |
“ |
Go to the position before the last jump |
:ju[mps] |
List of jumps |
CTRL + I |
Go to newer position in jump list |
CTRL + O |
Go to older position in jump list |
:changes |
List of changes |
g, |
Go to newer position in change list |
g; |
Go to older position in change list |
CTRL + ] |
Jump to the tag under cursor |
Macros
Macros in Vim are a powerful feature that allows you to record a sequence of commands and replay them on demand. They are particularly useful for automating repetitive tasks and performing complex editing operations efficiently.
Shortcuts |
Description |
qa |
Record macro |
q |
Stop recording macro |
@a |
Run macro |
@@ |
Rerun last run macro |
Indent Text
In Vim, there are several ways to indent text, whether it’s a single line or a block of code. Here are a few shortcut keys to indent text in Vim:
Shortcuts |
Description |
>> |
Indent (move right) line one shiftwidth |
<< |
De-indent (move left) line one shiftwidth |
>% |
Indent a block with () or {} (cursor on brace) |
<% |
De-Indent a block with () or {} (cursor on brace) |
>ib |
Indent inner block with () |
>at |
Indent a block with <> tags |
3== |
Re-indent 3 lines |
=% |
Re-indent a block with () or {} (cursor on brace) |
=iB |
Re-indent inner block with {} |
gg=G |
Re-indent entire buffer |
]p |
Paste and adjust indent to current line |
Exiting
In Vim, there are several ways to exit the editor and close your file. Here are some common shortcut keys are listed below:
Shortcuts |
Description |
:w |
Write (save) the file, but don’t exit |
:w !sudo tee % |
Write out the current file using sudo |
:wq or ZZ |
Write (save) and quit |
:q |
Quit (fails if there are unsaved changes) |
:q! or ZQ |
Quit and throw away unsaved changes |
:wqa |
Write (save) and quit on all tabs |
Diff
In Vim, the diff feature allows you to compare and highlight the differences between two or more files. It enables you to see the changes made between different versions of a file or compare files side by side.
Shortcuts |
Description |
zf |
Manually define a fold up to motion |
zd |
Delete fold under the cursor |
za |
Toggle fold under the cursor |
zo |
Open fold under the cursor |
zc |
Close fold under the cursor |
zr |
Reduce (open) all folds by one level |
zm |
Fold more (close) all folds by one level |
zi |
Toggle folding functionality |
]c |
Jump to start of next change |
[c |
Jump to start of previous change |
do or :diffg[et] |
Obtain (get) difference (from other buffer) |
dp or :diffpu[t] |
Put difference (to other buffer) |
:diffthis |
Make current window part of diff |
:dif[fupdate] |
Update differences |
:diffo[ff] |
Switch off diff mode for current window |
Tabs
In Vim, tabs are a convenient way to manage multiple files within a single Vim session. Tabs allow you to work on multiple files simultaneously while keeping them organised and easily accessible.
Shortcuts |
Description |
:tabnew or :tabnew {page.words.file} – |
Open a file in a new tab |
CTRL + wT |
Move the current split window into its own tab |
gt or :tabn[ext] |
Move to the next tab |
gT or :tabp[revious] |
Move to the previous tab |
#gt |
Move to tab number # |
:tabm[ove] # |
Move current tab to the #th position (indexed from 0) |
:tabc[lose] |
Close the current tab and all its windows |
:tabo[nly] |
Close all tabs except for the current one |
:tabdo command |
Run the command on all tabs (e.g. :tabdo q – closes all opened tabs) |
Working With Multiple Files
In Vim, you can work with multiple files simultaneously within a single Vim session. This allows you to edit and navigate through different files efficiently.
Shortcuts |
Description |
:e[dit] file |
Edit a file in a new buffer |
:bn[ext] |
Go to the next buffer |
:bp[revious] |
Go to the previous buffer |
:bd[elete] |
Delete a buffer (close a file) |
:b[uffer]# |
Go to a buffer by index # |
:b[uffer] file |
Go to a buffer by file |
:ls or :buffers |
List all open buffers |
:sp[lit] file |
Open a file in a new buffer and split window |
:vs[plit] file |
Open a file in a new buffer and vertically split window |
:vert[ical] ba[ll] |
Edit all buffers as vertical windows |
:tab ba[ll] |
Edit all buffers as tabs |
CTRL + ws |
Split window |
CTRL + wv |
Split window vertically |
CTRL + ww |
Switch windows |
CTRL + wq |
Quit a window |
CTRL + wx |
Exchange current window with next one |
CTRL + w= |
Make all windows equal height & width |
CTRL + wh |
Move cursor to the left window (vertical split) |
CTRL + wl |
Move cursor to the right window (vertical split) |
CTRL + wj |
Move cursor to the window below (horizontal split) |
CTRL + wH |
Make current window full height at far left (leftmost vertical window) |
CTRL + wL |
Make current window full height at far right (rightmost vertical window)
he very top (topmost horizontal window) |
CTRL + wJ |
Make current window full width at the very bottom (bottommost horizontal window) |
CTRL + wK |
Make current window full width |
Search In Multiple Files
You can search for a pattern across multiple files simultaneously using the command such as :vimgrep command. This allows you to perform a search operation that spans multiple files within a single Vim session. Here some shortcuts along with :vimgrep are listed below.
Shortcuts |
Description |
:vim[grep] /pattern/ {`{file}`} |
Search for pattern in multiple files |
:cn[ext] |
Jump to the next match |
:cp[revious] |
Jump to the previous match |
:cope[n] |
Open a window containing the list of matches |
:ccl[ose] |
Close the quickfix window |
Conclusion
In conclusion, Vim is a powerful text editor with numerous features. Here in this article, I have covered various aspects, including commands, modes, and functionality. We started with an introduction to Vim’s cheat sheet, providing an overview of commonly used commands and shortcuts. Hope this might help you while working with Vim. However, if you have any questions or queries related to this article feel free to comment below. Thank You.