Vim is a text editor, accessible from a command-line interface. It’s a great tool for quick edits on a single file, without needing to open a full separate text editor.

There are four modes of operation:

  • Command mode — We start in this mode by default and can switch to any mode from here. To switch back to this mode, we can press Esc.
    • Command-line mode allows us to enter commands (see below). We enter by pressing : in command mode.
  • Insert mode — Used to edit the contents of the file, accessible by pressing i.
  • Visual mode — Used to visually select text and run commands over that section of code, accessible by pressing v.

Neovim is a popular fork of Vim with a huge amount of extensions and extra feature support.

Commands

Command-line mode

Important commands for exiting:

  • :w to save the file without exiting.
  • :q to exit the editor.
  • :wq to write the file to disc and exit.
  • :q! to exit the editor and ignore changes made.
  • ZZ to save and quit.

Normal mode

  • "*yy to copy the current line.
  • "*yG to copy the entire file.
  • dd to cut the current line.
  • p to paste.
  • x to delete the character after the cursor.
  • u to undo the last operation.
  • Shift+v to select the whole line.

Visual mode

  • y to copy selected text.
  • d to cut selected text.
  • To paste the text:
    • p for after the cursor.
    • P for before the cursor.

Getting around

For full screen navigation:1

  • To replace the standard arrow keys:
    • h goes left one character.
    • j goes down.
    • k goes up.
    • l goes right.
  • gg to move the cursor to the first line.
  • #G to move the cursor to the # line.
  • GG to move the cursor to the last line.
  • zt to move screen such that the cursor’s at the top.
  • zb to move the screen so the cursor’s at the bottom.
  • zz to centre screen on the cursor.
  • CTRL+f to move the cursor forward a full page.
  • CTRL+b to move the cursor backwards a full page.
  • CTRL+u to move the cursor up a half page.
  • CTRL+d to move the cursor down half page.

Footnotes

  1. From this Reddit comment.