Now that I’ve started coding a lot more, I’ve been extensively using vim. I made a cheat sheet with an upload of my config and plugins for a few friends, so I thought I might as well share it with everybody.

They include: a commenting plugin, a buffer explorer plugin, a yankring plugin, better prolog/Qt syntax highlighting, easy .h -> .cpp switch plugin, some others, and a lot of shortcuts (like for pasting from other applications). Also the very essential tetris plugin.

Vim Awesomeness

Throughout <C-X> means Ctrl-X

First get the config — just execute this command and it will do it:

cd && mv .vimrc{,_old} && mv .vim{,_old} && wget http://www.doc.ic.ac.uk/~kg109/configs/vim{.tar,rc} && tar xf vim.tar && mv {,.}vimrc

Read on for the cheat sheet…

Navigation:

Go back to normal mode — <Esc> or easier: <C-c> or <C-[>
Move Around — Down (j), Up (k), left (h), right (l)
Move word — w (word forward), b (word backward) ; W (word forward — skip punctuation), B (word backward — skip punctuation)

Page Jump — Page Down (<C-f> — forward), Page Up (<C-b> — backward)
Page half jump — Down (<C-d> — down), Up (<C-u>)

End of Line — $
Start of Line — 0

Start of document — gg
End of Document — G

Jump to last place that you made an edit — g; (g semi-colon — you can also keep pressing it to keep going back)
Jump forward/back paragraph (next line with space) — { or }

Visual Mode:

Enter visual mode — v
Visual line mode — V
Visual block mode — <C-v>

Find-and-replace over some selection — select it in visual mode, then type: :s/oldword/newword/g after you type the first : it will expand to :’<,’> which is fine

Insert Mode:

Insert mode from before the cursor — i
Insert mode from after the cursor — a

Jump to the end of the line and go into insert mode — I (capital i)
Jump to the beginning and go into insert mode — A

Make a new line below and go into insert — o (line above is O)
Auto-complete variables/functions etc — start typing it and then <C-n> or <C-p> next and previous

Deleting:

delete character — x
delete line – dd
delete from cursor to end of line — D
delete several lines — select them in visual mode, and then d or x

delete whole document — ggdG — which does gg (start of document), d (delete) ..G (end of document)
Delete word – dw or dW (word with punctuation)

Delete everything inside {} braces — diB — delete inner brace
Delete everything inside the {} and the {}s — daB — delete a brace

Delete everything inside () brackets — dib — delete inner brackets
– dab — delete a bracket and its contents

Jump to equivalent bracket / brace — % — so if you’re at a { type % and it goes to the relevant }

Clearing

Clearing is just like deleting, but it puts you in insert mode after the deletion. You can also always use it in any case where you use d, so for example:

cw — clear word
cc — clear line
C — clear until the end of the line
ciB — clear inner brace

……… and all the other ones from the Deleting section

Auto-indent

Auto-indent line — ==
Auto-indent whole document — gg=G which goes to start of doc, and indent until the end

Editing files:
Open and edit a new file: from normal mode, type: :e filename.cpp

Find / Replacing

From normal mode:

/something — searches for something. Then ‘n’ searches for the next term forward, and ‘N’ backward
find and replace — :%s/oldword/newword/g
find and replace in all open files —        :bufdo %s/old-word /new-word /ge | update

You don’t have to use the / character by the way — you can use any other character like , or ~ So for example to find-and-replace you could:

:%s~old-word ~new-word~g

Buffers / Windows

Next buffer — <C-j>
Previous Buffer — <C-k>
Close a buffer: :bd — buffer delete

Split windows: :split
Vertical split: :vs — :q to close any of those (won’t close the buffer — :bd to do that)

Cycle through windows: <C-ww>
Switch between .h -> .cpp — <C-a>

Saving / Closing:

Save file: :w
Quit: :q or quit-without-saving —   :q!
Save and quit —   :wq or easier:     :x

Save all open buffers: :wa

Copying / Pasting:

A delete/clear in vim is a ‘Cut’ — so you can always paste it after. Do just ‘copy’ or ‘yank ‘:

yank line – yy
yank word –yw
yank to end of line — Y

…. you get the idea

Cycle through paste history — paste something (p) and then hit <C-p> to go back in history, or <C-n> to go forward. Alternatively, just hit <F10>

Commenting:

Comment out a line — – (literally the dash character)

Comment out “sexy style” : ,cs (comma c s) i.e. it will do a comment like:

/*
* Line commented out
*/

Switch comment style — ,ca — i.e. switch between using // or /* in C++

Repeating Actions:

Vim is uber consistent. So for most of the above, you can do them X amount of times by proceeding the command with X. So for example:

Delete the next five words: 5dw
Yank the next five lines: 5yy

….etc

Repeat last action: . (full stop)

Macros

Think of them as VBA macros — vim will record the key-presses you make, and when you call the macro it will execute them again:

You can record a macro and place it under any character name. For exactly, to place it under d, just type:

qd <perform all mouse movements > then hit q
Now you can call that macro again with: @d
Execute it four times for example with: 4@d

Folding

For large files, you might want to fold/hide away certain functions temporarily.
Fold selection — select it in visual, then zf
To open a fold: zo
Not a fold, but will wrap the line to a certain length — gq

Extra other configs:

Toggle line numbers off / on: <F12>
Look at paste history and paste something from it: <F10> then navigate through and <Enter>

Tetris:

:Tetris