VIM CHEAT SHEET
Sooner or later you will need it
Introduction
For a long time, I ran away from Vim because it seemed very tricky and because there is a lot of simpler editors out there to work with. However, as it comes installed in most Linux distributions, I made this cheat sheet to help me understand how things work. I have discovered that is possible to be very productive with Vim 😃.
I’ll try to go straight to the point. This cheat sheet is not intended to be a complete guide but it helps to get started.
Vim has different operation modes, and I will not cover all of them in this cheat sheet. I’ll stick with the basic functionality needed to open, edit and save a file.
Let’s begin! To open a file with Vim just type vim filename
.ESC
= enter “command mode”
Move the cursor
You can use the arrow keys to move the cursor around. There is also special keys to do this:
h
= Move one character leftj
= Move one row downk
= Move one row upl
= Move one character right
Edition Mode
The following keys have to be typed in “Command Mode”.
i
= Insert text in the cursor positionI
= Insert text at the beginning of the lineo
= Insert text in the next lineO
= Insert text in the previous linea
= Insert a character after the currentA
= Insert text at the end of the liner
= Replace the character at the current cursor positionR
= Enter replace mode to replace characters from the current cursor positionu
= Undo the last actionCTRL + r
= redo
Saving & Exiting
The following keys have to be typed in “Command Mode”.
:w
= Save:q
= Exit:q!
= Force exit (exit without saving):qa
= Exit from all opened files:wq
= Save and exit:x
= Save and exitZZ
= Save and exitZQ
= Force exit (exit without saving)
Copy, paste & cut
The following keys have to be typed in “Command Mode”.
yy
= Copy linep
= Paste content to the below lineP
= Paste content to the above lineyNy
= Copy N linescw
= Cut the word starting from the current cursor positiondd
= Cut or delete a lineD
= Delete the line starting from the current cursor positiondG
= Delete the lines starting from the current cursor position to the end of the filedGG
= Delete the lines starting from the current cursor position to the beginning of the filedw
= Delete the word starting from the current cursor positiondNd
= Cut or delete N linesx
= Delete a character at the current cursor position (similar to “D” key behaviour)X
= Delete a character before the current cursor position (similar to “backspace” key behaviour)yw
= Copy the word starting from the current cursor position
Visual Mode
The following keys have to be typed in “Command Mode”.
v
= Visual mode that allows selecting a text fragmentV
= Visual mode that allows selecting an entire lineCTRL+v
= Visual block that allows selecting a block of text
Navigation
The following keys have to be typed in “Command Mode”.
/pattern
= Search forward for a patter?pattern
= Search backward for a patternn
= Pattern forward searchN
= Pattern backward searchgg
= Goes to the first line of the fileG
= Goes to the last line of the fileH
= Goes to the top of the current screenM
= Goes to the middle of the current screenL
= Goes to the end of the current screen
Commands
The following keys have to be typed in “Command Mode”.
:set hlsearch
= Enable search highlight:set number
= Show line numbers:set tabstop=N
= Set the size of TAB to N:set expandtab
= Convert TAB in spaces:set bg=dark/light
= Change the color scheme:set ignorecase
= Makes the search case insensitive:syntax on/off
= Enable/disable syntax highlighting:LNs/<tobereplaced>/<replacer>/g
= Replaces(s) all(g) <tobereplaced> with <replacer> in the line LN:%s/<tobereplaced>/<replacer>/g
= Replaces(s) all(g) <tobereplaced> with <replacer> in the entire file:e filename
= Opens “filename”:r filename
= Copy the contents of the “filename” to the current file:split filename
= Split screen horizontally to show the current file and “filename”:vsplit filename
= Split screen vertically to show the current file and “filename”:! command
= Runs “command” in shell and show the STDOUT!! command
= Runs “command” in shell and paste the STDOUT in the file