Sunday, February 20, 2011

VIM keyboard shotcuts

vim file +54
open file and go to line 54 any : command can be run using + on command line

vim -O file1 file2

open file1 and file2 side by side


:qa close all windows add trailing ! to force

undo/redo
u undo
Ctrl+r redo
. repeat

navigation
gg Goto start of file
G Goto end of file

search/replace
/regexp searches forwards for regexp ? reverses direction
n repeat previous search N reverses direction
* searches forward for word under cursor # reverses direction
:%s/1/2/gc search for regexp 1 and replace with 2 in file c = confirm change
:s/1/2/g search for regexp 1 and replace with 2 in (visual) selection
:%s/
^[\ \t]*\n//g # replace all the empty lines
:5,12s/foo/bar/g # changes each 'foo' to 'bar' for all lines between line 5 and line 12.
:.,$s/foo/bar/g # changes each 'foo' to 'bar' for all lines between the current line (.) and the last line ($).

windows

:e set buffer for current window you can optionally specify a new file or existing buffer number (#3 for e.g.). Note if you specify a directory a file browser is started. E.g. :e . will start the browser in the current directory (which can be changed with the :cd command).
:sp new window above ditto
:vs new window to left ditto
:q close current window
:qa close all windows add trailing ! to force
Ctrl+w {left,right,up,down} move to window
Ctrl+w Ctrl+w toggle window focus
Ctrl+w = autosize windows to new terminal size for e.g.
:ba new window for all buffers ":vert ba" tiles windows vertically