Power Vim

Table of Contents

1 Getting started

I assume you have vim installed, and you know how to run it. What next?

1.1 Learning

1.1.1 Do vimtutor

Do it! Run vimtutor on the command line. (Demo)

1.1.2 Use Google

Use Google when you want to do some editing task; there's always some cool way to do it in vanilla, no-plugins vim. Rarely do you actually need plugins to do things efficiently.

(Demo)

Examples:

  • "moving to the end of line in vim"
  • "placing things at the ends of lines vim"
  • "find and replace vim"
  • "delete word vim"

For more learning, maybe try some vim games, there's a bunch. Play nethack for hjkl. :)

1.2 Escape

A very important key in vim that you will hit a lot. But unfortunately it is in a rather non-optimal position on the keyboard… So it's useful to optimize it.

Science fact: Ctrl-[ will do the same thing as Escape in vim. In fact, in the terminal, Ctrl-[ is identical to Escape; here's why if you are curious.

So, Ctrl-[ is a bit of an improvement over craning your pinky to hit Escape, but not that much. The remaining improvement comes from telling your operating system to treat your CapsLock key as an additional Ctrl key. This helps in all kinds of ways, since Ctrl is such a commonly pressed key. Possibly you can have your OS treat your Ctrl key as a CapsLock key, if you really want to keep a CapsLock on your keyboard.

Anyway, to turn CapsLock into Ctrl, you can follow this guide.

Then, you can easily use Ctrl-[ to send Escape; much better than moving your pinky to the far-away Escape key!

1.2.1 Do not map jj or jk to Escape

It's a huge hassle and annoyance.

  • You get used to something totally nonstandard
  • You mess up pasting text
  • You ruin the purity of insert mode
  • You always have to keep in the back of your mind not to hit that combination
  • It's not portable to other sets of vi keybindings

1.2.2 Do not turn CapsLock into an Escape key

Escape is basically useless outside of vim! The Ctrl key is obviously quite useful everywhere, even inside vim, so go with that.

2 Practices that go well with vim

2.1 Keyboard based workflow

A keyboard based workflow is really handy. By not moving your hands to the mouse or even to Home/End/etc, you can move a lot faster while developing. Just make sure you know how to touch type.

2.2 tmux

A great alternative to vim's super-confusing system of tabs, windows, buffers, etc. (Demo) Don't bother to learn those things in vim when you can just open new tmux windows and panes.

2.3 Tools with vim keybindings

These are handy to know because they can help you learn vim more quickly.

2.3.1 vimperator (fantastic - for Firefox)

An excellent vim-like interface for Firefox.

  1. Hinting

    When activated, puts "hints" on links. They can be filtered by typing letters in the text and followed by typing the number.

    vimp1.png

  2. Keybindings

    Has a lot of settings and makes it really easy to define all kinds of useful keybindings.

    vimp2.png

  3. Web development

    Even has some handy keyboard-oriented web development tools built in.

    vimp3.png

2.3.2 vimium (just okay - for Chrome)

A "just okay" vim-like interface for Chrome.

Pretty much the same as vimperator except without settings or keybindings. The only feature it really shares is the hinting.

2.3.3 File manager

ranger is pretty handy. (Demo)

3 Big edits:

Use unix tools for filtering, ! is your best friend. Select something, then !, then type a Unix pipeline. For a lot of batch editing, you can just do it with this; it's a great way to get experience with the extremely powerful text processing tools of Unix. For everything small scale, there's vim functionality.

If you want, define a new mapping for frequently used commands.
Put something like the following in your vimrc:

nmap <F5> :!grep awesome | cat

For more elaborate mappings, goole "vim mappings".

3.1 Examples

3.1.1 Reverse the selection horizontally

!rev

3.1.2 Reverse the selection vertically

!tac

3.1.3 Replace newlines with comma

!tr '\n' ,

3.1.4 Filter to just lines containing foo

!grep foo

3.1.5 Sort lines

!sort

3.1.6 Shuffle lines

!shuf

3.1.7 Uniquify lines

!sort | uniq

3.1.8 Select columns 1,3,5-7 of a CSV

!cut -d , -f 1,3,5-7

3.1.9 Get demographic information for a list of Andrew IDs

!xargs -I {} finger {}@andrew.cmu.edu

4 Regular edits

The "language" view of vi keybindings is best conveyed here.

4.1 Vim keybindings as language

Commands are sentences. There are verbs and nouns and adjectives which you chain together to do things.

Without this perspective, it might seem cryptic and hard to learn; but with this perspective, it's just a variation on how you normally speak in English.

Keep in mind, though, you can only speak this language while in normal mode, so stay out of insert mode while not typing!

4.2 Nouns

Motions are our main nouns

  • hjkl (left down up right)
  • w (word, forward one)
  • b (back one word)
  • e (end of current word)

4.3 Verbs

And when partnered with a verb, we perform actions

  • d (delete)
  • y (yank, like copy)
  • c (change, delete then enter insert mode)
  • v (visually select)

4.4 Advanced nouns

Text objects are some pretty useful nouns They're only usable if you first type a verb. They have two interfaces: i for inside, a for around (i is exclusive and a is inclusive of the delimiters).

  • it at (in/around tags)
  • i( a( (in/around parentheses)
  • i" a" (in/around quotes)

Basically they are useful for things surrounded by matching parens or brakcets or tags. Read :help text-objects.

4.5 Interjections?

There are some commands with immediate effect (interjections?)

  • i (insert mode, which is only for inserting text)
  • a (append, enter insert mode just after the selected character)
  • p (paste; pastes after currently selected character)
  • P (paste; pastes before selected character)
  • Esc (escape to command mode, where you do your editing)

4.6 Adjectives

4.6.1 Numbers

Preface things with numbers to do them multiple times!

4.6.2 registers

(you'll rarely use these) Syntax is "x where x is a letter corresponding to some register; then commands will act on that register (putting things in it and taking things out of it).

4.7 Visual selection modes

  • v (visual mode - like your usual shift select in other editors, but with all the power of vim motions)
  • V (visual line mode - selects entire lines)
  • Ctrl-v (bizzare visual block mode - this is really weird but frequently useful for quickly changing things that are vertically aligned)

4.8 Handy miscellaneous commands

  • gq
  • V=

4.9 Additional motions/nouns

Look these up using :help

  • W B E
  • t f T F
  • { } ( )
  • $ ^ 0
  • / ?
  • ge

5 Golf/Exercises

5.1 Dungeon escape

5.1.1 Source text

The task is to make the text at this url look like the target: http://www.cmucc.org/talks/fall14/golf1.txt

5.1.2 Target text

DUNGEON OF GOOD
[]
[guard] "prisoner"
(t)(t)(t)(t)()
royal{}
      big good

5.2 vertical limit

5.2.1 Source text

The task is to make the text at this url look like the target: http://www.cmucc.org/talks/fall14/golf2.txt

5.2.2 Target text

[ "There", "is", "no", "vertical", "limit", "for", "vim", "Ninjas" ]

6 Don't use vim

6.1 Vim sucks and is horrible

  1. Terrible codebase Globals everywhere oh god
  2. vimscript is awful
  3. built-in spellchecker and encryption system
  4. can't run things asyncronously. Grepping for something blocks the UI. Syntax highlighting blocks the UI. Everything blocks the UI. so vim will perpetually hang
  5. Bram Moolenaar is an awful project leader. These things will never get fixed.

    Basically the only redeeming feature is the keybindings.

6.2 emacs is much much better, and even less bloated

EMACS! (and it's more circular) emacs.png

  1. It's organized into packages that are loaded only on demand. Yes, it ships with tetris, no, that doesn't make it bloated. vim, on the other hand, is just one huge disgusting C program.
  2. it can run things asynchronously! it does this amazingly!
  3. it's actually easily extensible! It's just easy simple Elisp almost all the way down!
  4. It is an elegant, polished system.

    But its keybindings suck…

6.3 evil-mode gives nice vim keybindings in emacs

Actually what I am using right now! Ha ha! Come to the Emacs talk next week! Run this script to bootstrap and set it up: http://www.cmucc.org/talks/fall14/vim-install.sh Or just follow what it does (subsituting emacs for $vim)

7 Advanced

7.1 .vimrc

Comment your settings, saying what every setting does and/or why you have it set. You'll thank yourself for this later.

7.2 Plugins

Don't use them. But if you do, use Pathogen to manage them.

I do like powerline though (it's purely aesthetic). https://github.com/Lokaltog/vim-powerline. Don't use the pointless fancy symbols though, they're not that great and break using your vim over ssh.

7.3 Mappings

DO NOT MAP OVER DEFAULT VIM BINDINGS. There are no useless bindings in vim. They are all there for a reason. In researching for this talk I saw a lot of really stupid mapping recommendations. For example, remapping ; to : when ; is insanely useful. "Who remembers what it does anyway?" Why even use vim in that case?

If you want a single-key binding, use one of the function keys. Otherwise use a combination of your leader (defaults to \) and another key. The one exception is s. It's a truly useless command that is identical to cl.

8 Other misc commands

8.1 Macros

q

8.2 Ctrl-o/Ctrl-i

SO AMAZING. Navigates through your motion history in the file. THE BEST THE BEST THE BEST.

8.3 Marks

These are motions.

  • m someletter to mark a position (line and column)
  • ' someletter to that line (almost always, all you want)
  • ` someletter to that line and that column (this would be annoying if it was the default)

8.4 zz is handy

Centers screen on your cursor. I spent so much time going into insert mode, pressing enter until I had a nice empty space, then deleting all that space, before I found zz.

8.5 G and gg

Give them arguments to go to specific lines, hooray.

8.6 g?

ROT13 WOOOOO.

8.7 ways to go into insert mode

  • I for insert at the beginning of the line
  • A for append at the end of the line
  • o for insert on a new line after current line
  • O for insert on a new line before current line

8.8 search and replace

:[some range]s/someregex/replacementtext/[some options like g[lobal] and c[onditional]]

One useful range is %, all of the file.

Using visual mode and doing : will automatically do search and replace over what you have selected.

8.9 Semantic Linefeeds!

The idea is best explained here. But here's a short summary…

The idea is: When editing human-language text in LaTeX, Markdown, or other kinds of markup, restrict yourself to having only one sentence (or clause) per line. Why?

Pretty much every kind of structured text (LaTeX, Markdown) will ignore single newlines when producing their final output. And, most powerful text editors have a lot of tools for manipulating, moving around, copying on a line-by-line basis. So, you can easily make your editing and revising a lot easier and more ergonomic, just by using single newlines to separate logical units of your human-language text, without affecting the final product at all. It's kind of like writing English text like you write code, with one expression per line. (Though less aggressive)

And, even if you need to stay within a line length limit, you can just reindent the text at the end. In vim, you can use v to select the lines that need to be reindented, then gq.

Created: 2015-09-16 Wed 19:59

Emacs 24.5.1 (Org mode 8.2.10)

Validate