vim

Vim Cheat Sheet

Vim is a free, open-source text editor that comes installed by default with most operating systems. This tool is one of the most popular and powerful text editors that has been the preferred choice of many programmers. Learning to use Vim effectively can be a challenge, as Vim contains a lot of commands, and memorizing each and every command could take time. Below, we have compiled some of the most used Vim commands into a cheat sheet that will be helpful for using this text editor.The following table contains the most common and useful Vim commands to know, along with the functionalities of each command.

Command

Function

File Management

:w Write file to disk
Ctrl+g Show file information
ga Show character information
:q Quit vi without saving file
:wq Write file to disk and quit vi
:q! Ignore warning and discard changes
:w filename Save the file as filename
:r filename Read and insert filename

Navigation

h Move cursor left
l Move cursor right
j Move cursor down one line
2j Move cursor down 2 lines
k Move cursor up one line
2k Move cursor up 2 lines
w Jump to start of next word (punctuation considered words)
W Jump to start of next word (spaces separate words)
e Move to end of next word (punctuation considered words)
E Move to end of next word (no punctuation)
b Move backwards to start of current word
B Move backwards to start of current word (no punctuation)
( Move a sentence back
) Move a sentence forward
{ Move a paragraph back
} Move a paragraph forward
$ Move to end of line
^ Move to first non-blank character in line
0 Move to start of line
G Move to last line of document
3G Move to line 3
gg Move to first line of document
:n Go to line n in document
Ctrl+d Scroll down half a screen
Ctrl+u Scroll up half a screen
Ctrl+f Page forward
Ctrl+b Page backward

Switching Modes

i Enter Insert mode
v Enter Visual mode
Esc Return to normal mode from Insert and Visual mode

Insert/Append Text

i Insert text before current cursor location
I Insert text at beginning of line
a Append text after current cursor location
A Append text at end of current line
o Create new line for text below current cursor location
O Create new line for text above current cursor location
ea Insert (append) at end of current word

Search and Replace

/pattern Search forward
?pattern Search backward
* Search forward for word under cursor
# Search backward for word under cursor
r Replace single character at cursor position
R Replace characters starting at cursor position
cc Replace entire line
:%s/search/replace/g Replace every occurrence of search with replace.
:%s/search/replace/gc Replace every occurrence of search with replace with confirmation

Cut, Copy and Paste

x Cut character under current cursor location
X Cut character before current cursor location
dw Cut from current cursor location to next word
D Cut from current cursor location to end of current line
dd Cut entire current line
Y Copy selected text
yy Copy entire current line
p Paste text after cursor
P Paste text before cursor

Undo and Redo

u Undo last change
Ctrl+r Redo changes that were undone

Working with Multiple Files

 

:e filename Edit file in a new buffer
:ls List available buffers
:bn Move to next buffer
:bp Move to previous buffer
:bfirst Move to first buffer
:blast Move to last buffer
Ctrl+ws Split windows
Ctrl+ww Switch windows
Ctrl+wq Quit window
Ctrl+wv Split windows vertically
:tabnew Create new tab
gt, :tabn, Move to next tab
gT, :tabp Move to previous tab
:tabfirst Move to first tab
:tablast Move to final tab
#gt Move to tab number #

Set Line Number

:set number Show line numbers
:set nonumber Do not show line numbers

Visual Mode Commands

y Copy selected text
d Delete selected text
c Delete selected text and enter Insert mode
> Increase indent
< Decrease indent
= Fix indentation of current line
~ Change case

The cheat sheet above should fill you in on the most used Vim commands. Vim can be hard to use at first but mastering a few commands can help you to gain more productivity in text editing and coding.

About the author

Karim Buzdar

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn.