Ian Langworth’s
Things of Variable Interest

Everything I Missed in "Vim After 11 Years"

· 11 min read · Content-Type: text/tips; type=hot

What a response! The original “Vim After 11 Years” post was very popular, and it prompted a lot of good responses on Hacker News and Reddit which were peppered with useful tips and plugins I didn’t know about. I compiled all of them along with some additional tips from friends and coworkers, then tried a lot of them out, and finally distilled it for you here. Enjoy!

Motion

There were a lot of comments about mapping the ; key away from its default behavior:

;     Repeat latest f, t, F or T [count] times.

Indeed, ; seems pretty useful. I can recall having looped over a txn sequence to move to subsequent x’s on a line, and ; would’ve made that easier. I wrote previously that I had bound ; to :CtrlPBuffer, but now that might need to change.

Speaking of motion, a plugin brought to my attention was [EasyMotion][easymotion] which gives you a novel way of moving around using visual anchors. The trick is to keep your eye on the location where you want to move (my leader key is ,):

Animation from EasyMotion home page

I’ve installed EasyMotion and it seems neat, but after so many years of using Vim’s advanced movement keys, I’m don’t think that I’ll ever remember to use it. For example, if I needed to move to the second word three lines down, I would remember jjjww more quickly trying to remember incantation causes EasyMotion to appear (,, plus a motion command like w). EasyMotion would be more useful if a single key could bring up the interface for all points of text on the screen (like the Vimium plugin for Chrome lets you navigate to links on a web page).

Someone also mentioned vim-seek which “aims to make inline navigation effortless.” I don’t have time to learn of it and nobody I know has smashed down my door to tell me how life-changing it is, but I felt compelled to mention it here.

Leaders

A bit of discussion involved the leader key: an arbitrary key that plugins use for keybindings so that they don’t interfere with your own custom bindings. By default it’s \, but a lot of people remap it to , for similar reasons as the semicolon.

let mapleader = ","

A handful of comments on Reddit and Hacker News recommended changing it to the space bar which, unlike the ; and , keys, is redundant; it performs the motion as l and ).

" Note the required backslash.
let mapleader = "\<space>"

It’s also worth mentioning that some folks use folds so much that Space is bound to toggle folds:

nnoremap <space> za
vnoremap <space> zf

I tried this but it never caught on. I always felt that zo and zc were easier to remember.

Multiple Repeats

Macros are well known. Hit qa to record keystrokes to register a, hit q when you’re finished, hit @a to play the macro in register a and @@ to repeat the last macro.

But have you ever heard of :g//? I hadn’t. From the documentation:

                                  *:g* *:global* *E147* *E148*
:[range]g[lobal]/{pattern}/[cmd]
      Execute the Ex command [cmd] (default ":p") on the
      lines within [range] where {pattern} matches.

:[range]g[lobal]!/{pattern}/[cmd]
      Execute the Ex command [cmd] (default ":p") on the
      lines within [range] where {pattern} does NOT match.

...

To repeat a non-Ex command, you can use the ":normal" command:
  :g/pat/normal {commands}

How is this useful? You could emulate grep -v by deleting lines that match foo:

:g/foo/normal dd

Or you could append all lines matching foo to register a:

:g/foo/normal "Ay

Or you could change foo to bar only on comment lines that begin with a #:

:g/^\s*#/s/foo/bar/g

As a friend told me, “I only use it once a month, but I’m so glad I have it when I need it.”

Other People’s Code

Different projects may have different style conventions and I recommended keybindings for quickly changing whitespace settings among four common standards. Alternatively, there’ a plugin called “dirsettings” which reads .vimdir or .vimrc files in the current or parent directory(ies). Try the original script or check out the sequel which walks up the directory tree and sources all settings along the way.

If the only project settings you set are whitespace it might be worth looking into Tim Pope’s vim-sleuth plugin. It “adjusts shiftwidth and expandtab heuristically based on the current file, or, in the case the current file is new, blank, or otherwise insufficient, by looking at other files of the same type in the current and parent directories.” I’ve installed this and it seems to do a good job.

CSS Color

I mentioned at the bottom of the original post that I missed Emacs’ CSS color-highlighting mode, which highlights CSS colors the editor. A few people were quick to respond with Max Vasiliev’s vim-css-color plugin, which does this but causes a long delay when opening files. Then I was referred to Aristotle Pagaltzis’ fork which causes no delay but causes errors when opening files such as the stylesheet for this site and works fine. Hooray!

Image from ap/vim-css-color

Window Navigation

Multiple people mentioned binding Ctrl-direction to window-movement keys:

map <C-j> <C-W>j
map <C-k> <C-W>k
map <C-h> <C-W>h
map <C-l> <C-W>l

A friend goes further and appends Ctrl-W_. This maximizes the window’s height after the cursor has switched to it, which produces an interesting effect similar to some tiling window managers I’ve used:

map <C-J> <C-W>j<C-W>_
map <C-k> <C-W>k<C-W>_
map <C-h> <C-W>h<C-W>_
map <C-l> <C-W>l<C-W>_

Multiplexing

A lot of the forums discussed tmux, iTerm, screen. I’ll summarize.

GNU Screen has been the standard of terminal multiplexors for a long time. Most of the bugs have been fixed since its original release in 1987 but novel features are lacking, such as vertical window splits. Also, getting Unicode and 256 colors to work can be a battle.

vimscreen

