Vim Tricks

As time went by, I learned a lot of tricks to make my VIM experience faster, easier and more efficient. Obviously, some of these tricks are very basic, and rightfully so. Vim’s strength comes from its ability to combine simple commands and turn them into powerful tools to save you time and effort.

The basics

To learn the basics, I strongly suggest trying out vimtutor. Alternatively, VIM adventures is a game that will allow you to get comfortable with VIM while playing a game.

For simplicity’s sake, Go to in this context corresponds to moves your cursor to

Shortcut Description
0 Go to the start of the current line
^ Go to the first non-whitespace character of the current line
$ Go to the end of the current line
G Go to the end of the file
gg Go to the start of the file
M Go to the line at the middle of your screen
L Go to the last line on your screen
w Go to the beginning of the next word
e Go to the next last character a word
E Go to the next last character a word, including punctuation
b Go to the beginning of the previous word
B Go to the beginning of the previous word, ignoring punctuation
) Go to the next sentence
( Go to the previous sentence
} Go to the next paragraph
{ Go to the previous paragraph
y Yank/Copy
Y or yy Copy current line
p Paste yanked lines/characters on the line above¹
P Paste yanked lines/characters on the line below²
d Delete
dd Delete current line
dw Delete a word
D Delete from current position to end of the line
== Automatically indent
~ Change the case of the current letter (e.g. e becomes E)
% Move to the matching parenthesis, brace, or bracket
v Select character
x Delete character
o Add blank line after current line
O Add blank line before current line
>> Add an indentation
<< Remove an indentation

¹If the yanked lines contain a newline character. Otherwise, it will be pasted before the cursor.

²If the yanked lines contain a newline character. Otherwise, it will be pasted after the cursor.

Undo and Redo

Shortcut Description
u Undo
<CTRL> + r Redo

Searching

Shortcut Description
/ Search forward…
? Search backward…
/function Search for the next occurrence of function. Press enter to perform operation.
n Navigate to the next result
N Navigate to the previous result
* Search for word under the cursor (brings you to the next occurrence)

Command mode (:)

Shortcut Description
:x or ZZ Write and quit
:w Save
:q Quit
:q! Force quit
:tabnew <FILENAME> Open file in a new tab
:vsp <FILENAME> Vertically split current tab with a file
:sp <FILENAME> Horizontally split current tab with a file
:%s/potato/tomato/g Globally replace all potato with tomato
:%s/potato/tomato/gc Globally replace all potato with tomato, but ask confirmation for each replacement

Combining shortcuts

Shortcut Description
ggVG Select current file
y4jP Copy 4 lines starting from the current line toward the bottom and paste them under
5w Go to the beginning of the 5th word from your current position
50G Go to line 50
ci( Change inside (: Erase the text between ( and )
ci{ Change inside { (curly brackets) : Erase the text between { and }
ci" Change inside : Erase the text between " and "
cw Change word: Erase the word your cursor is on starting from its current position (e.g. if cursor is at beginning, the whole word will be erased) and go to insert mode

y4jP