, updated

Everyday Vim

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

Vim, short for Vi Improved, is a highly configurable text editor that has been around for decades. It has a steep learning curve, but once it clicks, it makes everyday editing noticeably faster. This note is how I use Vim day to day, and the commands I reach for most.

Why Vim?

Vim is popular among developers and power users for a few reasons:

  1. Speed - Complex edits happen from the keyboard alone. Once you’re fluent, you move through a file at an impressive pace.
  2. Efficiency - Your hands stay on the keyboard, which saves time and spares your wrists.
  3. Cool - 😎

Cheatsheet

Exiting

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

Editing

  • i - Insert before the cursor
  • a - Insert after the cursor
  • I - Insert at the beginning of the line
  • A - Insert at the end of the line
  • o - Open a new line below the current line
  • O - Open a new line above the current line
  • ea - Insert at the end of the word
  • x - Delete a character (cut)
  • r{character} - Replace the character under the cursor with character
  • p - Paste
  • u - Undo
  • control + r - Redo
  • J - Join the line below with the current line

Operators

  • d - Delete
  • y - Yank (copy)
  • c - Change (delete, then insert)
  • > - Indent right
  • < - Indent left
  • gU - Change to uppercase
  • gu - Change to lowercase

Operators combine with movement keys. For instance:

  • ci) - Change the text inside the parentheses (…)
  • ci} - Change the text inside the curly braces {…}
  • and so on

Arrow

  • h - Move the cursor left
  • j - Move the cursor down
  • k - Move the cursor up
  • l - Move the cursor right

To move several steps at once, put a count in front: 5j moves down five lines instead of pressing j five times.

Word

  • w - Jump to the start of the next word
  • b - Jump to the start of the previous word
  • e - Jump to the end of the next word
  • ge - Jump to the end of the previous word

Line

  • 0 - Jump to the start of the line
  • ^ / 0w - Jump to the first non-blank character of the line
  • $ - Jump to the end of the line

Document

  • gg - Go to the first line
  • G - Go to the last line
  • :{number} - Go to line number

Paragraph

  • % - Jump between matching {}, [], ()
  • } - Go to the next paragraph
  • { - Go to the previous paragraph

Window

  • H - Jump to the top of the window
  • M - Jump to the middle of the window
  • L - Jump to the bottom of the window

Other

  • gD - Go to the global declaration of the variable under the cursor

Scrolling

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

Searching

Character

  • f{character} - Jump forward to character
  • F{character} - Jump backward to character
  • ; - Repeat the search forwards
  • , - Repeat the search backwards

Current word

  • * - Jump to the next occurrence of the word under the cursor
  • # - Jump to the previous occurrence of the word under the cursor

Pattern

  • /{pattern} - Search forward for pattern
  • ?{pattern} - Search backward for pattern
  • /{pattern}\c - Search forward, ignoring case
  • n - Repeat the search in the same direction
  • N - Repeat the search in the opposite direction
  • / then enter - Repeat the last search
  • / then control + p - Step back through search history
  • / then control + n - Step forward through search history

Replace

  • :%s/{old}/{new}/g - Replace every old with new
  • :%s/{old}/{new}/gc - The same, asking before each replacement

Marking text (visual mode)

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

Once in visual mode, combine it with movement or operator keys. For instance:

  • v then 2j - Mark two lines, then p to paste over them
  • v then ip - Mark the paragraph under the cursor

Tabs

  • :tabnew {filename} - Open a new tab (the filename is optional)
  • :e {filename} - Edit a file in the current tab
  • gt / :tabn - Go to the next tab
  • gT / :tabp - Go to the previous tab
  • {number}gt - Go to tab number
  • :tabo - Close every tab except the current one
  • :qa - Quit all tabs

Windows

  • :sp {filename} - Split the window horizontally (the filename is optional)
  • :vsp {filename} - Split the window vertically
  • control + w then s - Split horizontally
  • control + w then v - Split vertically
  • control + w then w - Switch between windows
  • control + w then q - Close the current window
  • control + w then L - Move the current window to the far right

Tips

  • Delete a line without overwriting the yank register: "_dd sends the line to the black-hole register _
  • Change the next search match, then repeat it: cgn, then . for each following match
  • :ter - Open a terminal
  • :set nu - Show line numbers
  • "+y - Copy the selection to the system clipboard
  • "+p - Paste from the system clipboard
  • :ju - Show the jump list; control + o goes back through it and control + i goes forward
  • :noh - Clear search highlighting

Conclusion

Vim is a powerful tool for everyday work. Bringing it into my daily routine has made me noticeably faster. Give it a try, and you may wonder how you got along without it. Happy Vimming! ⌨️

Kudos to


All posts