Skip to content

Instantly share code, notes, and snippets.

@kennyng
Last active January 7, 2016 05:42
Show Gist options
  • Save kennyng/3761832f775f2c128992 to your computer and use it in GitHub Desktop.
Save kennyng/3761832f775f2c128992 to your computer and use it in GitHub Desktop.
Vim Commands Reference

Vim Commands Reference


Table of Contents


NAVIGATION

  • By words:
    • w (next word, by punctuation); W (next word, by spaces)
    • b (back word, by punctuation); B (back word, by spaces)
    • e (end word, by punctuation); E (back word, by spaces)
  • By line:
    • 0 (start of line)
    • ^ (first non-whitespace)
    • $ (end of line)
    • g_ (last non-whitespace)
    • home/end (for start of line/end of line)
  • By paragraph:
    • { (previous blank line)
    • } (next blank line)
  • By file:
    • gg (start of file); G (end of file)
    • 123G (go to specific line number '123')
    • :123 (go to specific line number '123')
  • By marker:
    • mx (set mark x)
    • 'x (go to mark x)
    • '. (go to position of last edit)
    • '' (go back to last point before jump)
  • Scrolling:
    • H (go to first line of current screen)
    • M (go to middle line of current screen)
    • L (go to last line of current screen)
    • Ctrl-f (forward full screen); Ctrl-b (backward full screen)
    • Ctrl-d (down half screen); Ctrl-u (up half screen)
    • Ctrl-e (scroll one line up); Ctrl-y (scroll one line down)
    • zz (center cursor line)
    • pageup/pagedown (for full screen scroll)

EDITING

  • Undo/Redo:
    • u (undo); Ctrl-r (redo)
    • . (repeat last editing command)
    • :earlier <#m or #s> (return file to a point <#m or #s> back)
    • :later <#m or #s> (advance file to a point <#m or #s> ahead)
  • Inserting:
    • i (insert text at cursor); I (insert text at start of line)
    • a (append text after cursor); A (append text after end of line)
    • o (open new line below); O (open new line above)
    • Ctrl-t (indent current line forwards); Ctrl-d (indent current line backwards)
  • Changing:
    • r (replace single character); R (replace multiple characters)
    • s (change single character)
    • cw (change word); C (change to end of line); cc (change whole line)
    • c (changes text in the direction of motion)
    • ci (change inside parentheses)
  • Deleting:
    • x (delete char)
    • dw (delete word)
    • d (deleted in the direction of the motion)
  • Cut & Paste:
    • yy (copy line into paste buffer); dd (cut line into paste buffer)
    • p (paste buffer below cursor line); P (paste buffer above cursor line)
    • xp (swap two characters; x to delete, then p to put back)
    • (custom mapping to enter paste mode)
  • Visual Blocks:
    • v (visual block stream); V (visual block line); Ctrl-V (visual block column)
      • most motion commands extend the block to the new cursor position
      • o (moves cursor to the end of the block)
      • Shift-i (edit blocks in Visual mode; switch to Normal mode to apply [useful for commenting])
    • d or x (cut block into paste buffer)
    • y (copy block into paste buffer)
    • > (indent block); < (unindent block)
    • gv (re-select last visual block)
  • Code Folding:
    • za [zA] (toggle open/close fold at cursor) [toggle all fold levels]
    • zf (create fold at cursor)
    • zd (delete fold at cursor)
    • zr [zR] (open all folds at one more level) [open all folds at all levels]
    • zm [zM] (closes all folds at one more level) [closes all folds at all levels]

