, updated

Everyday Vim

The Vim commands I actually use every day, kept as a cheatsheet. Editing, moving around, searching, tabs and windows, and a few tips.

Vim, short for Vi Improved, has been around for decades and it’s hard to learn at first. It’s worth the trouble because you do everything from the keyboard. Your hands never leave it, and once you know the commands you stop thinking about them. This note is the part of Vim I actually use, not everything it can do.

Exiting

  • esc / control + c - Leave insert mode
  • :w - Save
  • :q - Close the file
  • :wq / :x - Save and close

Editing

  • i / a - Insert before / after the cursor
  • I / A - Insert at the start / end of the line
  • o / O - Open a new line below / above
  • ea - Insert at the end of the word
  • x - Delete a character
  • r{character} - Replace the character under the cursor with character
  • J - Pull the line below up onto this one
  • p - Paste
  • u / control + r - Undo / redo

Operators

  • d - Delete
  • y - Yank (copy)
  • c - Change: delete it, then start typing
  • < / > - Indent left / right
  • gu / gU - Lowercase / uppercase

An operator needs a movement to act on. Put the two together and you get a command: ci) changes what’s inside the parentheses, ci} what’s inside the curly braces, d2w deletes the next two words.

Moving around

  • h j k l - Left, down, up, right
  • w / b - Start of the next / previous word
  • e / ge - End of the next / previous word
  • 0 / $ - Start / end of the line
  • ^ / 0w - First character on the line that isn’t a space
  • gg / G - First / last line of the file
  • :{number} - Go to line number
  • { / } - Previous / next paragraph
  • % - Jump to the matching {}, [] or ()
  • H / M / L - Top / middle / bottom of the window
  • gD - Go to where the variable under the cursor is declared

Type a number first and the move repeats: 5j goes down five lines, 3w forward three words.

Scrolling

  • zt / zz / zb - Put the current line at the top / middle / bottom of the window
  • control + e / control + y - Scroll down / up one line without moving the cursor
  • control + d / control + u - Scroll down / up half a page
  • control + f / control + b - Scroll down / up one page

Searching

  • f{character} / F{character} - Jump forward / backward to character
  • ; / , - Do that jump again, forwards / backwards
  • * / # - Next / previous place the word under the cursor shows up
  • /{pattern} / ?{pattern} - Search forward / backward for pattern
  • /{pattern}\c - Search forward, ignoring case
  • n / N - Search again, same direction / the other way
  • / then enter - Run the last search again
  • / then control + p / control + n - Go back / forward through past searches

Replacing

  • :%s/{old}/{new}/g - Replace every old with new
  • :%s/{old}/{new}/gc - The same, but it asks before each one

Visual mode

  • v - Start visual mode
  • V - Start visual line mode
  • control + v - Start visual block mode

Once you’ve selected something, a movement or an operator works on it. v then 2j marks two lines so you can p over them, and v then ip marks the paragraph you’re in.

Tabs

  • :tabnew {filename} - Open a new tab, the filename is optional
  • :e {filename} - Open a file in the current tab
  • gt / gT - Next / previous tab, or :tabn / :tabp
  • {number}gt - Go to tab number
  • :tabo - Close every tab but this one
  • :qa - Quit all tabs

Windows

  • :sp {filename} / :vsp {filename} - Split across / down, the filename is optional
  • control + w then s / v - The same splits, without typing a command
  • control + w then w - Switch between windows
  • control + w then q - Close this window
  • control + w then L - Move this window over to the far right

Tips

  • "_dd deletes a line without touching what you copied: _ is the black hole register
  • cgn changes the next search match, then . does the same to the one after it
  • "+y / "+p - Copy to / paste from the system clipboard
  • :ju - Show the jump list; control + o goes back through it and control + i forward
  • :noh - Turn off the search highlighting
  • :set nu - Show line numbers
  • :ter - Open a terminal

Further Reading


All posts