The editor for the real nerds.

It took me 3 weeks to get back up to normal editing speeds. Hopefully with this document, it will take you even less time.

Benefits

Known benefits:

  • Ubiquitous shell editor on Unix platforms. This means you can open an editor while ssh'ed into a server, without the need to leave the shell.
  • Shorter keyboard commands, often a single key – saves your wrists in the long run
  • uses very little memory, can open very large files without stuttering
  • undo trees, multiple clipboard buffers

Heard benefits:

  • "Better" than other shell editors like Emacs, Vi, Ed, Nano (obviously debatable).

Downsides:

  • Steep learning curve
  • Accidentally hit 'u' a lot in other editors when trying to undo

What you should know to do in your first day of using Vim

  • change modes: i for INSERT, esc to go back to NORMAL (navigation/shortcut mode), v for VISUAL
  • in NORMAL mode (note: capital letters means actual capitals – hold shift)
  • :q: quit (will warn if unsaved)
  • :q!: quit without saving
  • :wq: save and quit (also ZZ)
  • :w: save
  • u: undo
  • ctrl-r: redo
  • dd: delete line (puts in default clipboard)
  • x: delete character
  • p: paste from clipboard (after cursor)
  • P: paste from clipboard (before cursor)
  • <number>G: go to line <number>, n to go to next, N to go to previous</number>
  • gg: go to top of file
  • G: go to end of file
  • /<term>[Enter]: search (supports regex or strings)
  • :sh: jump back to shell without killing vim (exit or ctrl-d to return to vim)

What you should know in your first week of using Vim

  • hjkl: navigation, respectively left, down, up, right (can also use arrow keys)
  • w: go to next word
  • e: go to end of current word
  • b: go back a word
  • v: go into visual mode. You can move around your selection and then perform an action on it (e.g. cut d) This is like highlight/selecting in normal editors
  • shift + V: go into visual line mode. Your previous commands work on a per-line basis instead of per character (e.g. Vd is delete line. Vy is copy line. Vx is cut line.)
  • ctrl + v: go into visual block mode. Able to move your cursor down and then I and insert multiple things (e.g. delete # or add #)
  • dw: delete word. deletes to the end of the word it is on, doesn't do before the cursor.
  • dwi: delete inside word. deleted the word your cursor is on.
  • $: go to end of line
  • o: create newline below current line and move to it and begin insert mode]
  • :term: open a terminal inside vim (works in vim > 7, I think) - didnt seem to work in Vim 8.0
  • fn + arrow key jump to end of line
  • :e <filepath>: open other file directly from vim

How to copy/paste from other programs:

vim uses it's own clipboard implementation, which can be kind of annoying, but it allows for multiple clipboard buffers (registers). You can select which clipboard you use for a given delete/yank/put command using "<clipboard>, where <clipboard> is the register number.

Most systems use either + or * as the register number, So a copy into the system clipboard looks like "+yy (to copy a whole line) or "+<number>jy to copy the next <number> lines. Of course, replace + with * depending on your OS.

There are some great tips here for accessing the system clipboard, including a vim setting to make the default register your system register, which might simplify things significantly. In my .vimrc Ubuntu 18, this looks like set clipboard=unnamedplus to set the default clipboard to the + register.

What you should eventually know

  • :%s/word/newWord/g: Search and replace word with newWord. It's a similar implementation of the sed unix command.
  • :g/phrase/d: Globally find lines that contain "phrase" and delete those lines with 'd'.
  • Shift + v, (select the lines), Shift + . (>): indent lines. < will deindent lines.
  • Ctrl + w, then v open the same file to the right, and:
  • Ctrl + w, then l move between windows.

  • Suppose the cursor is on a word, and you want to search for a similar word. Press / then Ctrl-r then Ctrl-w to copy the current word to the command line. You can now edit the search pattern and press Enter

Cheatsheet