Type something to search...
VIM Cheat Sheet

VIM Cheat Sheet

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.

CommandDescription
:h[elp] keywordOpen help for keyword.
:sav[eas] fileSave the file as file.
:clo[se]Close the current file.
:ter[minal]Open a terminal.
KOpen help for word under cursor.

Cursor Movement

These commands control cursor navigation, helping you move efficiently within a file.

CommandDescription
hMove left.
jMove down.
kMove up.
lMove right.
gjMove down a screen.
gkMove up a screen.
HMove to top of screen.
MMove to middle of screen.
LMove to bottom of screen.
wMove forward one word.
WMove forward one WORD.
eMove to end of word.
EMove to end of WORD.
bMove back one word.
BMove back one WORD.
geMove to end of previous word.
gEMove to end of previous WORD.
%Move to matching character (default supported pairs: (), , []).
0Move 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.
ggMove to the first line of the file.
GMove to the last line of the file.
5GMove to line 5.
gdMove to the definition of the word under the cursor.
gDMove to the definition of the WORD under the cursor.
fxMove to the next occurrence of character x.
FxMove to the previous occurrence of character x.
txMove to the character before the next occurrence of character x.
TxMove 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).
zzCenter cursor on screen.
Ctrl + eMove screen down one line (without moving cursor).
Ctrl + yMove screen up one line (without moving cursor).
Ctrl + bMove back one full screen.
Ctrl + fMove forward one full screen.
Ctrl + dMove forward half a screen.
Ctrl + uMove back half a screen.

Insert Mode

Insert Mode commands facilitate text entry and minor adjustments while typing.

CommandDescription
iInsert before the cursor.
IInsert at the beginning of the line.
aInsert (append) after the cursor.
AInsert (append) at the end of the line.
oAppend (open) a new line below the current line.
OAppend (open) a new line above the current line.
eaInsert (append) at the end of the word.
Ctrl + hDelete the character before the cursor.
Ctrl + wDelete the word before the cursor.
Ctrl + jBegin a new line.
Ctrl + tShift the current line one tab to the right.
Ctrl + dShift the current line one tab to the left.
Ctrl + nInsert the next completion of the word under the cursor.
Ctrl + pInsert the previous completion of the word under the cursor.
Ctrl + rxInsert the register x contents.
Ctrl + oxTemporarily enter Normal Mode to issue a single command.
ESCExit Insert Mode.

Editing

Editing commands allow you to modify text, from single characters to entire lines.

CommandDescription
rReplace a single character.
RReplace multiple characters.
JJoin line below to the current one with one space in between.
gJJoin line below to the current one without space in between.
gwipReformat paragraph.
g~Switch case of character under cursor.
guMake character under cursor lowercase.
gUMake character under cursor uppercase.
ccChange (replace) an entire line.
c$Change (replace) to the end of the line.
CChange (replace) to the end of the line.
ciwChange (replace) the entire word.
cwChange (replace) to the end of the word.
ceChange (replace) to the end of the word.
sDelete character under cursor and substitute text.
SDelete line and substitute text (same as cc).
xpTranspose two letters (delete and paste).
uUndo.
UUndo all changes on line.
Ctrl + rRedo.
.Repeat last command.

Marking Text (Visual Mode)

Visual Mode commands help you select text for further actions like copying or deleting.

CommandDescription
vStart Visual Mode, mark lines, then do a command (like y-yank).
VStart linewise Visual Mode.
oMove to other end of marked area.
OMove to other corner of block.
Ctrl + vStart Visual Block Mode.
awMark a word.
abA block with ()
aBA block with
atA block with <>
ibInner block with ()
iBInner block with
itInner block with <>
EscExit Visual Mode.

Visual Commands

These commands operate on text selected in Visual Mode, such as shifting or copying.

CommandDescription
>Shift text right.
<Shift text left.
yYank (copy) marked text.
dDelete marked text.
~Switch case.
uChange marked text to lowercase.
UChange marked text to uppercase.

Registers

Registers store text for later use, enhancing copy-paste efficiency.

CommandDescription
:reg[isters]List all registers.
”xyYank text into register x.
”xpPaste text from register x.
”xPPaste before cursor.
”+yYank into system clipboard.

Marks and Positions

Marks and positions let you bookmark and jump to specific locations in a file.

CommandDescription
:marksList all marks.
maSet current position for mark a.
aJump to mark a.
y’aYank to mark a.
0Jump 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 + oJump to previous position.
Ctrl + iJump to next position.
:changesList all changes.
g,Jump to previous change.
g;Jump to next change.
Ctrl + jJump to next change.

Macros

Macros record and replay sequences of commands to automate repetitive tasks.

CommandDescription
qaStart recording macro a.
qStop recording macro.
@aExecute macro a.
@@Execute last macro.

Cut and Paste

Cut and paste commands handle copying, deleting, and moving text.

CommandDescription
yyYank (copy) a line.
2yyYank (copy) 2 lines.
ywYank (copy) the characters of the word from the cursor position to the start of the next word.
y$Yank (copy) to end of line.
yiwYank (copy) inner word.
yawYank (copy) a word including the surrounding white space.
pPut (paste) the clipboard after cursor.
PPut (paste) before cursor.
gpPut (paste) after cursor.
gPPut (paste) before cursor.
ddDelete (cut) a line.
2ddDelete (cut) 2 lines.
dwDelete (cut) the characters of the word from the cursor position to the start of the next word.
DDelete (cut) to the end of the line.
d$Delete (cut) to the end of the line.
diwDelete (cut) inner word.
dawDelete (cut) a word including the surrounding white space.
xDelete (cut) character.

Indent Text

Indentation commands adjust text alignment, crucial for code formatting.

CommandDescription
>>Shift text right.
<<Shift text left.
>%Shift selected lines right.
<%Shift selected lines left.
>ibShift inner block right.
<ibShift inner block left.
>atShift block right.
<atShift block left.
3==Auto indent 3 lines.
=%Auto indent selected lines.
=iBAuto indent inner block.
gg=GAuto indent whole file.
]pPaste and indent.

Exiting

Exiting commands manage how you save and close files in VIM.

CommandDescription
:wWrite (save) the file, but don’t exit.
:w !sudo tee %Write out the current file using sudo.
:wqWrite (save) and quit.
:xWrite (save) and quit.
ZZWrite (save) and quit.
:qQuit (fails if there are unsaved changes).
:q!Quit and throw away unsaved changes.
ZQQuit and throw away unsaved changes.
:wqaWrite (save) and quit on all tabs.
:qaQuit on all tabs.

Search and Replace

Search and replace commands help you find and modify text efficiently.

CommandDescription
/patternSearch for pattern.
?patternSearch backward for pattern.
\vpatternSearch for lines not containing pattern.
nRepeat search in same direction.
NRepeat search in opposite direction.
:%s/old/new/gReplace all old with new throughout file.
:%s/old/new/gcReplace all old with new throughout file with confirmations.
:s/old/new/gReplace all old with new in the current line.
:s/old/new/gcReplace 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.

CommandDescription
: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.

CommandDescription
:tabeOpen a new tab.
:tabnew {page.words.file}Open a new tab.
gtMove to the next tab.
:tabn[ext]Move to the next tab.
gTMove to the previous tab.
:tabp[revious]Move to the previous tab.
#gtMove 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.
:tabsList all tabs.
:tabdoRun 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.

CommandDescription
: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] fileGo to buffer {file}.
:lsList all open buffers.
:buffersList 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 sballOpen a file in a new buffer and split window.
Ctrl + wsSplit window.
Ctrl + wwMove to the next window.
Ctrl + wqClose the current window.
Ctrl + wvSplit window vertically.
Ctrl + whMove to the window on the left.
Ctrl + wlMove to the window on the right.
Ctrl + wjMove to the window below.
Ctrl + wkMove to the window above.
Ctrl + w=Make all windows the same height.
Ctrl + wHMove the current window to the far left.
Ctrl + wLMove the current window to the far right.
Ctrl + wJMove the current window to the bottom.
Ctrl + wKMove 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.

Related Posts

Check out some of our other posts

Git SSH Keys for GitHub, GitLab, and Bitbucket on Linux

Git SSH Keys for GitHub, GitLab, and Bitbucket on Linux

Introduction Git connects to remotes by default via HTTPS, which requires you to enter your login and password every time you run a command like Git pull or git push, using the SSH protocol. You m

read more
Dotfiles: A Git-Based Strategy for Configuration Management

Dotfiles: A Git-Based Strategy for Configuration Management

Keeping your dotfiles — those hidden configuration files that dot your home directory — both safe and easily accessible across various machines can seem daunting. However, with the strategy outlined b

read more
Introduction to Linux CLI

Introduction to Linux CLI

Introduction The Linux operating system family is a group of free and open-source Unix systems. They consist of Red Hat, Arch Linux, Ubuntu, Debian, openSUSE, and Fedora. You must utilize a shell

read more
How To Connect A Two EC2 Instances Database and Files Transfer Using AWS CLI

How To Connect A Two EC2 Instances Database and Files Transfer Using AWS CLI

Introduction In this post, I will show you how to connect a two EC2 instances database and files transfer using AWS CLI. I will use AWS CLI to create a VPC, EC2 instances, EBS, EFS, and security g

read more
How to Install Jenkins on AWS EC2 Instance

How to Install Jenkins on AWS EC2 Instance

Introduction In this post, I will show you how to Create an EC2 Instance on AWS and install Jenkins on it. PrerequisitesAWS CLI installed and configured IAM user with the following permi

read more
How to CI/CD AWS With Github using Jenkins

How to CI/CD AWS With Github using Jenkins

Introduction In previous posts, I have shown you how to setup Jenkins on AWS EC2 instance. You can check the post here. In this post, I will show

read more