tmux is the new hotness. It has vertical splits and great shell-scripting capabilities and much more. However, I’ve had an occasional crash and also experienced the keyboard slowness as described in this tmux vs. screen comparison, but it’s under heavy development and maybe I just need keep up with the latest versions.

vimtmux

iTerm2 is a replacement for Terminal.app on Mac OS X. It does horizontal and vertical splits, you can change font sizes in each pane (tmux and screen can’t do that, of course), and I think it “feels” faster than the built-in terminal application. I also recently notice that iTerm doesn’t have keybindings for directional pane navigation by default but you can add them in the preferences.

vimiterm

You can even integrate iTerm and tmux such that iTerm panes are mapped to tmux session panes. There’s a blog post illustrating how this works here.

Finally, someone mentioned vimux, a plugin which lets you “easily interact with tmux from vim.” The description says, “When you call RunVimTmuxCommand, vimux will create a 20% tall horizontal pane under your current tmux pane and execute a command in it without losing focus of vim.” Sounds useful, but I think I’ll stick with my Ctrl-Z solution I outlined in the original post.

Completion

The responses to the post highlighted a few plugins I hadn’t heard of.

Jedi looks awesome. If I were still writing Python daily I would definitely try it out:

Image from Jedi home page

There was a lot of discussion of YouCompleteMe, a powerful completion system for “C-family” (C/C++/ObjC/ObjC++) languages:

Animation from YouCompleteMe home page

A lot of people swear by Cscope, “a very powerful interface allowing you to easily navigate C-like code files.” You can read about integrating it here on the Vim wiki.

Eclim deserves a mention. By using Eclipse as a backend it brings a lot of features to Vim such as code completion, refactoring, generation, and whatnot. I’ve only gotten it to work once in my life and I didn’t have a positive experience. Besides, if you’re writing Java, the winner is IntelliJ IDEA — it’s got a Vim emulation plugin, too.

Finally, everyone knows about Ctrl-N and Ctrl-P for basic completion. But did you know about Ctrl-XCtrl-L for whole-line completion? Give it a try. (Credit: (@technicaldebtor)[https://twitter.com/technicaldebtor].)

Plugins

If you do want to install plugins, I previously recommended Pathogen, but there was so much response to use Vundle that it’s worth a discussion. What’s the difference?

Pathogen and Vundle are not the same thing: Pathogen could be called a “runtimepath manager” while Vundle is a “plugin manager”. The difference is huge:

  • Pathogen’s only purpose is to “inject” the directories located in ~/.vim/bundle into Vim’s runtimepath and make sure the documentation for all those plugins is correctly indexed. How they end up there and whether they are up-to-date or not is not Pathogen’s business.
  • Vundle, in addition to what Pathogen does, tries to deal with everything else plugin-related: installating, removing, disabling, updating…

So both plugins serve different purposes and solve different problems.

I don’t like to have my plugins scattered around my ~/.vim directory but I don’t need any help for updating or installing them. That’s not something I do often enough that I need a plugin to automatize it. Pathogen is all I need/want and it does its job perfectly. —a comment on Reddit

Vundle uses the service at vim-scripts.org (which I had never seen before) to find and install packages into ~/.vim/bundle/, which seems great. It even has a search mechanism and interactive installer. However, at the time of this writing, it’s still very new software.

I tried to replace my own management system with Vundle and it was not a good experience. I added the Bundle statements to my vimrc per the instructions but I was a little alarmed; to my knowledge, the ancient Vim script registry has no index or API, and I was uncertain what would be installed when I requested to install an unqualified, non-GitHub package like Zenburn. It was only after reading the source code that I discovered the new vim-scripts.org site which appears to be the source of the plugins.

After adding Bundle I ran the vim +BundleInstall command which dropped me into a weird interactive installer. It took a minute to realize that I was supposed to press i on each line to install the module. Then, trying to install half of the modules from GitHub resulted in GitHub asking me for a username and password (which is weird) and the installs failed with an error. Eventually I got everything installed (or so I think) and things seemed to work, but I wasn’t reassured. Maybe I’m just too dumb to know how to use it.

Vundle is promising, but it’s got a ways to go before I can call it a solid package manager that’s on the tier of cpan, gem, apt, npm or pip. And until then I’ll stick with my trusty Pathogen-based management system.

Meta-Discussion

Maybe you don’t want to spend any time configuring your editor and you want all the whiz-bang included. If so, check out spf13-vim, which comes with all of the good plugins mentioned here and before and colors and everything. There’s also Janus but it’s less impressive. I still think the right path is to start with an unconfigured, stock Vim distribution, but to each their own. Some seem to reject configuration outright:

“Every time I read an article like this, it’s how a power user installs an array of plugins and customizations to really soup up Vim, which to me kind of misses the point. If you want that level whiz-bang, use Coda or Sublime Text 2 or Eclipse or whatever.” —Hacker News

“I’ve seen one too many love-letter-to-Vim blog posts where the author recommends a flashy plugin that does nothing to really improve productivity. You know what improves productivity? Mastering motions and operators.” —“Stop the Vim Configuration Madness”

Vim has a lot of intelligent stuff in its default distribution, but the authors couldn’t think of everything, hence plugins. Compare visual block mode with numbered ranges of lines — of course you should be able to select a block of text with the cursor instead of esoterically entering line numbers. Compare :e foo.txt with fast, fuzzy filename file-finding — of course you should be able to jump to any file by hitting a key and typing any part of its name. Sometimes plugins really do help.

Vim is toolbox — it doesn’t come with everything you’ll ever need; it grows slowly over years. Sometimes eleven of them.