Left-right and up-down motions
h,j,k,l: left, down, up, right^e: scoll window down (ctrl + e)^y: scroll window up^f: scroll down one page^b: scroll up one pageH: move cursor to the topM: move cursor to the middleL: move cursor to the bottomgg: top of fileG: bottom of filet<char>: till some characterT<char>: till some character (backward)f<char>: find forward (it will include the found character, unlike till)F<char>: find backward
Text objects motions
<n> w: n words forward (n is optional)b: beginning of worde: end of word{: beginning of paragraph}: end of paragraph
Text objects (only in visual mode or after an operator)
s: sentencesp: paragraphst: tags (html/xml only)
Copying and moving text / Inserting text
d{motion}: delete/cut (ddto delete entire line)p: paste (a line that was cut for instance)c: change (replace)D/C: delete/change until end of liney{motion{: yank (yyto copy entire line)>: indent^: go to beginning of line$: go to end of lineI: go to beginning of line and enter insert modeA: go to end of line and enter insert modeo: add a new line below and enter insert modeO: add a new line above and enter insert mode
Examples
General rule: [optional number to repeat]{command}{text object or motion}.
diw: delete in wordcaw: change all word (all will grab surrounding whitespaces)yi(: yank all text inside parenthesesdi[: delete between square bracketsva": visually select all inside doublequotes (including doublequotes)3ddis equal tod2j3J: join the next three lines into 1 lined5}: delete from the current line through the end of the fifth paragraph down from here
Registers
:reg: access the registersi(insert mode) then^rand a register letter to paste from it, instead of"{register}p.m{register}: mark a cursor position in some register`{register}: go back to the marked position'{register}: go to beginning of line where there is the marked positiond`a: delete from here to position marked in register 'a'
Autocomplete (in insert mode)
^x^n: for just this file^x^f: for filenames^x^]: for tags only^x^l: whole lines^nand^p: go back and forth in the suggestion list
(^n is disabled by YouCompleteMe.)
Regex
Interactive find and replace
:%s/foo/bar/gc
% means whole document, not just the current line.
/g means every occurence, not just the first one. /c is the interactive option.
Capturing groups
:%s/abc\(def\)ghi/ko\1ok/g
Will print kodefok.
Non-greedy search
Normally, to search in a non-greedy way, we add a punctuation mark, as in .*? which means any string of characters as short as possible. For instance:
/<.>(.*?)<.>/
Applied to the string abc<p>def<a>ghi<b>jkl would match <p>def<a> and capture def. To make non-greedy searches with Vim, replace *? with \{-}.
Misc
Split the screen in half and display two different parts of a given file
:vsp
:set scrollbind
Then, on the right pane, scroll down or up, further in the file, and do:
:set scrollbind
From that point on, as you scroll on one pane, the other one will follow. To undo:
:set noscrollbind
Other
v: visually select (enters visual mode).: repeat the last command~: invert the case for current char/: search something (c/myvar, delete and insert from here to myvar):earlier 2m: go back to state in history that is 2 minutes old:cl: to list errors:cc#to jump to error number #:cnand:cp: to navigate forward and back:help ^n: to know about what ctrl n does:help c_^n: ctrl n in command mode:help i_^n,:help v_^n: ctrl n in insert mode or visual mode:helpgrep whatever: search in all documentation, then use :cl, :cc, :cp, :cn
Useful links
- Vim Cheat Sheet
- Vimgifs
- Vim Navigation Commands: sequences you have no excuse not to know
- Learn Vimscript the Hard Way
- How to Do 90% of What Plugins Do (With Just Vim)
- Vim anti-patterns
- Building Vim from source
- Use Vim as a Java IDE
- SnipMate
- Auto closing an HTML tag
- What is the best JavaScript (programming language) setup with Vim (text editor)?
- Vimcasts
- Here is why vim uses the hjkl keys as arrow keys
- Your First Vim Plugin
- vim + tmux - OMG!Code
- What is your most productive shortcut with Vim?
- nicknisi/dotfiles
- shawncplus/dotfiles
- Why I love Vim: It’s the lesser-known features that make it so amazing