Skip to content

Instantly share code, notes, and snippets.

@schmich
Last active March 21, 2024 12:46
Show Gist options
  • Save schmich/da33c8a3b59eea96471db47de113dd11 to your computer and use it in GitHub Desktop.
Save schmich/da33c8a3b59eea96471db47de113dd11 to your computer and use it in GitHub Desktop.
Vim Bootcamp

Vim Bootcamp

Concepts

  • Vim is a modal editor with four primary modes
    • Normal: default mode used for movement and commands
    • Insert: typical text editing mode
    • Visual: selection mode for selecting characters, lines, and blocks of text
    • Command-line: for entering editor commands (e.g. :q or :help)
  • Vim strives to be purely keyboard-driven
  • Most commands have some handy mnemonic to help you memorize it
    • e.g. d is delete, u is undo, p is put, i is insert
  • Many commands have multiple forms that do something similar
    • e.g. i inserts before cursor, I inserts at the beginning of the line
    • e.g. d is the delete command, D deletes to end-of-line, dd deletes an entire line
  • Orthogonal design
    • Vim has two main types of commands: movement and editing
    • You can combine editing commands with movement commands to make editing more powerful
      • e.g. w moves the cursor by a word, d deletes something, so dw deletes a word
    • Commands can be repeated by prefixing them with a number
      • e.g. j moves the cursor down one line, so 5j moves the cursor down 5 lines
      • e.g. yy yanks (copies) a line, so 10yy copies the next 10 lines
    • Editing-movements can be combined with repetition as well
      • e.g. d3w deletes 3 words

Movement

  • Cursor movement
    • j down
    • k up
    • h left
    • l right
  • Word movement
    • w go to begining of next word (punctuation separates)
    • W like w, but only space separates
    • e go to end of current word (punctuation separates)
    • E like e, but only space separates
    • b go back to the beginning of previous word (punctuation separates)
    • B like b, but only space separates
  • Line movement
    • $ go to last character on line (mnemonic: same as regex)
    • ^ go to first character on line (mnemonic: same as regex)
    • 0 go to beginning of line
    • 10| go to column 10
  • Searching movement
    • /foo go to the next instance of "foo" in the document
    • ?foo like /foo but in reverse
    • n repeat the last search in its natural direction
    • N repeat the last search in reverse
    • fx find the next location of x on the current line and go to it
    • Fx like fx but in reverse
    • tx find til the next location of x on the current line and go to it (puts cursor just before x)
    • Tx like tx but in reverse
    • ; repeat the last line search in its natural direction
    • , repeat the last line search in reverse
    • %G
    • [, {
  • Marks
    • mx creates a mark named x at the cursor's current location
    • `x moves the cursor to the location defined by mark x
    • `` moves the cursor back to its last location (e.g. after a search or paging)
  • Document movement
    • Ctrl-d page down
    • Ctrl-u page up
    • gg go to top of file
    • G go to bottom of file
    • 10G go to line 10
  • View movement
    • H move cursor to top of view (high point)
    • M move cursor to middle of view
    • L move cursor to bottom of view (low point)
    • zz center view on the cursor's current line

Editing

  • Mode switching: transitioning from normal mode to insert mode
    • i start inserting left of cursor
    • I start inserting at the beginning of the line
    • a start inserting right of cursor (mnemonic: after or append)
    • A start inserting at the end of the line
    • o start inserting on a new line below current
    • O start inserting on a new line above current
    • R start replacing (like insert, but overwrites)
    • Esc Ctrl-[ return to normal mode
  • Editing
    • u undo
    • Ctrl-r redo
    • y copy (yank) text, requires movement
    • yy copy entire line
    • p put (paste) after cursor
    • P put before cursor
    • d delete text, requires movement
    • D delete to end of line
    • dd delete entire line
    • c change text (change is a delete that also starts inserting), requires movement
    • C change to end of line
    • cc change entire line
    • s substitute text
    • rx replace character under cursor with x
    • x delete single character under cursor
    • . repeat last editing command
    • ~ toggle casing of selected characters
    • >> << indent, outdent line (mnemonic: bitwise shift, like C)
  • Visual mode
    • While in visual mode, most normal-mode commands will apply to whatever you have selected (e.g. y yanks selection, d deletes it)
    • v switch to character-based visual mode
    • V switch to line-based visual mode
    • Ctrl-v switch to block-based visual mode
      • While in block-based visual mode, after selecting text, I and A will switch you to multiline insert/append mode where you can edit multiple lines together
    • Esc Ctrl-[ return to normal mode

Repetition

  • Commands and movements can be repeated
  • 3yy yank 3 lines starting at the cursor
  • 10>> indent 10 lines starting at the cursor
  • d2w and 2dw both delete 2 words
  • 4p put 4 times
  • 10k move cursor up 10 lines

Command-Line

  • : in normal mode, switch to command-line mode to enter editor commands
  • A few editor commands
    • q quit
    • q! quit without saving
    • wq write file and quit
    • wq! write file and quit, even if read-only
    • w write file
    • w! write file, even if read-only
    • %s/foo/bar/g replace all instances of "foo" with "bar" in the document

Other Goodies

  • Macros
    • Macros allow you to record commands and play them back. This is helpful when editing structured patterns of data or performing a repetitive operation on similar text.
    • qx start recording macro into the macro named x
    • q if recording, stop recording
    • @x replay the commands stored in x
  • Vim can address anything that has matching characters (e.g. quotes, parentheses, brackets, braces)
    • e.g. di" deletes the inner text between two quotes, same with di', di(, di{, etc.
    • This works with other commands, too, e.g. c and y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment