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.

23 Responses to “What I like about vim”

  1. Avdi Says:

    It’s always hard to write a comment like this without coming off like a zealot. So let me start out by saying that I’m glad you’ve found an editor you love! Several of your criticisms are of Emacs are ones that I have as well. In particular, while it’s possible to get pretty decent indentation, highlighting, and ERB support in Emacs, it’s certainly not set up that way out of the box in any distribution I know of (not even Aquamacs). It’s not at all clear that, for instance, it’s usually a good idea to replace the dated ruby-mode.el that comes with it with a newer version.

    However, so many of the VIM features you cite are basic Emacs features as well that I question your characterisation of yourself as “proficient” with Emacs. For instance, I’m not sure how you can spend more than a month in Emacs without learning the commands for dividing a window into panes by heart. Likewise, if you’ve used it for a year without learning the commands to repeat a command, record and execute macros, and push/pop the mark stack I’d say it sounds like you never exerted the effort to learn Emacs which you clearly have exerted in learning VIM. Did you read the manual? Spend much time on EmacsWiki?

    This isn’t a plea to “come back to the light side” or anything like that. Frankly I’m happy to see more people using powerful, Open-Source, highly-extensible, cross-platform editors, whether it be VIM or Emacs. But It saddens me to see these “why X is better than Y” posts when a few moments of research would have revealed that Y is equivalent to X on many of the points. By all means, be happy with VIM! But don’t tear down Emacs for missing features which it very much supports.

  2. Pat Says:

    That’s exactly the sort of comment that I hate. “You don’t know how to do this thing in emacs, therefore you’re not qualified to comment on it.” But I don’t think that was your intention, and I like you anyway, so no ban! :)

    I know the window splitting features in emacs by heart. That’s what drove me to use emacs in the first place. vim has a better implementation in my opinion. Tabs + split panes, all under one neat OS window.

    It’s not that emacs doesn’t support some of the features I talked about – I assume it does, because it can do everything. Most of this is “I didn’t know I needed this until I used vim.” Not knowing how to repeat a command in emacs is my bad, but I never cared about it so much that I ever went looking for it. Same with marks.

    I’ve spent waaaaaaaaay less time and energy learning vim than I have emacs. I can’t count the hours I’ve spent on the emacs wiki, in the channel, and digging through source code on github. And yet following one tutorial on vim was enough to convert me. I think that’s sort of the point. For the stuff that I do on a daily basis, vim does it out of the box, everybody knows it by heart, and it’s right at my fingertips. Emacs, by contrast, always felt more like I was building the editor I wanted from the ground up.

    So I say I’m proficient in emacs because I’ve spent a lot of time hacking it, and have been effective with it. But being proficient in emacs is very different from being proficient in editing with emacs. And therein lies the difference, for me

  3. Avdi Says:

    Fair enough. I can see the argument of the features being in some way more obvious or less surprising or otherwise easier to find/use in Vim. Also, I actually have a lot of respect for people that prefer the modal-ness of Vim – I think the modal model makes a lot of sense. I prefer Emacs for other reasons, but Vim’s neatly complementary single-key commands make a lot of sense to me intellectually.

    I’m still kind of curious about the Tabs/panes thing though, because I honestly don’t get the distinction you are making. Granted I disable tabs in Emacs (I often have a hundred buffers open, and use ido-mode to navigate between them quickly), so I might be missing something about the interaction between Emacs “windows” (i.e. panes) and the tab bar which differs between Emacs and Vim. But I know that if I turn tabbar-mode on, I get split panes and tabs under one OS window. Would you mind expanding on the difference between the two editors in this regard?

  4. Avdi Says:

    Oh! I re-read your post and I think I get it now. Panes inside of tabs instead of tabs inside of panes. Kind of like virtual desktops. OK, yeah, I see where that would be nice. It exists for emacs, of course – I’ve used a couple of modes that make it possible to cycle through different pane-sets. But AFAIK it’s not been integrated with tabs. Nevermind, please ignore my obtuseness.

  5. Matthew Williams Says:

    Great write up. I’m in the process of becoming proficient in Emacs. Coming from TextMate (like most), I spent a lot of time with Emacs during my personal time to get myself as productive as I am with TextMate before I started using it when I’m on the clock at work. I’m on my second week of pure Emacs and I’m really enjoying it.

    I forgot who brought the idea up, but it was along the lines of spending 6 months in each editor and pick the winner. I still haven’t scratched the surface with Emacs and I think I have a pretty slick setup right now. I started with the Emacs Starter Kit from @technomancy and stripped it down to my own needs.

    I started with Emacs opposed to Vim because of watching guys like Jim Weirich (just like you watched Tammer). Oh, and the Emacs Peepcode video was great too! I too was at Aloha on Rails (I’m a local, great talk BTW) and I actually drove Jim around the Island that week and was able to pick his brain about Emacs and I learned a ton.

    One of these days I’m going to fire up MacVim and play around a bit. I’ve taken a peek at the Hashrocket VIM configs as well as the config @CoreyHaines is using and it’s all great stuff.

    Great writeup, I hope this post encourages others to spend a little bit of time to take a look at one or the other. At the very least, working with split buffers and having the ability to quickly jump to a model, test, controller, or view from any given point in a Rails app for example are just a few features that you can figure out quickly in a weekend and immediately increase your productivity.

    One question, is there a resource I can look at for test running inside of Vim? I’m cloudy on the subject a shell in Vim. Like with Emacs and Rinari, I can C-c ‘ t to run the test for the given file and that test runs in a new buffer with links back to the file and line where a test may have failed. What’s the Vim equivalent like?

    And finally, have you found any features in Vim that you miss from Emacs?

    Take it easy! @mwilliams (who just might be playing with MacVim tonight)

  6. Tammer Saleh Says:

    The funny thing is that this post convinced me not to attempt moving over to Emacs. I had toyed with it, mostly because of the absolutely terrible vim customization language. But really learning either of the powerful editors is more important than trying to hit that last 10% with the competition.

    I’ll also give the ruby bindings a looksee… could solve my issues.

  7. Harry Vangberg Says:

    “He kept hitting escape all the time, which looked annoying.”

    It is annoying. You’re bending your hand, drifting from the home row and what not. But the solution is really simple. Ctrl-c has the same effect as ESC. That in itself doesn’t make much of a change, at least not on a Mac, but that is where the trick comes in: remap that (stupid) Tabs Lock-key to act as Ctrl and you’re good to go.

  8. iain Says:

    Nice, another VIM user joins our ranks ;) (it’s too hard to resist)

    I’ve been using VIM for a few years now and I still learn new, sometimes even basic, commands every week.

    I have two tips to getting better at vim:

    1: take a look at some VIM screencasts by Derek Wyatt, he’s awesome. http://vimeo.com/6999927 2: print out the graphical cheat sheet and lay it next to your keyboard. You’ll pick up most commands soon enough. http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

    Good luck!

  9. John Bender Says:

    MacVIM was my editor of choice until I ran into erlang.

  10. David Alan Hjelle Says:

    “He kept hitting escape all the time, which looked annoying.”

    It is annoying. Rather than remapping Caps Lock to Ctrl and using Ctrl-C, I’d recommend remapping Caps Lock to Esc. This is really nice when using Vim, because your hands stay completely on the home row.

    Of course, this isn’t possible with Apple’s Keyboard Control Panel. You’ll have to use something like PCKeyboardHack http://www.pqrs.org/tekezo/macosx/keyremap4macbook/extra.html. Just assign Caps Lock to trigger keycode 53, and you are set.

  11. brad dunbar Says:

    Couldn’t agree more! Vim fulfills my needs without configuration files, out of the box, on any *nix system I stumble across, quickly and efficiently. I’ve used tons of editors and Vim is the only one that satisfies.

    By the way, if you hate context switching with Esc (who doesn’t? its way off the home keys) you can use Ctl-[ instead.

  12. David Mathers Says:

    You can also use q with numbers. I find it more convenient to use the keys right next to @ rather than letter keys.

    I didn’t know about dap. I thought dip deleted paragraphs. So I looked into it: dip (delete inner paragraph) deletes the paragraph itself while dap (delete a paragraph) deletes the paragraph plus the blank line beneath it. Thanks for the tip!

  13. Lucas Húngaro Says:

    Pat, what tutorial have you read?

  14. SJS Says:

    Editor wars are amusing when they’re good-natured ribbing between professionals (or at least the experienced). Vicious mud-slinging is tiresome and juvenile, and indicates the slinger’s opinions are worth ignoring.

    I routinely tease my emacs-using colleagues (and they me) for their editor choice — but I will also express admiration for some of the tools they have. I respect those who have mastered it. I’ve tried to learn Emacs several times, and failed miserably each time.

    Which is odd, really, considering that I had used microemacs as my primary editor for awhile. (Of course, that was before I had invested thousands of hours playing Rogue, Larn, and Nethack.)

    As for escape being hard to reach… I’ve never found it so. It’s at the corner of the keyboard, and is thus the easiest non-home-row key to find and hit without looking. Plus, you can type control-open-square-bracket “^[” if that’s easier for you.

    And finally, if you like to use IDEs and end up in Java-land, then you should know that NetBeans can use the jVi plugin to acquire much of the vi(m) goodness, which makes the IDE a LOT more palatable to touch-typists.

  15. Jason Says:

    I’ve used Emacs on and off for a long time, and never really tried Vim, beyond really basic usage.

    I think your point about the editor wars is right on though, personal preference should never be a substitute for honest inquiry.

    Anyway, I was just posting to say thanks, you’ve inspired me to take a closer look at Vim.

  16. wenbert Says:

    Hello,

    Do you mind sharing your .vimrc file? And what plugins do you use?

    I am also new to Vim.

    Thanks!

  17. the one who blogs Says:

    I personally like Emacs and use it for everything. The great thing in emacs you can mold it to fit your style. Nothing is really hardcoded, so you can fix everything you don’t like.

    Textmate and such are for people who want to use the defaults of their editor, so they adapt to the editor’s way of doing things. Emacs on the other hand is for people who want their editor to adapt to their needs.

    These are two schools of philosophy really.

    BTW, complex extensions are also big pluses for Eamcs. E.g, if you don’t know orgmode I suggest checking it out. It’d keep me with Emacs even if I wanted to leave which I don’t:

    http://orgmode.org/

  18. Kevin English Says:

    Hey Pat, Netbeans has a cool vi plugin called JVI (http://jvi.sourceforge.net/). To me this is the best of both world because Netbeans is pretty cool Rails IDE.

    –Honolulu hacker. kenglish

  19. Bucciarati Says:

    “immediately correcting a mistyped word”

    You can use ^W in insert mode

    :help i_^W

  20. Pierre Says:

    I have been switching between Vim and Emacs for a year now. Yet I have not made my mind … Sure, Emacs is more powerful but Vim is lighter.

    In my case, I prefer to let my tiling window manager caring about windows. So I only want to use both on terminal mode.

    I love Vim but I have lot of difficulties with the dual mode and always forget to switch from one to the other … Emacs key binding does not feel so alright to me.

    I wish I found the perfect editor one day.

  21. Ambv Says:

    Hello, happy to see another vi ninja. What David Alan Hjelle said above is not fully true.

    While CTRL+C does work most of the time, it’s not EXACTLY like ESC because all it actually commands the editor is to “leave the INSERT mode”. This is no problem while editing plain text but if you have some fancy plug-ins using the “InsertLeave” event, you will find soon enough that CTRL+C does not trigger them.

    CTRL+[ is ESC.

    David is also wrong about Mac OS X not letting you remap CapsLock to Ctrl. It might have been true once but at least since Leopard (haven’t checked older versions) you’re able to choose your mapping right in the Keyboard Preferences pane.

    And one more thing, the modal nature of Vim comes in handy when you realize it remembers all your modal commands and for the crazy combo you did 5 minutes ago, all you have to do is simply recall it using the Up key while in edit mode. Moreover, typing a bit of the command’s beginning filters the history and I use this feature quite often.

  22. Mark Wilden Says:

    Ah, one of my favorite topics. :) I’ll just spew somewhat randomly in response to Pat’s post and replies.

    I simply do not understand why anyone would like TextMate if they knew how to use Vim, emacs, or even Visual Studio. Some of my reasons are at http://mwilden.blogspot.com/2008/03/textmate-is-worst-program-that-people.html.

    Instead of yy, you can type Y.

    rails.vim is awesome, but don’t feel you have to jump into it right away. But when you do, entering :AS to open the spec for the current file in a split window will be fun (I contributed a bit to this functionality).

    I didn’t know about dap until about six months ago. I wrote my own version of vi in 1985, and I would still learn new capabilities months later.

    Ctrl-W Ctrl-^ opens the prior file you were editing in a split window.

    You can set marks in other files by using capital letters: mA, e.g.

    Learning one or two commands a day is absolutely the best way to learn Vim.

    TextMate’s Cmd-T is awesome, but check out Jamis’s Vim equivalent.

    I use q for macros, so to start, I just type qq. After you’ve run a macro with @q, you can hit @@ to run it again. If you want to run it 200 times (e.g., on the rest of the file), type 200@@.

    I’m about to start work at a company where they pair program all the time, so I’m going to have to give up Vim, unfortunately. They use RubyMine, which has lots and lots of useful Ruby/Rails features out of the box. No vi emulation mode, though.

  23. SteveC Says:

    Editor wars are stupid. That’s why I made a stupid video game about them: wordwarvi.sourceforge.net (runs on linux mainly, but also macs, though without joystick support on mac.)

Leave a Reply