SEARCH & REPLACE (SUBSTITUTE)

  • Searching:
    • /[pattern] (search for pattern forwards); ?[pattern] (search for pattern backwards)
    • * (search forward for word under cursor); # (search backward for word under cursor)
    • n (next match in same direction); N (next match in opposite direction)
    • f[x] (forward to next character x); F[x] (backward to previous character x)
    • ; (move again to same character in same direction); , (move again to same character in opposite direction)
  • Substitute:
    • :range s*[ubstitute]*/pattern/string/cgiI
    • search range: . (current line); % (every line in file); number,number (absolute range of line numbers)
    • c (confirm each substitution)
    • g (replace all occurrences; without g, only replace first occurrence)
    • i (ignore case for pattern); I (do not ignore case for pattern)
    • Example: :%s/foo/bar/g (replace all occurrences of foo with bar)
  • Regular Expressions:
    • Metacharacters
      • . (any character except newline)
      • \s (whitespace character); \S (non-whitespace character)
      • \d (digit); \D (non-digit)
      • \w (word character, [A-Za-z0-9_]); \W (non-word character)
      • \a (alphabet character); \A (non-alphabet character)
    • Quantifiers
      • * (matches 0+ of preceding); .* (matches everything)
      • + (matches 1+ of preceding); = (matches 0 or 1 of preceding)
      • {n,m} (matches from n to m of preceding)
      • {n} (matches exactly n times of preceding)
    • Grouping, Backrefrences, and Alternations
      • &, \0 (the whole matched pattern)
      • \1 (matched pattern in the first pair of () ); \2 (matched in second pair); ... \9 (matched in ninth pair)
      • ~ (previous substitute string)
      • l [L] (next character made lowercase) [all following characters]
      • u [U] (next character made uppercase) [all following characters]
      • \e (end of \U or \L)
      • | (combine several expressions for any matches, OR)

FILE MANAGEMENT

  • :w (write/save file to disk)
  • :w name (save file to disk as name)
  • ZZ or :x (save file to disk and quit)
  • n (edit a new file); n! (edit new file without saving current changes)
  • :q (quit editing file); :q! (quit editing without saving changes)
  • :e (edit same file again, if changed outside vim)
  • :e filename (edit the file filename)
  • :e . (directory explorer)

WINDOWS MANAGEMENT

  • :TT (custom mapping to open files in tabs)
  • Ctrl-w (used as prefix for following commands)
    • n (new window)
    • r (swap windows)
    • _ (maximize current window); = (make all windows equal size)
    • + (increase window size); - (decrease window size)
    • <direction> (go to window in that direction)
  • Ctrl-w Ctrl-w (cycle through windows)
  • :split filename (horizontal split and load another file)
  • :vsplit filename (vertical split and load another file)
  • :sview filename (same as split, but read-only)
  • :hide (close current window)
  • :only (keep only this current window open)

BUFFER MANAGEMENT

  • :b# (toggle previous buffer)
  • :bn (go to buffer specified by n)
  • :ls or :buffers (list all buffers)

REGISTER MANAGEMENT

  • :reg (list all named registers and show their contents)
  • "ky (copy selected contents to register k)
    • use * as register for copying to system clipboard
  • "kp (paste the contents of register k)
  • qk (record edits into register k; q again to stop recording)
  • @k (execute recorded edits); @@ (repeat last recorded edit execution)
  • "kd (replace register k with what cursor is on)

SOURCE NAVIGATION

  • % (jump to matching parenethesis/bracket/brace)
  • gd (go to definition of local symbol under cursor); Ctrl-O (return to previous position)
  • Ctrl-] (go to definition of global symbol, requires tags file); Ctrl-T (return to previous position)
  • Ctrl-n, Ctrl-p (in insert mode, automatic word completion)

MISCELLANEOUS

  • :help command (help with any command)
  • :set (shows variables different from defaults); :set all (shows all vars)
  • set foo? (shows the value of foo); set foo& (reset foo to default)
  • modelines: By default, first and last five lines of file are read by vim for variable settings.
  • Nearly all commands can be preceded by a number for a repeat count.
  • Select Python code snippet in v-mode, then :!python to execute and replace selection with output.
  • :Diffbuf (custom command to show diff between current buffer and original/saved version)

Vim-R

  • ,rf (start R in split window after 'tmux new-session -s r-dev')
  • ,rq (close R with no save)
  • ,l (send current line)
  • ,fd (send function on current line and the body below)
  • ,se (send current selection and echo)
  • ,pe (send current paragraph and echo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment