Type something to search...
VIM Cheat Sheet

VIM Cheat Sheet

What Is a VIM?

VIM is a text editor that is available on most Linux distributions. It is a powerful text editor that can be used to edit files from the command line. It is a modal text editor, which means that it has different modes for different tasks. This post is a cheat sheet for VIM.

VIM Modes

VIM has three modes:

  • Normal Mode
  • Insert Mode
  • Command Mode

Normal Mode

In normal mode, you can move around the file, delete text, and perform other tasks. You can enter normal mode by pressing the Esc key.

Insert Mode

In insert mode, you can type text. You can enter insert mode by pressing the i key.

Command Mode

In command mode, you can perform actions on the file. You can enter command mode by pressing the : key.

VIM Commands

Global

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

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

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

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)

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

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

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

CommandDescription
:marksList all marks.
maSet current position for mark a.
“a`Jump to mark a.
y'aYank 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 + 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

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

Cut and paste

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

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

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

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

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

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

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

In this article, we have learned how to use Vim to edit text files. We have learned how to move around the file, how to edit text, how to search and replace text, and how to work with multiple files.

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

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

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

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 perm

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 sho

read more