Syntax
- Repeatable commands:
<number><verb><noun>
Verbs
Delete (cut):
dYank (copy):
yChange:
cIndent:
>Visually select:
vVisually select lines:
V
Modifiers
Till before the matching character forward/backward:
torTTo the matching character forward/backward:
forFUntil the next matching string forward/backward:
/or?Inner block up to the matching text object:
iWhole block up to and including the matching text object:
a
Nouns
Beginning/end of line:
0or$Beginning/end of file:
ggorGTo top/middle/last line of current viewport:
HorMorLTo column number 8:
8|To line number 4:
4GTo first non-blank character on current line:
^To first non-blank character on previous/next line: `` +
or- ``One character to the left/down/up/right:
horjorkorlTo next/previous beginning of a word:
worbTo next/previous beginning of a WORD:
WorBTo next/previous end of a word:
eorgeTo next/previous end of a WORD:
EorgETo next sentence:
sTo next/previous sentence:
)or(To next paragraph:
pTo next/previous paragraph:
}or{To next tag (HTML/XML):
tTo next coding block:
bTo matching brace, bracket, parenthesis or quote (only on matched pairs):
%To next/previous match of last search pattern:
gnorgN
Modes
Insert mode
Enter insert mode before/after cursor:
ioraEnter insert mode at beginning/end of line:
IorACreate new line before/after current line and enter insert mode:
oorO
Visual mode
Enter visual mode:
vEnter visual block/line mode:
Ctrl+vorVReselect previous area in visual mode:
gvGo to the other end of visual block:
v_oorv_O
Replace mode
- Enter replace mode:
R
Edition
Case
Switch case of word to uppercase/lowercase:
gUworguwSwitch case of character under cursor:
~Invert case of word:
g~w
Indenting
Unindent entire line:
<<Indent entire line:
>>Autoindent entire line:
==Autoindent whole file:
g==GG
Registers
List registers:
:regDelete character under/before the cursor:
xorXDelete/Yank current line:
ddoryyDelete/Yank 5 lines:
5ddor5yyDelete from cursor to beginning of next word/WORD:
dwordWDelete the whole word/WORD (including spaces):
dawordaWYank from cursor to beginning of next word/WORD:
yworyWYank the whole word/WORD (including spaces):
yaworyaWDelete/Yank up to beginning of current line:
d^ory^Delete/Yank up to end of current line:
d$ory$Delete/Yank from line 2 to line 10:
:2,10dor:2,10yDelete line into register [a-zA-Z0-9]:
"[a-zA-Z0-9]ddYank line into register [a-zA-Z0-9]:
"[a-zA-Z0-9]yyDelete/Yank line into host clipboard:
"+ddor"+yyReplace current line with a blank line:
SPut (paste) current register after/before cursor:
porPPut register [a-zA-Z0-9] after cursor:
"[a-zA-Z0-9]pPut text from clipboard after/before cursor:
"+por"+PSelect recently pasted text:
V`]Paste last executed command:
":pRerun last executed command:
@:
Replace
Replace character under cursor with #:
r#Replace from cursor up to end of word/line:
ceorc$Replace 6 words from cursor:
c6wReplace inside single/double quotes:
ci'orci"Replace inside backticks:
ci`Replace inside matching parenthesis, brackets, braces or chevrons: `` ci)
orci]orci}orci> ``Replace inside HTML/XML tag:
citReplace inside block of code:
cib
Misc
Join next line to current line:
JManually wrap current line to specified textwidth:
gqqIncrease/decrease number under cursor:
Ctrl+AorCtrl+XIncrease numbers in visual selection:
gthenCtrl+ADecrease numbers in visual selection:
gthenCtrl+XEnter next character literally (e.g. a Tab):
Ctrl+VorCtrl+Q
Search
Search forward/backward:
/or?Search word under cursor forward/backward: `` *
or# ``Remove highlight after search:
:noh<CR>Use perl (for search, substitution, etc):
:perldoRun command [cmd] only on lines matching {regex}:
:g/{regex}/[cmd]Run command [cmd] only on lines not matching {regex}:
:v/{regex}/[cmd]Run command [cmd] from line matching {regex1} to line matching {regex2}:
:g/{regex1}/ .,/{regex2}/[cmd]
Examples
- Search and replace background with background-color:
:%s/background/&-color/g
Repeat and Undo
Repeat last change:
.Repeat last command:
@:Repeat last substitution:
&Repeat last character search:
;Repeat last search forward/backward:
norNUndo last change:
uRedo last undone change:
Ctrl+RRestore line:
U
Files
Open file under cursor:
gfOpen file:
:e <file>Split window and open file:
:sp <file>Split window vertically and open file:
:vs <file>Save current file as “file”:
:w <file>Save current file as “file” and use as new working file:
:sav <file>Save file and exit:
:wqor:xorZZFast saving:
:w!Write file using sudo:
:w!!
Views
Movement
Jump up/down half a page:
Ctrl+UorCtrl+DJump forward/backward a full page:
Ctrl+ForCtrl+BMake current line be at top/middle/bottom of current viewport:
ztor `` z.orz- ``
Buffers
Delete (close) current buffer:
:bdShow next/previous buffer:
:bnor:bpJump to buffer:
:b<Number>Jump to previous buffer:
Ctrl+^List buffers:
:buffersRun command in all buffers:
:bufdo %s/#000000/black/g
Tabs
Show next/previous tab:
gtandgTJump to tab:
<Number>gtList tabs:
:tabsGo to first tab:
:tabfirstGo to last tab:
:tablastClose current tab:
:tabcloseClose all tabs except current one:
:tabonlyOpen file in new tab:
:tabe file.txtRun command in all tabs:
:tabdo %s/#000000/black/g
Windows
Move to window to the left/bottom/top/right:
Ctrl+wthenh/j/k/lMove to previous window:
Ctrl+wthenwRun command in all windows:
:windo %s/#000000/black/g
Completion
Word completion:
Ctrl+PWord completion backwards:
Ctrl+NLine completion:
Ctrl+XCtrl+LSyntax-aware Omni completion:
Ctrl+XCtrl+O
Folds
Toggle folds:
:set foldenable!<CR>Enable/disable folding:
ziToggle current fold:
zaToggle current fold recursively:
zAOpen current fold:
zoOpen current fold recursively:
zOClose current fold:
zcClose current fold recursively:
zCOpen all folds:
zRClose all folds:
zMOpen folds to reveal cursor:
zvFold HTML/XML tags:
Vatzf
History
Go back to last position/line where file was modified:
`.or'.Go back to last position/line where we left insert mode:
`^or'^Insert text where Insert mode was stopped last time:
giGo to older/newer position in change list:
g;org,Go back/forward in position history:
Ctrl+oorCtrl+iList command-line history:
:history :or:history cList search string history:
:history /or:history sList input line history:
:history @or:history iList all history:
:history allList entries 6 to 12 from search history:
:history / 6,12List the most recent two entries from history:
:history all -2Show command-line window:
q:Show search string window:
q/orq?
Macros
Start recording macro into register [a-zA-Z0-9]:
q[a-zA-Z0-9]End macro recording:
qPlayback macro from register [a-zA-Z0-9]:
@[a-zA-Z0-9]Manually written macro example:
Ihello world! ^[:s/hello/goodbye/^MjCut line into register ‘a’ and then run as macro:
"addthen@a
Marks
Set a mark:
m[a-zA-Z]Go to mark:
`[a-zA-Z]
Shell
Drop to a shell:
:shRun shell command ‘cmd’:
:!cmdInsert output of shell command ‘cmd’:
:x !cmd
Misc
Surround Word in doube quotes:
ciW""<Esc>PSurround Word in single quotes:
ciW''<Esc>PEdit
.vimrc::e! $MYVIMRC<CR>Turn on/off hex mode:
:%!xxd<CR>or:%!xxd -r<CR>Toggle invisible characters:
:set list!<CR>Toggle spellchecking:
:set spell!<CR>Chain two or more commands:
:%s/#000000/black/g | %s/#ffffff/white/g