VIM cheat sheet

Posted by on 5 May 2012

I am closing down my old blog on socketwiz.com and I didn't have much content over there and this was and article I felt like preserving.

Since I have been using Vim almost exclusively lately I made myself a cheat sheet to help me remember some stuff. This is certainly not an exhaustive list, just some things that I don’t know how to do off the top of my head that I wrote down and ended up getting a little carried away. Just thought I would share:

Motions

motiondescription
hCount characters left
lCount characters right
^To the first character of the line.
$To the last character of the line.
fTo the counth character occurrence to the right. F to the counth character occurrence to the left.
tTo 1 character just before the counth character occurrence to the right.
TTo 1 character just before the counth character occurrence to the left.
wCount words forward.
WCount words forward (different definition for what a word is, includes special characters and such.)
eCount forward to the end of word.
bCount words backward.
triggereffect
c{motion}Change
d{motion}Delete
y{motion}Yank into register
g~{motion}Toggle case
gu{motion}Make lowercase
gU{motion}Make uppercase
>{motion}Shift right
<g{motion}Shift left
={motion}Auto-indent

Buffer management

commanddescription
<C-^> or Switch to the buffer you just left.

Split windows

commanddescription
s or :sp[lit] Split file horizontally.
v or :vsp[lit] Split file vertically.
wCycle between open windows.
hFocus the window to the left.
jFocus the window below.
kFocus the window above.
lFocus the window to the right.
:cl[ose] cClose the active window.
:on[ly] oKeep only the active window, closing all others.

Spelling

commanddescription
:setlocal spellEnable spell checker
]sJump to next spelling error.
[sJump to previous spelling error.
z=Suggest corrections for current word.
zgAdd the current word to spell file.
zwRemove the current word from spell file.

Code folding

commanddescription
zf{motion}Folds code when "foldmethod" set to manual or "marker".
zaToggle fold at cursor.
zoOpens fold at cursor.
zcClose fold at cursor.
zROpen all.
zMClose all.

Search and replace

commanddescription
:args **/*.txtMulti-file Step 1) populate the argument list with the files you want to search.
:argdo %s/search/replace/gcMult-file Step 2) replace all occurrences of search with replace but prompt before doing so.
*Search for the word under the cursor.

Jumps

commanddescription
:jumpsDisplay the jump list.
Jump backwards through the jump list.
Jump forwards through the jump list.
:changesDisplay the change list.
g;Jump backwards through the changes list.
g,Jump forwards through the changes list.
gfjump to the file name under the cursor.

Marks

commanddescription
:marksDisplay the marks list.
mSet a file bookmark.
mSet a buffer bookmark.
'Jump to the mark.
''Jump to the line in the current buffer where jumped from.
:delmarks Delete specified mark.
:delmarks a-dDelete marks a through d
:delmarks a,b,x,yDelete only marks a,b,x and y.
:delmarks!Delete all lower case marks.

Registers

commanddescription
:registersDisplay the register list.
"{motion}Overwrite or use contents of register.
"{motion}Append or use contents of register.
0Populated with last yanked text.

Macros

commanddescription
q{register}Start recording and store it in the specified register.
qStop recording.
{count}@{register}Execute specified macro count times.

Sessions

commanddescription
:mksession /path/to/session.vimA Session keeps the Views for all windows, plus the global settings.
:source /path/to/session.vimOpen a session from within Vim.
vim -S /path/to/session.vimStart Vim with a session.

Quickfix window

commanddescription
:copenOpen the quickfix window.
:cclClose it.
:cwOpen it if there are "errors", close it otherwise (some people prefer this).
:cnGo to the next error in the window.
:cnfGo to the first error in the next file.
:cc{num}Go to the error by number.
keymove to
%End of construct
[[Backwards to the beginning of the current function.
][Forwards to the beginning of the current function.
]}Beginning of the current block.
[{End of the current block.
}[Beginning of the current comment block.
}]End of the current comment block.
gdFirst usage of the current variable name. (Mnemonic: go to definition).
gDGo to the first global usage of the current variable name.
ggBeginning of file.
GEnd of file.

Help

keymove to
~<C-]>~follow link
~~jump back

Addon References

ctrlp.vim https://github.com/kien/ctrlp.vim

commanddescription
CtrlPInvoke ctrl-p in file mode.
F5Purge the cache for the current directory.
Cycle between modes.
Switch to filename instead of full path.
Switch to regex mode.
Navigate the result list.
..To go up the directory tree.
:Excute command against the file.
:Jump to line number.

Tcomment https://github.com/tomtom/tcomment_vim

commanddescription
gc{motion}Toggle comments (for small comments within one line the &filetype_inline style will be used, if defined).
gccToggle comment for the current line.
C{motion}Comment region.
CcComment the current line.
:TComment.
b:TCommentBlock.

vim-fugitive https://github.com/tpope/vim-fugitive

commanddescription
Gstatusgit status (- to add/reset changes)
Gcommitgit commit
Gmovegit move
Gremovegit rm
Ggrepgit grep
Gloggit log
Gbrowseopen current file on Github
Git run arbitrary git command

vim-surround https://github.com/tpope/vim-surround

commanddescription
csChange old delimeter to new.
dsRemove delimeters.
ysiwWrap current word.
SWhile in visual mode.

gitgutter https://github.com/airblade/vim-gitgutter

commanddescription
[hPrevious hunk
]hNext hunk

Miscellaneous

commanddescription
vEnable characterwise Visual mode.
Enable linewise Visual mode.
Enable blockwise Visual mode.
gvReselect the last visual selection.
oGo to other end of highlighted text.
"+pPaste from system clipboard.
:0,$dDelete every line in a file.
Increment number.
Decrement number.
vitSelect contents inside of an HTML tag.
:mapList all currently define mappings.
:verbose mapDisplay where mapping was defined.
g/{pattern}/dDelete every line matching pattern.
v/{pattern}/dDelete every line not matching pattern.
:w !sudo tee %Save a file with sudo that you opened with proper permissions.
bufdo {command}Execute specified command on every buffer.
{trigger}i{container}Inside some container like brackets, tag, or parenthesis for example.
:eRefresh or reload buffer

Merge Conflicts

(requires the fugitive plugin)

:Gdiff
Target      |       Working     |       Merge
//2                                     //3

[c and ]c to navigate between conflicts

:diffget //2 or //3
:diffupdate
:only
:wq
$ git add .
$ git commit -m 'message'

Build from source

$ cd ~
$ hg clone https://code.google.com/p/vim/
$ hg tags | grep less # find out what latest stable build is
$ hg checkout <latest_stable>
$ cd vim
$ ./configure --with-features=huge \
            --enable-rubyinterp \
            --enable-pythoninterp \
            --enable-gui=gtk2 --enable-cscope --prefix=/usr
$ make VIMRUNTIMEDIR=/opt/local/share/vim/vim73
$ make install

Tags: #dtrace #git

Categories: #technology