4 min read

Useful Vim commands

Vim commands

Normal mode to insert mode

  • i: insert text just before the cursor.
  • I: insert text at the start of the line.
  • a: append text just after the cursor.
  • A: append text at the end of the line.
  • o: open a new line below.
  • O: open a new line above.
  • s: substitute the current character.
  • S: substitute the current line.
  • r: replace the current character.
  • R: replace continuous characters.

Move cursor around

  • 0: move to the start of the line.
  • ^: move to the first non-blank character of the line.
  • $: move to the end of the line.
  • ctrl-b: move back one screen.
  • ctrl-f: move forward one screen.
  • H: jump as high as possible, i.e. the first line of the window.
  • M: jump to the middle of the window.
  • L: jump to the lowest line of the window.
  • G: jump to the end of the file.
  • 1G or gg: jump to the start of the file.
  • 30G: jump to line 30.
  • w: move to the start of next word. 2w move two words.
  • e: move to the end of next word.
  • b: move backward one word.
  • (: move to previous sentence.
  • ): move to next sentence.
  • {: move to previous paragraph
  • J: move to next paragraph.
  • ctrl-o: jump backward to previous location.
  • ctrl-i: jump forward to next location.
  • ma: mark current position, then move to other places and use 'a (single quote then a) to come back to the start of the marked line, or you can use ‘a' (backtick and a) to jump to exact place. You can also usemband 'b’, i.e. any letters a-z A-Z.
  • %: jump to corresponding item, e.g. from left brace to the right brace

Visual mode

  • v then ap: (a paragraph) choose a paragraph where the curson is on.
  • y then aw: choose the word where the cursor is on.
  • u then a": choose the whole quoted word/sentence where the cursor is within.
  • u then ab: choose a block of text, i.e. within parathese, brakets, etc.
  • ~: switch cases of letters, i.e. upper to lower, lower to upper.
  • V: visual mode with lines.

Edit text

  • d: delete and put text into clipboard.
  • dd: delete the current line.
  • dl or x: delete the current letter where the cursor is.
  • dw: delete a word where the cursor is.
  • d$: delete text after the cursor of the current line.
  • d0: delete text before the cursor of the current line.
  • dh, dj, dk, d2ap, d2w, d31, 24h, d5j, etc. Combine number with options.
  • y: yank or copy text
  • yy: yank the current line.
  • yap: yank the current paragraph
  • p: paste text after cursor position.
  • P: paste text before cursor position.
  • xp: cut then paste after cursor, so swap two characters.
  • dwwP: cut one word, move to next word, paste before that word, so swap two words
  • .: repeat last action. If you want to repeat a series of actions, use qa to start recording a macro, then do changes, then press q to stop recording, in another line, use Ca to do same changes on that line. Or qb, qc, etc.
  • u: undo the last change.
  • ctrl-r: redo the undo.
  • :earlier 5m: back to five minutes ago, i.e. time machine..
  • :later 45s: forward in time…
  • :undo 5: undo the last five changes.
  • :noh: no highlight after search.
  • :undolist: view the undo tree.
  • /word: to move to the first occurrence of word.
  • n: go to next occurrence.
  • N: go to previous occurrence.
  • /\<word\>: search word exactly.
  • /\d\*: search 0 or more digit (s).

Search and replace

  • :S/search/replace/g: search and replace in current line.
  • :%S/search/replace/g: search and replace in all lines.
  • :%S/search/replace/gc: ask for confirmation

Multiple files

Multiple sections

  • :set foldmethod=indent, then at indent line, ze to close the fold (compress), zo to open the fold, or za to switch between close and open, alternate.

Multiple files

  • :edit file1, :e file2, then use :b 1 to go to file l, :b 2 to file 2. b means buffer. :ls to show all editing files.

Multiple windows

  • :new: to open a new window.
  • ctrl-w h/j/k/l or ctrl-w ctrl-w: to move among windows.
  • :sp: to split current window. or use ctrl-w s.
  • :vsp: to split vertical window, or use ctrl-w v.
  • ctrl-w r: to rotate positions of windows.
  • ctrl-w K: move current window to topmost position.
  • :resize 10 or resize -10: change window size to display 10 more/less lines.
  • ctrl-w _: increase current window size as much as possible.
  • ctrl-w =: make all windows same size.

Multiple tabs

  • :tabnew: to open a new tab
  • gt: go to next tab.
  • gT: go to previous tab.
  • :tabmove: to reorder tabs, e.g. :tabmove O moves the current tab to the first position.

Others

  • :!: to run shell commands within vim.