Skip to content

Instantly share code, notes, and snippets.

@schnittchen
Last active November 10, 2018 12:50
Show Gist options
  • Save schnittchen/bca2a43e44fe1dd8fc51467dc41dc383 to your computer and use it in GitHub Desktop.
Save schnittchen/bca2a43e44fe1dd8fc51467dc41dc383 to your computer and use it in GitHub Desktop.
VIM survival and beyond cheatsheet

VIM survival and beyond cheatsheet

To brush up beginner knowledge: run vimtutor

Bare minimum

a.k.a. I hate vim but I'm forced to use it:

Everything here starts in "normal mode"!

  • some commands bring you in "insert mode"
    • Press i to insert text before cursor, insert some text, finish with ESC
  • colon brings you to "command mode"
    • enter :w<enter> to save file
    • :q<enter> to quit without saving
    • :q!<enter> to quit, abandoning changes
  • If unsure which mode your're in, ESC brings you back to normal mode

Essential movements

  • hjkl character-by-character
  • ^f ^b page-wise up/down
  • G gg bottom/top line

Poisoned fruit: you can use arrow keys instead of hjkl. Avoid them!

Basic editing

  • we have already seen i

  • To insert at the end of line: A

  • x and X to delete char after/before cursor

  • dd to delete whole line

  • J to join line to next one

That's it, the bare minimum to get things done.


Beyond survival

This + that (1)

  • u and C^R are undo/redo, but only one step unless configured
    • :help undo
  • D/C deletes/changes from cursor to end of line

Yanking and pasting

This feature is closest to the "clipboard":

Yanking saves text in a "register". Deleting saves the deleted text in a register.

  • y + motion, d + motion.
  • yy and dd yank/delete a line
  • p / P paste the yanked/deleted text
    • before/after the cursor
    • when yanked/deleted line-wise, on previous/next line

Visual modes

  • v, V (linewise), C^V (block-wise)
  • then motions to move the boundary
  • o (and O in block-wise) to switch boundary
  • finally, a command like c, d, y (insert/delete/yank). Or ESC.

More movements

:help motion.txt will blow your mind!

  • "find": f<letter> and F<letter> move to next/prev <letter> on line
  • "till": same with t and T, but stop right before letter
  • w b move to beginning of next /end of previous word
  • W B same fo WORDs (white-space separated text)
  • H M L jump to top/middle/bottom of current screen of text
  • % jumps to matching bracket (of any kind)
  • <number>G go to this line
  • 0 and $ go to beginning (non-whitespace) and end of line
  • ( ) sentence forward/back
  • [ ] paragraph forward/back
  • / regular-expression search (vim has its own flavour!)

Regularities and grammar

  • yy cc dd are line-wise
visual_editing_operation: v (motion)* operator
editing-operation: ([register] [repetition] operator [force] [repetition] (motion | text-object)) | visual-editing-operation
text-object: object | (a or i)
  • Register: for yanking and pasting, which register to use instead of default. :help registers
  • Repetition: how many times
  • Force: v V C^V force character-, line-, blockwise mode
  • a is "around", i "inside" mode
    • for text objects defined by delimiters: with vs. without them
    • for all others: with or without trailing whitespace

Editing operations can be repeated with the powerful and simple .. Typical:

  1. do a search
  2. do editing operation
  3. repeat search
  4. decide whether to apply op here too
  5. repeat at (2) or (3)

(Text) objects

:help text-objects

These are the objects (without leading a/i)

  • w W s p words WORDs sentences paragraphs
  • text quoted in single, double quotes or backticks
  • brackets (all four kinds)
    • SUPER USEFUL in programming b/c nesting aware
  • t HTML/XML tags

In visual mode, text objects (with a/i) extend the selection.

This + that (2)

:set [no]<feature> sets/unsets flags:

  • feature=number: line numbering
  • feature=hls|hlsearch search result highighting
  • feature=paste past mode: disables some syntax rules for pasting as-is from clipboard

Insert mode goodies

  • C^R" paste previous yank (unless line-wise)
    • example: ce(C^R")ESC wraps word in brackets
  • C^N auto-completion from several sources
    • C^N/C^P ... then <enter> if menu pops up
  • C^XC^L completes line: massive time-saver!
  • C^XC^O "omni-completion" (needs language integration)
  • C^XC^F complete file path

Where to go from here

  • to get a comprehensive understanding of vim: :help user-manual
  • to get a good overview of features and discover more motions and bindings, check out this awesome cheat sheet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment