Published: 2012-10-02

Vim Cheat Sheet

I love Vim. It is the text editor with which I have found I am most productive. Almost every day I discover some new trick I didn't know the day before. So I'm going to start putting those tricks here, when I remember, for my benefit and the benefit of others. This is my personal Vim cheat sheet.

There are some useful links at the bottom of the page as well. These include tutorials and general reference sites.

CommandDescription
:g/{regex}/{command}Execute {command} on all lines that match {regex}
cwDeletes the word under the cursor and goes into edit mode. This can then be repeated on the next occurrence of the original word with n..
!sortNot strictly a Vim command (the ! calls an external program) but nifty. It uses the Unix sort command to sort the highlighted text.
{count}ggGo to the {count} line of the document. Default is the first line. Example: 25gg would move to the 25th line.
{count}GLike gg except the default is to go to the last line.
J and gJJoin the selected lines together. The first removes indentation and inserts spaces, the second does not. A {count} can also precede the command in lieu of selecting lines.
gqq and gqHard wraps long lines. The first wraps the current line, the second can be given a direction after the command.
=Applies some fancy auto-indentation. Works for various languages dependent on the file type (assuming filetype is active).
:TOhtmlCreates an HTML document from the current file, complete with syntax coloring based on the current color scheme.
z{a|c|o|M|R}Code folding. Respectively, toggle, close, open, close all, open all.
m{name} and `{name}Create and jump to, respectively, a bookmark. If {name} begins with a lower-case letter then it is a local bookmark (single file). If the first letter is upper-case, then it is a global bookmark (can be accessed from any buffer).
:only or CTRL-W oClose all windows but the current one.
CTRL-W r and CTRL-W RRotate windows down/right and up/left, respectively.
CTRL-W =Make all windows approximately the same height and width.
:resize {-|+}{N} or {N}CTRL-W {-|+}Decrease or increase the current window height by {N}. Default is N=1. If the sign is omitted in the first form, then just set the height to {N}.
{N}CTRL-W _Set current window height to {N}. Default is tallest possible.
:vertical resize {-|+}{N} or {N}CTRL-W {<|>}Decrease or increase the current window width by {N}. Default is N=1. If the sign is omitted in the first form, then just set width to {N}.
{N}CTRL-W |Set current window width to {N}. Default is widest possible.
:lwd %:p:hSet the local working directory to the directory of the current file.
c-^Edit alternate (previous) file.
:shRun a shell, Vim will resume when the shell exits (Ctrl-D).
%Move to the closing grouping symbol, parens, brackets, etc. Can be used in visual select mode.
c{MOVEMENT}Delete (cut) the result of the next movement command and enter insert mode.
CTRL-Y / CTRL-EMove viewport down or up respectively.

References