What I like about vim

October 27th, 2009

TextMate was the first editor I ever really got into. I had used other editors before, but it wasn’t until I read The Pragmatic Programmer that I learned I needed to be any good at using them. I happily used TextMate for a couple years until one day I decided I just couldn’t live without split panes anymore. That’s when I gave Emacs a try, and I liked it, so I’ve been using it for the past year or so.

I’ve always had a few minor gripes with emacs. My config always seems a bit finicky. And I have a tendency to mess around with code, and just run it, so I end up with a lot of unstable code running inside emacs. If I’m messing with keybindings or certain functions, I often end up with a pretty broken emacs, forcing me to restart. I love emacs’s smart indentation, but it doesn’t always get it right. Ruby method calls that span multiple lines don’t indent properly if I leave out the parens. It also doesn’t have the best support for non-standard syntax. For example, I have a coworker who is compelled to write “class <<self” instead of the more idiomatic “class << self” (don’t ask me why. I’ll change it and check in, and a later commit has it changed back. I have more important battles to fight). Emacs doesn’t seem to realize that they’re the same, and screws up the highlighting in the second case.

The indenting/highlighting I’m sure could be solved with a little bit of spelunking in ruby-mode.el. I’ve just never gotten around to it. The fact is, I don’t really want to spend any time digging around my editor’s code to figure out an issue like that. I want a powerful editor that “just works”. And to tell you the truth, I’ve never grown fond of elisp.

So all of this has made me consider taking a deeper look at vim. Frankly though, I didn’t have the time or motivation to do it. Then I went to Aloha on Rails (best conference EVER) and sat next to Tammer Saleh while he was working on his presentation. He was using MacVim and was FLYING. I honestly could not really keep up while watching him, there were just little bits of code shifting around the screen. I noticed he kept hitting escape all the time, which looked annoying, but I couldn’t get over the fact that he was moving way faster than I ever have on Emacs. Okay, I’ll give it a shot.

I’m on my first day of learning vim as I write this (yes, in vim). Here are my notes as I go along:

vim rewards me for knowing how to type

Most of the commands are done with one or two keystrokes. So far, very few require modifier keys. I didn’t realize what a boost this was. I already know how to touch-type pretty darn well, so just being able to get stuff done with a few simple characters is awesome. Using modifier keys feels natural to me (and I’m starting to think it’s because I’ve been raped by other editors all these years ;) but it’s definitely not easier.

vim rewards me for being logical

Commands can be built up through composition. In emacs, when I wanted to be able to duplicate a line, I had to write a new function for it and assign it to the keymap. In vim it’s just yyp – y to set it up to yank, y again to make it apply to the whole line, p to paste. I can easily figure out how to perform something on the next three words, lines, paragraphs, code blocks, current selection, whatever. There may be a faster way to do it sure, but if I can think of the building blocks necessary to achieve some result, I just put those blocks together and bam. I can do that with Emacs too, it just takes 20x characters.

. to repeat command

This is super sweet. Any time you run a command, you can repeat it just by pressing ‘.’ . I’m sure you can do this in emacs too, I just don’t know how :) Also, I doubt it would work the same way anyway, because vim commands are more than just a single function.

Macros

This is somewhat like repeating a command, but even more awesome. You press q[a-z] to start recording, run a bunch of commands, then q to stop recording. This saves a macro of all the commands you just did. Type @a and it will replay all those commands. Perfect for when you need to do something repetitive that’s too hard to do via regular expression search & replace.

Tabs & panes

I mentioned the reason I moved to Emacs in the first place was because I wanted split panes. It turns out that vim has them too, but on steroids. Or at least, exactly the way I wanted them. So to start, vim can have multiple tabs, just like TextMate, or Safari, or whatever. But you can create split panes within the tabs. Net result? I can have one tab for my User model, with the tab split into two panes – one for user.rb and one for user_spec.rb. SICK. The best way I’ve found to do this within emacs is to have multiple frames (= OS window) and do split panes in there. The thing is, I don’t like having multiple operating system windows open when working on a project. I like having one window per project, and navigating within that. vim gives me a better way to view my code than I originally even wanted!

