vim cheatsheet
Another vim cheatsheet based on my needs / feelings / findings.
Credits to
Moves on line
hjkl: basic moves ←↓↑→0: go to the first column^: go to the first non-blank character of the line$: go to the end of lineg_: go to the last non-blank character of linefa: go to next occurrence of the letteraon the line.,(resp.;) will find the next (resp. previous) occurrencet,: go to just before the character,FandT: likefandtbut backward3fa→ find the 3rd occurrence ofaon this line
Moves on document
NG: go to line Ngg: shortcut for 1G - go to the start of the fileG: go to last linew: go to the start of the following worde: go to the end of this word.W: go to the start of the following word (space separator)E: go to the end of this word (space separator)%: Go to the corresponding (, {, [.*: go to next occurrence of the word under the cursor#: go to previous occurrence of the word under the cursor
Modification
a: insert after the cursoro: insert a new line after the current oneO: insert a new line before the current onecw: replace from the cursor to the end of the worddd: delete (and copy) the current line
Search / Replace
/pattern: search for pattern:%s/foo/bar/g: find each occurrence of ‘foo’ (in all lines), and replace it with ‘bar’.:s/foo/bar/g: find each occurrence of ‘foo’ (in the current line only), and replace it with ‘bar’.:%s/foo/bar/gc: change each ‘foo’ to ‘bar’, but ask for confirmation first.
Copy/Paste
p: pasteP: paste beforeyy: copy the current line, easier but equivalent toddP:set paste: enable special paste mode to turn off autoindent when you paste code:set nopaste: turn off the paste-mode, so that auto-indenting when you type works correctly again.
Undo/Redo
u: undo<C-r>: redo
Tab/space handling
:set tabstop=2 shiftwidth=2 expandtab: configure and enable expand tab:set retab: replace tab to spaces whenexpandtabis already enabled:set et|retab: replace tab to spaces (first enable optionexpandtabwithet)
Macro
Macros are recorded in registers (a to z).
qa: start recording to register aq: stop recording@a: playback register aqaq: delete recording stored in register a
Load/Save
:e <path/to/file>: open:w: save:saveas <path/to/file>: save to <path/to/file>:x, ZZ or :wq: save and quit (:x only save if necessary):q!: quit without saving, also: :qa! to quit even if there are modified hidden buffers.:bn (resp. :bp): show next (resp. previous) file (buffer)
Combine
One can combine the moves with commands in order to apply an operation from a start point to an end point:
<start position><command><end position>
For example : 0y$ means
0: go to the beginning of this liney: yank from here$: up to the end of this line