vim

Quick reference of VIM commands that I either use often or would like to remember. Will be updated constantly and is of dubious use to anybody else besides me - there are much more complete references on the webternet. I think that creating a personalized cheatsheet of commands is the best way to learn VIM - you can always look a command up if you happen to forget it, but at the same time you wouldn't have to sift through commands that you never use.

  1. vim

Movement

Command Description
gg start of the buffer
G end of the buffer
5G jump to line 5 in the buffer
5gg same as 5G above
<Enter> first non-blank character on next line
- first non-blank char on previous line
zz center line on screen

Marks

Command Description
mc set mark named c
'c jump to line with mark c
`` move to previous location
:marks list all marks

Insert mode

Command Description
Ctrl+Y insert char above the cursor
Ctrl+E insert char below the cursor
Ctrl+A insert text from last insert
Ctrl+R= built-in calculator
Ctrl+T indent
Ctrl+D unindent
Ctrl+N complete a word
Ctrl+E when completion is active, stop it and go back to the originally typed text
Ctrl+Y when completion is active, stop it and accept the currently selected entry

Command mode

Command Description
Ctrl+R + Paste from the X clipboard

Entering & modifying text

Command Description
gi insert mode at the place of last modification
g~w switch case of word under cursor
guw lowercase the word under cursor
gUw uppercase the word under cursor
^Vunnnn enter a unicode codepoint, where nnnn is the codepoint value

Search & Replace

Command Description
fx move to the next occurence of character x
Fx move to the previous occurence of char x
df; delete text from cursor to the first ; to the right
* find previous occurence of a word under cursor
# find next occurence of a word under cursor
:%s/foo/bar/ replace first foo in each line in entire file with bar
:noh turn off search highlighting
:%s/\s+$// delete trailing whitespace
:%s/\n/','/ join lines with comma

Text objects

Command Description
:help text-objects get help on text objects
a< select whole <...> block
i' select text inside single quoted string
i" select text inside double quoted string
at select whole tag

Repeats

Command Description
qq start recording commands into register q
q stop recording
@q execute recording in register q
@@ execute last recording
:reg show registers' contents
:'<,'>norm @q execute a recording on a visual block; you don't actually enter '<'>, VIM does it for you when you press :

Folding

Command Description
zo open fold
zc close closest fold
zR open all folds
zM close all folds
zj move to the start of the next fold
zk move to the end of the previous fold
:set foldmethod=manual | indent | expr | marker | syntax | diff set folding method

Working with windows

Command Description
Ctrl+W _ maximize current window

Effective editing

From Bram Moolenaar's talk 7 Habits For Effective Text Editing 2.0.

Tips:

  1. Use * and :hlsearch to search for variable names. Reduces typing and eliminates chances of typos
  2. Use Ctrl+N completion command for long and tricky function names
  3. Spellchecker also helps to avoid typos. Abbreviations automatically correct frequently mistyped words.

    Command Description
    :iabbrev teh the automatically correct teh to the, case-insensitive
    :syntax keyword WordError teh ?
  4. Navigation

    Command Description
    :!ctags -R . create tags file for sources in current dir
    :tag init go to first occurence of tag init
    :tnext go to next occurence of tag
    :grep "\" */.h search for occurences of in .h files in the current directory recursively and display the file where the first occurence is found
    :cnext go to the next occurence
    :clist list all occurences
    :copen list all occurences in quickfix window

Misc

Command Description
ZZ save & exit
ZQ exit without saving
]p paste before, adjust indent
"wyy copy current line into register w
"wp paste contents of w
ga what is this character under cursor?
:set filetype=php tell Vim to treat the current file as a PHP file (syntax highlighting etc)
:set showcmd show partial commands as you type them
:%retab replace indents with spaces using current tabstop setting
:set list show unprintable characters such as s
:r !xxd % read in a hexdump of the current file
gf go to filename under cursor (make sure path option set correctly)