Vim: The Good Parts



Vim: The Good Parts

0 1


vim-ll

My vim lunch and learn presentation for work

On Github alexkuang / vim-ll

Vim: The Good Parts

Pretend you're a new programmer tasked with maintenance of a file on an old linux server. You ssh in and try editing the file with vim, since you've heard good things about it. But when you attempt to open the file, you discover that your key presses aren't inserting new letters, and CTRL-C won't exit the application like normal. What now? (cue demo - file: basics.py)

Vim is MODAL

  • Yes, key functions change depending on mode
  • Coders (or even just writers) don't spend all their time inserting text, why optimize solely for that?
  • Lots of modes, but two "most basic" ones are probably:
    • Normal mode: Primary mode, operate on text. Delete, cut, paste, indent, etc. This is the mode you will be spending the most time in.
    • Insert mode: Self-explanatory, every other text editor's "normal"
After hitting google you now know how to drop into insert mode and out, type new text, and even copy + paste in normal mode. But you're still jamming on the arrow keys to get everywhere, and every thing just seems slow and clunky. Even with the mouse, this is basically just on par with Notepad. And why is there a normal mode anyway? Why not just have key chords like every other text editor?

Motions and Text Objects

In normal mode, vim has a ton of motions and text objects that let you tell it how to move the cursor and how to select text.

One of the pieces of "common newbie wisdom" is to increase speed by using hjkl instead of the arrows to move in normal mode.

That helps a bit since you stay on home row, but hjkl is really for small adjustments and should be your last resort. Vim has way better mechanisms for movement.

Motions and Text Objects

Motions let you jump efficiently around in text. Some examples:

  • w - forward to next word
  • e - forward to end of word
  • fc - forward to next occurrence of c
  • tc - forward 'til (to character before) next occurence of c

Motions and Text Objects

The concept of "text objects" allows precise selections. Particularly powerful are objects combined with the `i` and `a` modifiers:

  • i( - (inner parentheses)
  • a( - (a set of parentheses)
  • i[ - [inner brackets]
  • a[ - [a brackets]
  • it - <p>inner tags</p>
  • at - <p>a set of tags</p>

Other objects exists as well: sentences, paragraphs, {, (etc)...

(cue demo - file: motions-objects.scala)

Why this is life-changing

  • It's a language for describing what you want to do to your text; think: "change inner tags", "delete 3 words", "delete inner {", etc, vs "hit backspace until it looks OK" or "fight with smart-select on the mouse"
  • Efficiency in actions + more natural "way of thinking" = less chance of breaking flow (IMO, esp on command line)
  • Each new operation/motion/text object dramatically increases the "commands" you know since it's all composable!
  • You can define your own motions/objects (e.g., ruby block) or operators (e.g., vim-surround)!

BUT WAIT, THERE'S MORE!

Now vim seems a bit more useful. You can issue "commands" to your text editor like dit instead of repeatedly fiddling around with the mouse. But still, hammering dit repeatedly while refactoring some code seems like kind of a drag, and might even lead to the dreaded Repetitive Stress Injury. Enter the . (dot) command.

(cue demo - file: move-op-repeat.scala)

Move, Operate, Repeat

Common pattern in the usage in the dot command: move -> operate -> repeat. The entirety of vim is geared towards making this workflow easy.

Move: Move to the text you want to edit (efficient motions) Operate: Make whatever changes you want (flexible operators composable with motions + text objects) Repeat: Repeat that change easily (dot operator)

There are other repeat operators (ex: macros, see :help @), but the dot operator alone gives HUGE bang for buck

Recap

THE takeaway for vim usage:

Efficient Movement: Use super fast motions instead of arrow keys / mouse fiddling! Precise Action: Compose operators with motions + text objects to do exactly what you want DRY: Vim provides a ton of ways to cut down on repetition

What now?

Adopting vim

  • Go vim or go home--Give it a real chance!
  • Don't go crazy configuring yet! Grab a sensible, but not HUGE, "modern" vimrc template (See example_vimrc in project) to start; this can be expanded to your preference.
  • Get back to "pre-vim" productivity, then exceed it... Hopefully shouldn't take long.
  • How?

Adopting vim

  • Learn the normal mode keystrokes. :help [key] is super helpful here.
  • ... But one step at a time! Pick one thing, drill it into your muscle memory, repeat.
  • Learn the vim motions + objects. :help motion.txt
  • DRY: notice repetition, cut it out. Again, . is super important, but there are others like @@, &, etc
  • Don't rote memorize! Long, specific commands are just chained smaller components, so learn + reuse those components. ex: ggVGy -> gg, V, G, y
  • But seriously, practice! (See second bullet point)

For non-vim users

"Vim nature": injecting a vim-like philosophy/interface to other programs. It's becoming more and more common as people catch on to the awesomeness of vim.

  • emacs: evil mode - Oddly enough, best received of all...
  • sublime: vintage mode
  • eclipse: vrapper
  • bash: set editing-mode vi in ~/.inputrc... or set -o vi
  • Firefox: vimperator, pentadactyl

Misc Cool Demos

(Moving beyond stock vim)

Plugins for the skeptics

  • Use pathogen!! (or vundle)
  • ctrl-p
  • dispatch
  • vim-surround
  • (Basically anything Tim Pope)

(cue demo - no file; just plugins)

Filters

(Inspired by previous ACME text editing L&L)

Lets you run your selections through arbitrary code/scripts

(cue demo - file: filters.py)

More demos?

Perhaps during dev demo days?

Thanks!

(https://github.com/alexkuang/vim-ll)