Power Vim

Table of Contents

1 Getting started

1.1 installing vim

Just use Linux?

  • OS X

brew install vim, brew install macvim for a GUI.

  • Windows

Google and install it.

1.2 learning

Do vimtutor. Use Google when you want to do some editing task - there's always some cool way to do it in vanilla, no-plugins vim.

Examples:

  • moving around in vim
  • placing things at the ends of lines
  • doing some find and replace
  • doing anything, pretty much ("deleting a word")

Maybe some vim games, there's a bunch. Play nethack for hjkl. :)

1.3 Escape

A very important key in vim that you will hit a lot. So save yourself some effort: Remap CapsLock to Ctrl, then use Ctrl-[ to go to normal mode. Ctrl-[ sends the same thing as Escape in all terminals, and is also mapped to Escape in gvim for consistency. I recommend this over mapping CapsLock to Escape because a lot of other programs use Ctrl as well.

Do not map jj or jk to Escape, that is a huge 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

1.3.1 OS X

  1. Open System Preferences → Keyboard and choose Modifier Keys:

    ./mac1.png

  2. Then select which of the four modifier keys should send Control:

    ./mac2.png

1.3.2 Linux

It may be in your desktop environment. for GNOME, check Tweak Tool -> Typing -> Ctrl key position.
If you don't see it, use xmodmap:

curl [[http://www.cmucc.org/talks/fall14/xmodmaprc]] > ~/.xmodmaprc

Then run this command on startup (or when you want to swap):

xmodmap ~/.xmodmaprc

1.3.3 Windows

Google it (you need to do registry editing).

2 Practices that go well with vim

2.1 keyboard based workflow

Basically anything that enables a keyboard based workflow. By not moving your hands to the mouse or even to Home/End/etc, you can type and edit faster. Just make sure you know how to touch type.

2.2 Semantic Linefeeds!

One sentence (or clause) per line. Pretty much every kind of structured text will just ignore single newlines (LaTeX, Markdown). So, when using an editor that has a lot of tools for manipulating lines (pretty much everything), you gain a lot of power with this organization! And, even if you need to stay within a length limit, just visually select the lines with v and then gq. http://rhodesmill.org/brandon/2012/one-sentence-per-line/

2.3 tmux

A great alternative to vim's super-confusing system of tabs, windows, buffers, etc.

2.4 tools with vim keybindings

2.4.1 Browser

2.4.2 vimperator (fantastic - for Firefox)

  • 1
    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
    Has a lot of settings and makes it really easy to define all kinds of useful keybindings ./vimp2.png
  • 3
    Even has some handy keyboard-oriented web development tools built in ./vimp3.png

2.4.3 vimium (pretty meh - for Chrome)

Pretty much the same except without settings or keymaps the only feature it really shares is the hinting.

2.4.4 File manager

ranger

2.4.5 Shell

readline has vi mode. Set it in your ~/.inputrc with:

set editing-mode vi

This will give you vi mode for bash, rlwrap, all kinds of things that use readline.

  • OS X
    Apple ships outdated bash, so: brew install bash

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

!rev

3.1.2 Replace newlines with comma

!tr '\n' ,

3.1.3 Filter to just lines containing foo

!grep foo

3.1.4 Sort lines

!sort

3.1.5 Shuffle lines

!shuf

3.1.6 Uniquify lines

!sort | uniq

4 Regular edits

4.1 Vim as language

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

It might seem cryptic and hard to learn, but vim/vi keybindings are more semantic than any other editor I know of. Ctrl-X? Ctrl-V? F11? C-u C-x M-x butterfly? these are random and archaic.

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 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 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 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 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 There are also 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

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

5.1.1 Target

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

5.2 vertical limit

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

5.2.1 Target

[ "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 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. One exception is s. It's a truly useless command that is identical to cl.

7.4 Macros

q

7.5 Ctrl-o/Ctrl-i

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

7.6 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)

7.7 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.

7.8 G and gg

Give them arguments to go to specific lines, hooray.

7.9 g?

ROT13 WOOOOO.

7.10 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

7.11 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.

Date: 2014-09-03T23:48-0400

Author:

Org version 7.9.3f with Emacs version 24

Validate XHTML 1.0