Vim cheat sheet
Here is a short cheat sheet of vim things I frequently use, meant to accompany my vim video:
- Vim works with motions, counts, and operators. For example,
d5j
applies thed
(delete) operator to5
x thej
motion, deleting 5 lines down. - It’s really helpful to turn on relative line numbering so that you can
quickly see how many lines up/down you want to copy or delete. Add this to
your
~/.vimrc
:set relativenumber set number
- Essential motions:
j
– next linek
– previous linel
– going to the righth
– going to the leftw
– word – go to the next wordb
– back – go to the previous word
- Other helpful motions:
^
– go to the beginning of the line$
– go to the end of the line%
– jump to corresponding parentheses or bracketf
– jump to the first occurrence of a character (e.g.f)
will jump to the nearest closing parentheses, orf,
will jump to the closest comma)F
– jump to the first previous occurrence of a character}
– go to the end of the current paragraph{
– go to the beginning of the current paragraphgg
– go to top of file42gg
– go to line 42G
– go to bottom of filectrl-o
– go to previous location (helpful when you are jumping around multiple files)ctrl-i
– go to next location- Markers
ma
– set marker “a” at the current line'a
– jump to marker “a”
- Operators:
d
– delete/cuty
– copyp
– pastec
– change: delete/cut and go into insert mode- Repeat operator twice to apply to entire line (e.g.
dd
– delete whole line) - Do operator uppercase to apply to the rest of the line (e.g.
D
– delete from the cursor until the end of the line) v
– go into visual modectrl+v
– go into visual block mode (helpful for indenting or commenting or otherwise modifying a block of code)- To indent, once in visual block mode, press
I
to insert at the beginning of the first line, and indent it as desired - To unindent, select the whitespace you want to remove, then press
d
orx
- To indent, once in visual block mode, press
.
– repeat the previous action
- Search/replace:
- In command mode, type
/your search term
to search n
to go to next search result,N
to go to previous search result- Type
:%s/search term/replacement/g
to replace all occurrences ofsearch term
in the current file
- In command mode, type
- Macros:
qw
to record macro “w”. Do some editing, then pressq
to stop recording.@w
to replay macro “w”. This will play back all the keystrokes you recorded in the macro.