tpope

I don’t actually have to know how to write vimscript. I just have to invoke the pope (so I’m told).

Editing is modal – who knew?

Let’s say you want to delete a paragraph of text. If this is the sort of thing you do frequently, you might have it bound to a key combo and know it by heart. If not, then you’re going to go to the beginning of the paragraph using the arrow keys, select all the text by using shift + arrow keys, and finally pressing delete. That is a fundamentally different way of interacting with the program than simply typing text. In short, you’re in a different mode. vim makes that mode explicit, and in doing so opens up some interesting possibilities. And of course, it also frees up the keyboard, so that the common commands become one keystroke. btw, you delete a paragraph in vim with dap. Um, sweet.

Now you may not agree with me that editing is modal. That’s fine. I don’t think it’s black and white. There are some small edges cases for me (such as immediately correcting a mistyped word), but I’m getting the hang of it and building muscle memory. For the most part though, the idea that editing is modal fits my brain & and usage pretty well.

Marks

vim lets you set marks within a document. This is nifty when you’re jumping around between three different methods in a source file. Set a mark, bounce around, and get back to your original spot with two keystrokes.

An editor for people and programmers

The main thing I like about vim so far is that it’s simple to do the things that I genuinely do all the time while editing text. Even the tutorials are more geared towards every day use it seems. I know that emacs can do everything that vim can (and more ;) but I have a tough time remembering everything that I want/need to do. Often if something is useful enough I’ll make a custom keybinding so that I can remember it easily…but then I’m away from standard keybindings which can be a pain. Plus, why aren’t some of these things “standard” to begin with (duplicating a line comes to mind). I would say that vim just ships with more sensible defaults for both basic and power editing.

Scripting in Ruby

Apparently you can write vim scripts in Ruby. According to Jamis, it works really well. I’ve only used it a little bit in modifying Jamis’s script a bit but I was surprised at how smooth the integration was. No headaches at all. Also, Ruby >>>>> elisp for my purposes.

ERB highlighting

This was something I could never get working very well in Emacs. I can’t tell you how many hours I spent fiddling with however many different modes. Just never seemed to work consistently. On my newer Mac I got the highlighting work, or at least it seemed so, but after 5 minutes of typing it would go sloooooooow. No clue what the heck happened. I’ve still been using TextMate to edit all my views because its ERB support is great. Turns out vim is good at it too. Nice.

Two weeks in – what I’ve learned

vim is sweet, but the learning curve is huge

Not a whole lot to report on this front. I’ve built up the muscle memory for some common commands that irritated me early on, and now I’m finding vim to be even nicer. What I have noticed is that because there’s so much stuff in vim, “if you don’t use it, you lose it.” Meaning that I have to periodically look up things that feel like they should be simple, because I just haven’t used them enough and committed them to memory. I’m making it a point to learn one or two new commands each day, and use the hell out of them.

Editor wars are stupid

I know TextMate better than most people I pair with that use it regularly. I’m proficient with emacs and now competent in vim. What this has done is made me truly appreciate the differences between them. I used TextMate yesterday for the first time in a long time (pairing), and was blown away by the command+t behavior. It’s nothing new to me of course, but after trying all the various copycats in emacs & vim, it’s clear that TM’s implementation is head-and-shoulders above the rest. vim and emacs both have their pluses and minuses. You can get a lot of stuff done in any editor. Arguing about which is better is a horrific waste of time.

This also means that if you respond to this saying that I’m stupid and misused/misunderstood Emacs, I’m going to delete your post and ban you from this blog forever. Not even joking. Save the petty crap for reddit.

Bottom line

I’m using vim for everything right now, which is something I couldn’t do with emacs (in particular erb files). Don’t see myself using emacs unless I’m doing lisp or haskell work (emacs has a really slick haskell-mode). But frankly those are uber thin edge cases for my usage.