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,
d5japplies thed(delete) operator to5x thejmotion, 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
Ito insert at the beginning of the first line, and indent it as desired - To unindent, select the whitespace you want to remove, then press
dorx
- To indent, once in visual block mode, press
.– repeat the previous action
- Search/replace:
- In command mode, type
/your search termto search nto go to next search result,Nto go to previous search result- Type
:%s/search term/replacement/gto replace all occurrences ofsearch termin the current file
- In command mode, type
- Macros:
qwto record macro “w”. Do some editing, then pressqto stop recording.@wto replay macro “w”. This will play back all the keystrokes you recorded in the macro.