Skip to content

Instantly share code, notes, and snippets.

@lettertwo
Created April 8, 2019 14:56
Show Gist options
  • Save lettertwo/813e765a9f68127b0807d1d6619c5ab0 to your computer and use it in GitHub Desktop.
Save lettertwo/813e765a9f68127b0807d1d6619c5ab0 to your computer and use it in GitHub Desktop.
Philosophy of VIM

Philosophy of Vim

  • most of programming time is spent editing, not writing.
  • editing is a process of manipulating text in ways beyond just writing, such as deletion, replacement, reordering, duplication, formatting, etc.
  • since writing (input) is a relatively small part of the whole of programming, making the other tasks easier by default can be a productivity win.
  • os shortcuts for editing can't get 'shadowed' by custom actions, since they take the form of commands in 'normal' (command) mode

A Modal Editor

Command Mode

You will almost always be in this mode, so much so that it is useful to think of this mode as the 'normal' mode. In fact, Vim calls it Normal Mode. In general, the other modes can be thought of transitional or temporary–you are always in the process of returning to Command Mode.

Return to Command Mode by pressing esc (or ctrl+c).

A Repertoire of Movements

Command Mode is centered on the concept of 'movements'

Basic movements:

  • left: h
  • down: j
  • up: k
  • right: l

WTF hjkl???

There are apparently historical reasons for this–early terminals often lacked arrow keys. However, a side affect of this is that the most common movement comes without having to even leave the 'home row'

Word boundary movements:

  • next word: w
  • end of word: e
  • beginning of word: b

Grouping movements:

  • beginning of line: ^
  • end of line: $
  • next paragraph: }
  • previous paragraph: {
  • matching bracket: %

targeted movements:

  • move to next occurrence of the current word: *
  • move to previous occurrence of the current word: #
  • move (un)til character: t<char>
  • move back (un)til character: T<char>
  • move to found character: f<char>
  • move back to found character: F<char>
  • move to match: /<regex>
  • move back to match: ?<regex>
  • move to next occurrence of a match: n
  • move to previous occurrence of a match: N

Insert Mode

This mode works the way text editing works throughout the OS. Same shortcuts (cmd+c, cmd+p, etc), same movements (arrow keys, page up/down, ctrl+a, ctrl+e, etc).

There are many movement commands that can result in switching to Insert Mode:

  • insert: i
  • insert after: a
  • insert at the start of the line: I
  • insert at the end of the line: A
  • insert on new line: o
  • insert on new line above: O
  • substitute: s
  • change: c

Visual Mode

A mode for creating selections. It's a modified version of Command mode, where commands either result in modifications to a selection range, or perform an editing task on the selection.

To enter Visual Mode (starting from Command Mode):

  • v to select by character
  • V to select by line

While in visual mode, most movement commands can be used to expand or contract the selection. Commands that make edits do so on the current selection, and automatically switch to either Command Mode or Insert Mode, depending on the command. For example, pressing i

A Language of Text Editing

Command Mode presents a sort of "language" that allows describing text editing tasks with combinations of commands. Many commands can be conveniently thought of as the "verbs", "nouns" and "prepositions" that make up the "language".

Examples of "verbs":

  • change (c)
  • replace (r)
  • substitute (s)
  • delete (d)
  • yank (y)
  • put (p)

WTF 'change', 'replace', AND 'substitute'? aren't these all the same thing?

Nope!

  • Change deletes everything in the next movement and puts you in Insert Mode (though with a selection, it behaves like substitute).
  • Substitute immediately deletes the current character (or selection) and puts you in Insert Mode.
  • Replace never enters Insert Mode at all, but just replaces the current character (or every character in a selection) with the provided character.

Examples of "nouns":

  • word (w)
  • end (e)
  • beginning (b)

"Nouns" can be any of the available movements!

Examples of "prepositions":

  • inside (i)
  • around (a)
  • (un)til (t)

WTF i thought i and a were insert commands!

Many Vim commands expect to be appended with a movement. When waiting for that movement, the command is 'pending', during which time "prepositional" commands become available. So, when a command like d (delete) is entered, nothing will happen because the command is 'pending' a movement. Pressing i next won't enter Insert Mode, as you might expect when starting out. In fact, it will leave the command 'pending', because i in this context is the 'inside' command, which requires a subject (such as w) to complete the movement!

You can combine these parts of the "language" to perform editing tasks:

  • delete to the end of a word: dw (or de)
  • delete the current word: diw
  • yank to the end of word: ye
  • change inside parentheses: ci) (or ci()
  • delete around brackets: da] (or da[)

There are many other ways to combine commands. For example:

  • delete 3 words: d3w (or 3dw)
  • change a visual selection: v<movement>c
  • yank the next 5 characters: y5l (or 5yl)
  • auto-indent inside a block: =i} (or =i{)
  • delete back through the word 'foo': d?foo
  • duplicate this line plus the 3 lines above:y3kp
  • replace the entire document with a bunch of Qs: ggvGrQ

Tips & Tricks

  • repeat the last command with .
  • undo with u
  • redo with ctrl+r
  • jump to top of the document with gg
  • jump to the bottom with G
  • jump to a line number with <number>G
  • Some commands can be performed on the current line by repeating them:
    • delete the current line: dd
    • change the current line: cc
    • yank the current line: yy
  • remap CAPSLOCK to ESC
  • open the key binding resolver in Atom (cmd+.) to learn what various keys do.

Atom + Vim Mode

  • Multiple cursors!
  • OS integration!
  • MUCH prettier!

https://atom.io/packages/vim-mode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment