Vim is a free and open-source, screen-based text editor program inspired by Vi. It may be difficult for newcomers and even long-term users to remember all the different commands. This one-page cheat sheet will help you get back to work quickly and improve your velocity while working in Vi/Vim:
Vi & Vim Cheat Sheet in PDF format.
This post covers the core essentials Vi & Vim cheat sheet, including brief references of Vi insert mode, edit commands, ex commands, navigation, search and substitute, etc. The cheat sheet is also available in an easy-to-print
pdf and
jpg format. You may also want to check my post on the
Differences Between Vi and Vim.
👉 You have to use the ESC (escape key) to get into the vi command mode. You will need to press the RETURN Key for executing a command starting with the ponctuation character : or / or ?. Use CTRL+c to cancel a command. Use . to repeat your last command and :!cmd to execute a shell command (where cmd is the command to execute).
Insert & Replace
i
insert text before the cursor
a
insert text after the cursor
I
insert text at the beginning of the current line
A
insert text at the end of the current line
o
insert text in a new line below the cursor
O
insert text in a new line above the cursor
r
replace a character at the cursor position
R
replace characters starting at the cursor position
Cursor Motion
h or ←
move cursor left
l or →
move cursor right
k or ↑
move cursor up
j or ↓
move cursor down
G
goto the end of file
nG or :n
goto the line number n
0
move to the beginning of the line
$
move to the end of the line
CTRL+f
move one screen view forward
CTRL+b
move one screen view backward
Ex Commands
:q
quit current open file
:q!
force to quit without saving changes
:w
save file
:wfile
save file as file
:wq!
force to save file then quit
:x,ywfile
write from line x to line y into file
:w »file
append buffer to file
:efile
edit another file
:e!file
edit another file without saving current changes
:rfile
insert file content at the current cursor position
:n
edit next file in vi arguments file list
CTRL+G
display current file name and position
Search & Substitute
/string
search forward for string
?string
search backward for string
n
repeat the last search
:,$s/str1/str2/gc
search and substitute str1 by str2 from the current line to the end of file. A line number can be specified before the comma “,”. The “c” ask for confirmation before each substitution
:%s/str1/str2/g
replace all str1 by str2 in all the file without prompting for confirmation
:%s/str1/str2/
replace str1 by str2 on the first occurrence in each line of the file
Undo, Delete, and Copy
u
undo last change
CTRL+r
redo last undo change
mA
set mark A to current position. Marks can be the letter [a-z] and [A-Z]
y’A
yank (copy) from current line to mark A
d’A
delete from current line to mark A
P
put the buffer content before the cursor
p
put the buffer content after the cursor
x
delete character at cursor position
dW
delete first word after cursor position
d$ or D
delete from cursor position to end of line
dd
delete current line
J
join current line with following line
Going Further
:map for mapping a key in command mode to a group of commands. Example: :map de :1,$d^M will delete all
lines when using the :de command.
:set to define or show your editor options. Example: :set number will make vi display line numbers, and
“:set all” will display all current vi options.
:ab to define a text abbreviation in insert mode. Example: :ab VIM Vi Improved will auto-complete “VIM”
in insert mode for the phrase “Vi Improved”.