Vim Cheatsheet

Cursor Navigation

Vim has several commands for moving the cursor around the file. Here are the basic ones, which work in Normal mode:

  • h: Move cursor left
  • j: Move cursor down
  • k: Move cursor up
  • l: Move cursor right

These are some more advanced commands:

  • w: Move to start of next word

  • b: Move to start of previous word

  • e: Move to end of word

  • 0 (zero): Move to start of line

  • ^: Move to first non-blank character of line

  • $: Move to end of line

  • shift g: Move to end of file

  • gg: Move to start of file

  • <number> shift g: Move to line number. For example, 10G will move to line 10.

  • shift h: Move to top of screen

  • shift m: Move to middle of screen

  • shift l: Move to bottom of screen

  • :<number>: Move to line number. For example, :10 will move to line 10.

  • %: Move to matching parenthesis

  • *: Search for the word under the cursor

  • #: Search backwards for the word under the cursor

  • n: Move to next occurrence of search term

  • N: Move to previous occurrence of search term

Vim also supports searching with / (search forwards) and ? (search backwards). For instance, /text will move the cursor to the next occurrence of “text”, and ?text will move it to the previous occurrence. After a search, you can use n and N to navigate between occurrences.

Remember that these commands are meant for Normal mode. If you are in Insert mode, you would need to press Esc first to return to Normal mode.

Scrolling

Here are some common scrolling commands you can use in the Vim text editor:

  • Ctrl+d: scrolls down (toward the end of the file) half a screen.
  • Ctrl+u: scrolls up (toward the start of the file) half a screen.
  • Ctrl+f: scrolls forward down the page (equivalent to Page Down).
  • Ctrl+b: scrolls back up the page (equivalent to Page Up).
  • Ctrl+e: scrolls the window up by one line.
  • Ctrl+y: scrolls the window down by one line.
  • zz: centers the screen around the cursor.
  • zt: puts the line with the cursor at the top of the window.
  • zb: puts the line with the cursor at the bottom of the window.

It’s important to remember that in Vim, the cursor stays where it is when you scroll. It doesn’t move with the screen like it does in some other editors. This allows you to move the view to a part of the file while keeping your place.

Switching modes

Here’s a summary of the keys you can use to switch between different Vim modes:

  1. Normal mode to other modes:

    • i: switch to Insert mode
    • a: switch to Insert mode, but start inserting after the current cursor position
    • o: switch to Insert mode, opening a new line below the current one
    • shift o: switch to Insert mode, opening a new line above the current one
    • I: switch to Insert mode, but start inserting at the beginning of the line
    • A: switch to Insert mode, but start inserting at the end of the line
    • v: switch to Visual mode
    • V: switch to Visual Line mode
    • Ctrl+v: switch to Visual Block mode
    • :: switch to Command-line mode
    • /: switch to Command-line mode with a forward search
    • ?: switch to Command-line mode with a backward search
    • !: switch to Command-line mode for executing shell commands
    • R: switch to Replace mode
    • Q: switch to Ex mode
  2. Other modes back to Normal mode:

    • Esc: switch back to Normal mode from any other mode
  3. Visual mode to Select mode:

    • Shift+v: switch to Select mode
    • Ctrl+g: switch to Select mode

Remember that while in Insert and Command-line mode, pressing Ctrl+[ or Ctrl+c will also take you back to Normal mode. It’s often handy to use these combinations if your keyboard has the Esc key in an inconvenient location.

Operations

Here are some common operations you can perform in Vim:

  • x: Delete the character under the cursor
  • shift x: Delete the character before the cursor
  • d(motion): Delete from the cursor to wherever the motion takes you
  • y(motion): Yank (copy) from the cursor to wherever the motion takes you
  • c(motion): Delete from the cursor to wherever the motion takes you and start insert mode
  • gu(motion): Change the text from the cursor to wherever the motion takes you to lowercase
  • gU(motion): Change the text from the cursor to wherever the motion takes you to uppercase

Motion modifiers

  • i: Inside. For example, di" will delete everything inside the next pair of quotes.
  • a: Around. For example, da" will delete everything inside the next pair of quotes, including the quotes.
  • t: Till. For example, dt" will delete everything up to (but not including) the next pair of quotes.
  • f: Find. For example, df" will delete everything up to (and including) the next pair of quotes.
  • shift f: Find backwards. For example, dF" will delete everything up to (and including) the previous pair of quotes.

These can be followed by cursor navigation commands to perform the operation on a larger piece of text. For example, d2w will delete the next two words, and c3j will delete the next three lines and start insert mode.