
VIM Cheat Sheet
- Mohammad Abu Mattar
- Linux , VIM Emulation
- Published: 20 Oct, 2022
- Updated: 05 Apr, 2025
- 09 Mins read
What Is VIM?
VIM (Vi Improved) is a versatile text editor pre-installed on most Linux systems, known for its efficiency in command-line file editing. Its modal nature—switching between modes like Normal, Insert, and Command—makes it a favorite among developers and system administrators. This cheat sheet serves as a handy guide to mastering VIM’s core functionalities.
VIM Modes
VIM operates in three primary modes, each designed for specific tasks, allowing users to navigate, edit, and manage files seamlessly.
Normal Mode
Normal Mode is VIM’s default state for navigation and manipulation. Use it to move the cursor, delete text, or copy content, entering it with the Esc key.
Insert Mode
Insert Mode enables text input and editing. Activate it with the i key to start typing at the cursor’s position.
Command Mode
Command Mode lets you execute file-level operations like saving or quitting. Access it by pressing : from Normal Mode.
VIM Commands
This section lists essential VIM commands, organized by function, to streamline your workflow.
Global
Global commands affect the entire file or VIM environment, such as saving files or accessing help.
Command | Description |
---|---|
:h[elp] keyword | Open help for keyword. |
:sav[eas] file | Save the file as file. |
:clo[se] | Close the current file. |
:ter[minal] | Open a terminal. |
K | Open help for word under cursor. |
Cursor Movement
These commands control cursor navigation, helping you move efficiently within a file.
Command | Description |
---|---|
h | Move left. |
j | Move down. |
k | Move up. |
l | Move right. |
gj | Move down a screen. |
gk | Move up a screen. |
H | Move to top of screen. |
M | Move to middle of screen. |
L | Move to bottom of screen. |
w | Move forward one word. |
W | Move forward one WORD. |
e | Move to end of word. |
E | Move to end of WORD. |
b | Move back one word. |
B | Move back one WORD. |
ge | Move to end of previous word. |
gE | Move to end of previous WORD. |
% | Move to matching character (default supported pairs: (), , []). |
0 | Move to the start of the line. |
^ | Move to the first non-blank character of the line. |
$ | Move to the end of the line. |
g_ | Move to the last non-blank character of the line. |
gg | Move to the first line of the file. |
G | Move to the last line of the file. |
5G | Move to line 5. |
gd | Move to the definition of the word under the cursor. |
gD | Move to the definition of the WORD under the cursor. |
fx | Move to the next occurrence of character x. |
Fx | Move to the previous occurrence of character x. |
tx | Move to the character before the next occurrence of character x. |
Tx | Move to the character 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, but in the opposite direction. |
} | Move to the next paragraph (or function/block, when editing code). |
{ | Move to the previous paragraph (or function/block, when editing code). |
zz | Center cursor on screen. |
Ctrl + e | Move screen down one line (without moving cursor). |
Ctrl + y | Move screen up one line (without moving cursor). |
Ctrl + b | Move back one full screen. |
Ctrl + f | Move forward one full screen. |
Ctrl + d | Move forward half a screen. |
Ctrl + u | Move back half a screen. |
Insert Mode
Insert Mode commands facilitate text entry and minor adjustments while typing.
Command | Description |
---|---|
i | Insert before the cursor. |
I | Insert at the beginning of the line. |
a | Insert (append) after the cursor. |
A | Insert (append) at the end of the line. |
o | Append (open) a new line below the current line. |
O | Append (open) a new line above the current line. |
ea | Insert (append) at the end of the word. |
Ctrl + h | Delete the character before the cursor. |
Ctrl + w | Delete the word before the cursor. |
Ctrl + j | Begin a new line. |
Ctrl + t | Shift the current line one tab to the right. |
Ctrl + d | Shift the current line one tab to the left. |
Ctrl + n | Insert the next completion of the word under the cursor. |
Ctrl + p | Insert the previous completion of the word under the cursor. |
Ctrl + rx | Insert the register x contents. |
Ctrl + ox | Temporarily enter Normal Mode to issue a single command. |
ESC | Exit Insert Mode. |
Editing
Editing commands allow you to modify text, from single characters to entire lines.
Command | Description |
---|---|
r | Replace a single character. |
R | Replace multiple characters. |
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 | Reformat paragraph. |
g~ | Switch case of character under cursor. |
gu | Make character under cursor lowercase. |
gU | Make character under cursor uppercase. |
cc | Change (replace) an entire line. |
c$ | Change (replace) to the end of the line. |
C | Change (replace) to the end of the line. |
ciw | Change (replace) the entire word. |
cw | Change (replace) to the end of the word. |
ce | Change (replace) to the end of the word. |
s | Delete character under cursor and substitute text. |
S | Delete line and substitute text (same as cc). |
xp | Transpose two letters (delete and paste). |
u | Undo. |
U | Undo all changes on line. |
Ctrl + r | Redo. |
. | Repeat last command. |
Marking Text (Visual Mode)
Visual Mode commands help you select text for further actions like copying or deleting.
Command | Description |
---|---|
v | Start Visual Mode, mark lines, then do a command (like y-yank). |
V | Start linewise Visual Mode. |
o | Move to other end of marked area. |
O | Move to other corner of block. |
Ctrl + v | Start Visual Block Mode. |
aw | Mark a word. |
ab | A block with () |
aB | A block with |
at | A block with <> |
ib | Inner block with () |
iB | Inner block with |
it | Inner block with <> |
Esc | Exit Visual Mode. |
Visual Commands
These commands operate on text selected in Visual Mode, such as shifting or copying.
Command | 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
Registers store text for later use, enhancing copy-paste efficiency.
Command | Description |
---|---|
:reg[isters] | List all registers. |
”xy | Yank text into register x. |
”xp | Paste text from register x. |
”xP | Paste before cursor. |
”+y | Yank into system clipboard. |
Marks and Positions
Marks and positions let you bookmark and jump to specific locations in a file.
Command | Description |
---|---|
:marks | List all marks. |
ma | Set current position for mark a. |
a | Jump to mark a. |
y’a | Yank to mark a. |
0 | Jump to start of line. |
” | Jump to last edit position. |
. | Jump to last edit position. |
Jump to last edit position. | |
:ju[mps] | List all jump positions. |
Ctrl + o | Jump to previous position. |
Ctrl + i | Jump to next position. |
:changes | List all changes. |
g, | Jump to previous change. |
g; | Jump to next change. |
Ctrl + j | Jump to next change. |
Macros
Macros record and replay sequences of commands to automate repetitive tasks.
Command | Description |
---|---|
qa | Start recording macro a. |
q | Stop recording macro. |
@a | Execute macro a. |
@@ | Execute last macro. |
Cut and Paste
Cut and paste commands handle copying, deleting, and moving text.
Command | 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. |
y$ | Yank (copy) to end of line. |
yiw | Yank (copy) inner word. |
yaw | Yank (copy) a word including the surrounding white space. |
p | Put (paste) the clipboard after cursor. |
P | Put (paste) before cursor. |
gp | Put (paste) after cursor. |
gP | Put (paste) before cursor. |
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. |
D | Delete (cut) to the end of the line. |
d$ | Delete (cut) to the end of the line. |
diw | Delete (cut) inner word. |
daw | Delete (cut) a word including the surrounding white space. |
x | Delete (cut) character. |
Indent Text
Indentation commands adjust text alignment, crucial for code formatting.
Command | Description |
---|---|
>> | Shift text right. |
<< | Shift text left. |
>% | Shift selected lines right. |
<% | Shift selected lines left. |
>ib | Shift inner block right. |
<ib | Shift inner block left. |
>at | Shift block right. |
<at | Shift block left. |
3== | Auto indent 3 lines. |
=% | Auto indent selected lines. |
=iB | Auto indent inner block. |
gg=G | Auto indent whole file. |
]p | Paste and indent. |
Exiting
Exiting commands manage how you save and close files in VIM.
Command | Description |
---|---|
:w | Write (save) the file, but don’t exit. |
:w !sudo tee % | Write out the current file using sudo. |
:wq | Write (save) and quit. |
:x | Write (save) and quit. |
ZZ | Write (save) and quit. |
:q | Quit (fails if there are unsaved changes). |
:q! | Quit and throw away unsaved changes. |
ZQ | Quit and throw away unsaved changes. |
:wqa | Write (save) and quit on all tabs. |
:qa | Quit on all tabs. |
Search and Replace
Search and replace commands help you find and modify text efficiently.
Command | Description |
---|---|
/pattern | Search for pattern. |
?pattern | Search backward for pattern. |
\vpattern | Search for lines not containing pattern. |
n | Repeat search in 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. |
:s/old/new/g | Replace all old with new in the current line. |
:s/old/new/gc | Replace all old with new in the current line with confirmations. |
:noh[lsearch] | Remove highlighting of search matches. |
Search in Multiple Files
These commands extend search capabilities across multiple files.
Command | Description |
---|---|
:vimgrep /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. |
:ccl[ose] | Close the window. |
Tabs
Tab commands manage multiple files in separate tabs for multitasking.
Command | Description |
---|---|
:tabe | Open a new tab. |
:tabnew {page.words.file} | Open a new tab. |
gt | Move to the next tab. |
:tabn[ext] | Move to the next tab. |
gT | Move to the previous tab. |
:tabp[revious] | Move to the previous tab. |
#gt | Move to tab #. |
:tabm[ove] {number} | Move the current tab to the {number}th position (indexed from 0). |
:tabc[lose] | Close the current tab. |
:tabo[nly] | Close all tabs except for the current one. |
:tabr[ewind] | Move to the first tab. |
:tabfir[st] | Move to the first tab. |
:tabl[ast] | Move to the last tab. |
:tabs | List all tabs. |
:tabdo | Run a command for each open tab. |
:tabdo {cmd} | Run {cmd} for each open tab. |
Working with Multiple Files
These commands handle multiple files using buffers and window splits.
Command | 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. |
:b[uffer]{number} | Go to buffer {number}. |
:b[uffer] file | Go to buffer {file}. |
:ls | List all open buffers. |
:buffers | List all open buffers. |
:sp[lit] {file} | Open a file in a new buffer and split window. |
:vsp[lit] {file} | Open a file in a new buffer and vertically split window. |
:vert[ical] ba[ll] | Open a file in a new buffer and vertically split window. |
:tab ba[ll] | Open a file in a new buffer and split window. |
:tabe[dit] {file} | Open a file in a new buffer and split window. |
:tabnew {file} | Open a file in a new buffer and split window. |
:tab sball | Open a file in a new buffer and split window. |
Ctrl + ws | Split window. |
Ctrl + ww | Move to the next window. |
Ctrl + wq | Close the current window. |
Ctrl + wv | Split window vertically. |
Ctrl + wh | Move to the window on the left. |
Ctrl + wl | Move to the window on the right. |
Ctrl + wj | Move to the window below. |
Ctrl + wk | Move to the window above. |
Ctrl + w= | Make all windows the same height. |
Ctrl + wH | Move the current window to the far left. |
Ctrl + wL | Move the current window to the far right. |
Ctrl + wJ | Move the current window to the bottom. |
Ctrl + wK | Move the current window to the top. |
Summary
This guide covers VIM’s essentials: navigating files, editing text, performing search-and-replace operations, and managing multiple files. With practice, these commands will boost your productivity in this powerful editor.