Vim Tips
Useful Tutorials
- VIM for (PHP) Programmers
- Vim anti-patterns
- http://vim.wikia.com/wiki/Best_Vim_Tips
- http://lucumr.pocoo.org/2010/7/29/sharing-vim-tricks/
Key Mappings
- Modes
i
,I
— insert/insert at beginning of linea
,A
— append/append at end of lineo
,O
— open a new line after/before the currentv
— visual selectionV
— visual line selectionR
— replace mode (replaces more than one char)⌃V
— visual blocks mode
- Undo/Redo and Copy & Paste
u
,⌃r
— undo/redoy
— copyyy
=Y
— copy whole linep
,P
— paste after/before cursorgp
,gP
— paste after/before cursor and put cursor one char after/before pasted textyyp
— duplicate line
- Navigation
⌃i
=⌃I
=⇥
— go backwards (cursor position history)- Warning: if you remap
⇥
, then⌃i
won't work anymore!
- Warning: if you remap
⌃o
=⌃O
— go forwards (cursor position history)''
,``
— go to last cursor position's first/actual char on line⌃u
,⌃d
— 1/2 page up/down⌃b
,⌃f
— page up/downH
,L
— go to top/bottom of screengi
— go to position where you last left insert modegv
— reseselect last selection'.
— goto last edited lineg;
,g,
— goto previous/next edited positiongg
,G
— go to first line/last line (EOF)(
,)
— go to beginning/end of sentence{
,}
— go to beginning/end of paragraph123gg
=123G
— go to line 123n%
— go to the n-th % line%
— find corresponding bracketf<c>
,F<c>
— forward/backward find first letter <c> (goto letter)t<c>
,T<c>
— forward/backward find first letter <c> (goto before letter)⌃G
— show current position in file
- Text Navigation
w
— goto after end of (next) wordW
— goto after end of (next) word (and after fullstops, etc.)e
— goto end of (next) wordb
— goto beginning of previous wordB
— goto beginning of previous word (and before fullstops, etc.)0
,$
— goto absolute start/end of line^
,g_
— goto start/end of line
- Marks
m<c>
— set mark named [a-z]'<c>
— goto line of mark <c>`<c>
— goto exact position of mark <c>
- Text Selection
vaa
,Vaa
— select everything (line mode)vib
— select text inside the parenthesis block (of the cursor)vi"
,vi'
— select text inside double/single quotesvi{
— select text inside paragraph
- Text Manipulation
{visual}p
— replace selected word with clipboard (register "")⌃A
,⌃X
— increment/decrement a number~
— change case of current letterrX
— replace char with Xce
— change from current position to word endC
,c$
— change until end of linecc
— change current lineciw
,caw
— change inner/a wordcib
,cab
— change inner/a blockci{
,ca{
— change inner/a paragraph
- Formating
⌃d
,⌃t
— indent/deindent line inInsert
modegqq
— reformat current linegqap
— reformat current paragraphJ
— join lines=
— auto indent<num>o⎋⎋
,<num>O⎋⎋
— insert <num> lines below/above
- Auto Completion
⌃N
— dictionary completion⌃P
— dictionary completion⌃X⌃O
— omni completion (opens preview window for details)⌃W⌃Z
— close preview window
- Search
/<query>
,?<query>
— forward/backward search for <query>/
,?
— search again for the last searched queryn
,N
— next/previous search result*
,#
— search for word under cursor forward/backwardq/
— show history list of recent searches
- Replacing Text
:s/<old>/<new>/g
— replace <old> with <new>flag g
— global for current line (replace all matches)flag c
— confirm substitution matchesy
,n
,a
,q
— yes/no/all remaining/quit
flag i
— ignore case
:#,#s/<old>/<new>/g
— replace from line # to line #:%s/<old>/<new>/g
— replace in whole file%s//<new>/
— replace last searched query with <new>*
and%s//<new>/
— search for word under cursor and replace it with <new>
- Split Window/Tab Navigation
⌃W h
,⌃W l
— goto left/right split⌃W j
,⌃W k
— goto below/above split⌃W ⌃W
— jump between splits⌃W s
,⌃W v
— split horizontally/vertically⌃W c
— close current window⌃W o
— close all windows except current⌃W |
,⌃W _
— maximise (v)split size
- Folding
zf{motion}
,{Visual}zf
— fold {motion} lines/fold selectionzd
,zD
— delete a fold (recursivly)zE
— eliminate all folds in filezo
,zc
,zO
,zC
— open/close fold (recursivly)za
,zA
— open and close (toggle) a fold (recursivly)zR
,zM
— open/close all foldszj
,zk
— goto start/end of the next/previous fold
- Macros
q<c>
— start recordingq
— stop recording@<c>
— play recorded macro
@@
— replay previously played macro
- Command Line
q:
— show history list of recently executed commands⌃D
— autocomplete filename while doing:e ~/D
or simply:
to see possible commands
- Commands
:x
,ZZ
— save and exit⌃wq
— exit if nothing to save:new
— new window:sp
— split window:vsp
— vertical split window:e <file>
— edit file:r <file>
— read file:!
— execute shell command
- Misc
- Saving a text selection to a file
v
- select some text
:w test.txt
— saves the selected text to file 'test.txt'
]p
— pastes the contents of a buffer (automatically adjusting the indent)zb
,zz
,zt
— scroll screen so that current line is at the bottom/middle/top
- Saving a text selection to a file
Vimdiff
[c
,]c
— goto next/previous difference