Vim cheat sheet
From wiki.titan2x.com
Contents |
Navigation
C-] jump to subject C-t, C-o jump back
Moving around
f{char} to [count]'th occurence of {char} to the right
t{char} till before [count]'th occurence of {char} to the right
w, b move one word forward, backward
(, ) move sentences backward, forward
{, } move paragraphs backward, forward
H, M, L go to top, middle, bottom of the screen
C-y, C-e scroll up/down
Command execution
:read !{cmd}
execute cmd and insert after the cursor
:{range}!{cmd}
pipe range into command, and replace range with command output
!!{cmd}
pipe current line into command, and replace line with command output
Window manipulation
C-w s, C-w v split window horizontally/vertically
C-w [arrow] switch window in arrow direction
C-w h/j/k/l switch to window in specified direction
C-w H/J/K/L move window in specified direction
{n}C-w +, {n}C-w - increase/decrease window size by n lines
C-w o close all windows except current
C-w c close window
C-w q quit window
Marks
m{a-zA-Z} set mark at cursor position
'{a-z} jump to mark
jump to the position before latest jump
'a - 'z marks valid within one file
'A - 'Z marks valid between files
Range specifiers
. current line $ last line 1 first line % entire file 't position of mark t /pattern the next line where pattern matches ?pattern the previous line where pattern matches
Note: + or - may follow the above, with a number
Registers
["x]{op} select register x for the operator.
for example, deleted text will be inserted into x.
or, copying will be done from register x.
:reg displays the contents of registers
Case swapping
~{motion} swap case
g~~ switch case of current line
gu{motion} make lowercase
guu make current line lowercase
gU{motion} make uppercase
g?{motion} Rot13 encode
Substitution
- Pattern
\| or
\& and
examples:
foobeep\&... matches foo in foobeep
.*Peter\&.*Bob matches in a line containing both Peter and Bob
\* 0 or more
\+ 1 or more
\? 0 or 1
\{n,m} n to m
\< beginning of a word
\a alphabetic character
\l lowercase character
\1 same string as matched by first \(\)
- Sub-replace-special
\0, \& replaced with the whole matched pattern \1 replaced with the matched pattern in the first pair of () \u next character made uppercase \U following characters made uppercase until \E
- s_flags
c confirm each substitution y: yes, l: yes then quit, n: skip, a: yes to all, q: quit g replace all occurences i ignore cases I don't ignore cases
- example
- change "Last, First" to "First Last"
:%s/\([^,]*\), \(.*\)/\2 \1/
Compiling
Generate tag file: ctags -R .
:make :cn go to next error :cfirst go to first error :clast go to last error :clist list errors :tag /function jump to matching function
diffs
The easiest way to configure two files is with vimdiff file1 file2.
[c jump back to the previous start of a change ]c jump ahead to the next start of a change :[range]diffg[et] modify the current buffer to undo difference with another buffer :[range]diffpu[t] modify another buffer to undo difference with the current buffer do same as :diffget without a range or argument dp same as :diffput without a range or argument :diffupdate re-update the differences
Misc
C-a, C-x add/subtract count to the number at or after the cursor
g/{pattern}/{cmd} execute command for lines matching pattern
:set list show tabs as ^I and end of line as $
:bufdo {cmd} execute command in all buffers
:ls show all buffers
gg=G re-indent file
