Learn Vim

  • data-coding
  • learn
  • vim
  • spring

Philosophy

  1. Optimise for reading code/text, not writing
  2. Be programmable by itself (keystrokes are composable commands)
  3. Avoid the mouse (too slow) or even the arrow keys (too much hand movement)

In practice this means using different "modes of operation" for different kinds of tasks.

Vim modes

  • NORMAL (<ESC>, sometimes double)
    • Default mode the editor starts in; one should spend most of the time here
    • Each keypress is equivalent to an editor command
  • INSERT (i)
    • "Normal" mode of other editors
    • Each keypress actually inserts a character to a given file
  • VISUAL (v)
    • Allows you to take a selection of text and apply various transformations
  • COMMAND (*)
    • Lets you run commands that control the whole editor in its "command line"
    • Technically known as Command-line or Cmdline mode
  • You use the <ESC> key a lot when using Vim: consider remapping Caps Lock to Escape (macOS instructions).

Snippets

Normal:

  • Basic movement: hjkl (left, down, up, right)
  • Words: w (next word), b (beginning of word), e (end of word)
  • Lines: 0 (beginning of line), ^ (first non-blank character), $ (end of line)
  • Screen: H (top of screen), M (middle of screen), L (bottom of screen)
  • Scroll: Ctrl-u (up), Ctrl-d (down)
  • File: gg (beginning of file), G (end of file)
  • Line numbers: :{number}<CR> or {number}G (line {number})
  • fx: jump to the next occurrence of character x
  • /{regexp}<Enter>: look up {regex} from the current line downwards (with n for next)
  • u: undo
  • y: copy/yank
  • p: paste

Insert:

  • d2w delete two words or 3dddelete three lines below or df) delete until you find ) (= noun, verb, modifier); generally d{motion}
  • a: append to the right
  • A: append to the end of line

Visual

  • v: characters
  • <Shift+v>: lines
  • <Ctrl+v>: blocks

Command:

  • :q!: quit without saving
  • :x: quit with saving
  • :history, :help command
  • :s/{regexp}/{string}/{mod}: substitute matched {regexp} for {string} with given {mod} (g for replace all, c with confirmation, i for case insensitive) whilst \r is the new line
  • :!{cmd}: execute {cmd}
  • :r!{cmd}: execute {cmd} and paste its stdout to the currently opened file (buffer)
  • :%!{cmd}: pass the contents of the whole file (%) through {cmd}
  • :sp, :svp: split windows
Metadata