Skip to content

Vim

Syntax

  • Repeatable commands: <number><verb><noun>

Verbs

  • Delete (cut): d

  • Yank (copy): y

  • Change: c

  • Indent: >

  • Visually select: v

  • Visually select lines: V

Modifiers

  • Till before the matching character forward/backward: t or T

  • To the matching character forward/backward: f or F

  • Until the next matching string forward/backward: / or ?

  • Inner block up to the matching text object: i

  • Whole block up to and including the matching text object: a

Nouns

  • Beginning/end of line: 0 or $

  • Beginning/end of file: gg or G

  • To top/middle/last line of current viewport: H or M or L

  • To column number 8: 8|

  • To line number 4: 4G

  • To 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: h or j or k or l

  • To next/previous beginning of a word: w or b

  • To next/previous beginning of a WORD: W or B

  • To next/previous end of a word: e or ge

  • To next/previous end of a WORD: E or gE

  • To next sentence: s

  • To next/previous sentence: ) or (

  • To next paragraph: p

  • To next/previous paragraph: } or {

  • To next tag (HTML/XML): t

  • To next coding block: b

  • To matching brace, bracket, parenthesis or quote (only on matched pairs): %

  • To next/previous match of last search pattern: gn or gN

Modes

Insert mode

  • Enter insert mode before/after cursor: i or a

  • Enter insert mode at beginning/end of line: I or A

  • Create new line before/after current line and enter insert mode: o or O

Visual mode

  • Enter visual mode: v

  • Enter visual block/line mode: Ctrl+v or V

  • Reselect previous area in visual mode: gv

  • Go to the other end of visual block: v_o or v_O

Replace mode

  • Enter replace mode: R

Edition

Case

  • Switch case of word to uppercase/lowercase: gUw or guw

  • Switch 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: :reg

  • Delete character under/before the cursor: x or X

  • Delete/Yank current line: dd or yy

  • Delete/Yank 5 lines: 5dd or 5yy

  • Delete from cursor to beginning of next word/WORD: dw or dW

  • Delete the whole word/WORD (including spaces): daw or daW

  • Yank from cursor to beginning of next word/WORD: yw or yW

  • Yank the whole word/WORD (including spaces): yaw or yaW

  • Delete/Yank up to beginning of current line: d^ or y^

  • Delete/Yank up to end of current line: d$ or y$

  • Delete/Yank from line 2 to line 10: :2,10d or :2,10y

  • Delete line into register [a-zA-Z0-9]: "[a-zA-Z0-9]dd

  • Yank line into register [a-zA-Z0-9]: "[a-zA-Z0-9]yy

  • Delete/Yank line into host clipboard: "+dd or "+yy

  • Replace current line with a blank line: S

  • Put (paste) current register after/before cursor: p or P

  • Put register [a-zA-Z0-9] after cursor: "[a-zA-Z0-9]p

  • Put text from clipboard after/before cursor: "+p or "+P

  • Select recently pasted text: V`]

  • Paste last executed command: ":p

  • Rerun last executed command: @:

Replace

  • Replace character under cursor with #: r#

  • Replace from cursor up to end of word/line: ce or c$

  • Replace 6 words from cursor: c6w

  • Replace inside single/double quotes: ci' or ci"

  • Replace inside backticks: ci`

  • Replace inside matching parenthesis, brackets, braces or chevrons: `` ci) or ci] or ci} or ci> ``

  • Replace inside HTML/XML tag: cit

  • Replace inside block of code: cib

Misc

  • Join next line to current line: J

  • Manually wrap current line to specified textwidth: gqq

  • Increase/decrease number under cursor: Ctrl+A or Ctrl+X

  • Increase numbers in visual selection: g then Ctrl+A

  • Decrease numbers in visual selection: g then Ctrl+X

  • Enter next character literally (e.g. a Tab): Ctrl+V or Ctrl+Q

  • Search forward/backward: / or ?

  • Search word under cursor forward/backward: `` * or # ``

  • Remove highlight after search: :noh<CR>

  • Use perl (for search, substitution, etc): :perldo

  • Run 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: n or N

  • Undo last change: u

  • Redo last undone change: Ctrl+R

  • Restore line: U

Files

  • Open file under cursor: gf

  • Open 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: :wq or :x or ZZ

  • Fast saving: :w!

  • Write file using sudo: :w!!

Views

Movement

  • Jump up/down half a page: Ctrl+U or Ctrl+D

  • Jump forward/backward a full page: Ctrl+F or Ctrl+B

  • Make current line be at top/middle/bottom of current viewport: zt or `` z. or z- ``

Buffers

  • Delete (close) current buffer: :bd

  • Show next/previous buffer: :bn or :bp

  • Jump to buffer: :b<Number>

  • Jump to previous buffer: Ctrl+^

  • List buffers: :buffers

  • Run command in all buffers: :bufdo %s/#000000/black/g

Tabs

  • Show next/previous tab: gt and gT

  • Jump to tab: <Number>gt

  • List tabs: :tabs

  • Go to first tab: :tabfirst

  • Go to last tab: :tablast

  • Close current tab: :tabclose

  • Close all tabs except current one: :tabonly

  • Open file in new tab: :tabe file.txt

  • Run command in all tabs: :tabdo %s/#000000/black/g

Windows

  • Move to window to the left/bottom/top/right: Ctrl+w then h/j/k/l

  • Move to previous window: Ctrl+w then w

  • Run command in all windows: :windo %s/#000000/black/g

Completion

  • Word completion: Ctrl+P

  • Word completion backwards: Ctrl+N

  • Line completion: Ctrl+X Ctrl+L

  • Syntax-aware Omni completion: Ctrl+X Ctrl+O

Folds

  • Toggle folds: :set foldenable!<CR>

  • Enable/disable folding: zi

  • Toggle current fold: za

  • Toggle current fold recursively: zA

  • Open current fold: zo

  • Open current fold recursively: zO

  • Close current fold: zc

  • Close current fold recursively: zC

  • Open all folds: zR

  • Close all folds: zM

  • Open folds to reveal cursor: zv

  • Fold 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: gi

  • Go to older/newer position in change list: g; or g,

  • Go back/forward in position history: Ctrl+o or Ctrl+i

  • List command-line history: :history : or :history c

  • List search string history: :history / or :history s

  • List input line history: :history @ or :history i

  • List all history: :history all

  • List entries 6 to 12 from search history: :history / 6,12

  • List the most recent two entries from history: :history all -2

  • Show command-line window: q:

  • Show search string window: q/ or q?

Macros

  • Start recording macro into register [a-zA-Z0-9]: q[a-zA-Z0-9]

  • End macro recording: q

  • Playback macro from register [a-zA-Z0-9]: @[a-zA-Z0-9]

  • Manually written macro example:

      Ihello world! ^[:s/hello/goodbye/^Mj
  • Cut line into register ‘a’ and then run as macro: "add then @a

Marks

  • Set a mark: m[a-zA-Z]

  • Go to mark: `[a-zA-Z]

Shell

  • Drop to a shell: :sh

  • Run shell command ‘cmd’: :!cmd

  • Insert output of shell command ‘cmd’: :x !cmd

Misc

  • Surround Word in doube quotes: ciW""<Esc>P

  • Surround Word in single quotes: ciW''<Esc>P

  • Edit .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