coming to terms with vim

March 24th, 2007

Preface

In case you didn't already know this.. the choice of editor to a geek means more than the choice of religion to the average person. Flame wars over editors are legendary and go back decades. The Unix community split into two main currents, each following one special editor. As you would have it, these two currents are about as black and white as you can find, exactly as the two editors also are: vi and emacs. They are on either extreme of the spectrum. emacs (1975) was written by Richard Stallman and friends, and aims to be everything. It can integrate with lots of other applications, shells, you can run everything *inside* emacs, never having to leave the editor! vi (1976) aims to be nothing, it is the anti-thesis, written by a sadistic bastard called Bill Joy. :D It is as cut down, crippled and bare bones of an editor as you couldn't imagine. And so the wars have waged, emacs vs vi.

Both editors are super popular to this day. You will find vi on *every* Unix system. If you have to edit a config file on some ancient machine and you find out your editor isn't installed, eventually you'll have to fall back on vi. emacs isn't always included, because of its size. Depending on how heavy the install is, it can easily be 60mb, which for an editor, on a scaled down system, is unacceptable. vi is about 600kb.

That is not the whole story, however. A pragmatic Dutchman called Bram Moolenaar decided to liberate the world from vi and came up with vim (1991): Vi IMproved. Now, ironically perhaps, vim is much more of an emacs than a vi, it has a gazillion features. Still, it's much lighter, and compared to emacs, still lightweight. With all its extensions and plug ins, it's hard to call it a lightweight editor in absolute terms, because there are lots of smaller ones, like nano.

Choosing an editor

Since I started out with Linux around the turn of the millennium, and not in the 80s, I don't have such strong feelings about vi and emacs. I could care less. I'm happy to use kate most of the time. It's a very well designed editor, easy to use, intuitive, pleasant. But since I'm hacking Haskell a lot these days, and Haskell is just esoteric enough to not be supported by anyone, I've come to realize that, indeed, it's a choice between emacs and vim.

I'm told there is a Haskell mode for emacs, and probably quite a good one, because it's advertised by the Haskell people. On the other hand, if you search long enough, you might just find some support for vim stashed away in a corner. Syntax highlighting isn't the problem, everyone can do that. But Haskell's indentation is hell, and the vim plugin makes it a little better. I've seen Haskell mode for emacs in action, and that too is rather imperfect.

I reject emacs on the same grounds as every vim user in the world. The key bindings are just horrible. Ctrl+X Ctrl+S to save the file, Ctrl+X Ctrl+C to quit. That's already painful if you do it once, it's unbearable if you do it while hacking, every two minutes. In addition, I work in X all the time, and emacs' gui is just awful. Whether it's gnu emacs or xemacs, they both stink.

UPDATE: Last night I saw a note about emacs-cvs having a completely new gui, so perhaps that is something worth exploring at some point.

So the choice falls on vim
vim is not the guy who approaches you with a smile and an outstretched hand. It's the guy sitting in his cubicle consumed in his work and mumbling to himself. What originally got me interested in vim was not the strong endorsements from lots of people I had talked to it, it was actually this blog entry from a guy who's selling vim integration plug ins for things like Visual Studio. There were two things about his text that resonated with me. First of all, if anyone is going to be looking for vim plug ins for probably the most popular IDE out there (ie. lots of people are already very happy with this IDE), there must be something pretty special about vim. And secondly, he loves vim because it's ergonomic. Hah, ergonomic, imagine that! Have you ever tried editing away from your desk? It's awful, you don't know what to do with the mouse, the mousepad sucks, you can't use the desktop normally, bleh. But he said vim works just as well on the train as it does on your desk. That got me thinking.

So does it? I don't know. You see, vim is a lot to handle. It's like having a particle accelerator dropped in your lap. (And it's just as heavy too!) Yes, there's a manual, but by the time you read it and grasp the meaning, you'll be an old man. vim's premise is radical to everyone who uses the mouse to edit. You don't need the mouse, don't touch the mouse, you can do everything from the keyboard. I'm still not comfortable with h,j,k,l, I cling to the arrow keys. I try to force myself to use k instead of arrow-up, but it feels so unnatural. vim's gui isn't much to brag about either, it looks better, but it doesn't exactly do much for you gui wise.

And, of course, the number of key combinations to remember is completely overwhelming. You just can't learn vim, you have to take it one day at a time. In fact, that's what Moolenaar says too - today you learn something that helps you today, tomorrow you learn something that helps you tomorrow. So I feel like the journey has just begun, I know almost all the keys on the cheat sheet now, plus a few more.

Configure or die

Unfortunately, the biggest drawback with vim (and probably emacs) is configuration. It can do almost everything, but that doesn't necessarily help me. I need to tell it what I need it to do. My .vimrc is now 67 lines and growing. And if there's one thing I truly dislike about complicated applications it's this - having to configure it down to the smallest detail. And having to carry your config everywhere you go, otherwise all the pain that went into setting up these options is lost once you get on a machine where you don't have your setup. KDE solves this problem now (at least for me) quite well, it's configurable to death, but the defaults are so good that most things I don't have to change (or even know about). Not so with vim, not only aren't the options listed, it's not always that easy to figure out what they mean.

Depending on the machine, the default vim config may even have vi compatibility set, so yes, your vim is reduced to vi, how terrifying! Why Bram decided to include a vi compatibility mode one can only guess, but one suspects it was so that he could argue that vi lovers could still breathe, meanwhile he would rescue the rest of us from that terror. I can imagine this would be the only way he could make a case to every distro out there to include vim by default.

So .vimrc is not exactly a walk in the park, it's quite a mess, and the more you want to configure it, the more muddled it becomes. The first thing I wanted to fix was saving the file. By default in vim, this is accomplished with an emacs like awkward sequence of <Esc> : w i. Yes, four key presses, just like emacs. I managed to map it to one key: <F1>. Out of all the commands I type in vim, it's the one I use the most and I can't stand that :w stuff, often typing :W by mistake, which gives an error. :lazy: If you want to know how that's done, look no further:

map! <F1> <ESC>:w<RETURN>a " insert mode
map <F1> :w<RETURN> " normal mode

Yes, at least the mapping is pretty obvious, you tell it exactly what you would normally type. It's just that there are a dozen different kinds of map command, apparently this map! is a way to get something working with insert mode. :/

But vim is not too bad, certain features make you realize that kate has weaknesses that the mouse can't solve. Like navigating word-for-word (instead of by character), deleting whole words, paragraphs.. generally it's things that have to do with moving in the file that are more sophisticated. To fix a typo in kate, you could either double click to select the word, delete and type it in - or just click at the incorrect character and replace it. In vim you hit w to move word-for-word until you come to it, then hit c w (change word) to erase it and you can type it in. That's quite convenient in code. To duplicate a line: Y p, in kate: Home Shift+End Ctrl+C End Return Ctrl+V. Depending on what you're doing, *a lot* more convenient.

Of course, I'm still trapped somewhere in between, so I'm seeing the flaws in mouse oriented editing, without knowing all about the vim approach, so it's still awkward. If I had to edit a file to save my life, I would pick nano. But hopefully vim does actually work as well as people say and I'll be fluent enough to hack plug ins and make it work well for me.

:: random entries in this category ::

6 Responses to "coming to terms with vim"

  1. Brian says:

    You won't regret learning vim.

    It's probably not a good idea to remap common things like :w. If you ever have to use vim away from your computer, you'll be stuck doing something you aren't used to, unless can take your mappings along with you. Vim keystrokes are sequential, not "chords" like emacs, so ESC :w or even ESC :wq isn't nearly as hard as an Emacs C-x C-s sequence. I can :wq so fast now I don't even notice. The "pound ESC with the left hand, type : with the right hand" combination is so routine you'll eventually do it by second nature. I'm with you on not using hjkl though; never could get used to that.

    Aside from being able to do common things fast (which you can in Vim) you can do things in Vim you can't do in most other editors at all. Look into Visual Block mode for example. And regular expressions. And piping text to external programs via :!. Also Vim's "windows" are wonderful to work in; better than tabs in my opinion, though Vim lets you use tabs if you want also. Lots of other stuff.

  2. numerodix says:

    Well just the fact that you type it quickly because you're used to it isn't really a strong argument. I want it to be something simple. I can still use :w but it takes me longer.

    As for regex, I find vim pretty awkward at that. In kate the search/replace dialog (with regex) could use a lot of polish, but that whole :s%/search/replace/g is longer than with sed! And already then I feel like I'm typing more than I should be, cause I barely know sed.

    Visual blocks are nice, but I don't currently see a purpose for them.

    And how do you pipe out of vim? I've only tried piping into it, eg. !!date

    Well the windows aren't very convincing. In fact I meant to mention this point in favor of kate, the tabs are wonderful to work with. gvim's tabs are not, and the windows (I assume you mean the splits) reduce screen space and I find it awkward to keep track of what is where.

  3. Graham says:

    Haha, you're hacking Haskell these days? Nice; I had to learn that in my first semester of my first year at university :)

    And yes, those keyboard 'shortcuts' in emacs are a total pain in the ass.

  4. Brian says:

    numerodix: If you want it to be something simple, you probably don't want Vim. : ) My only point is that Vim CAN be very fast to use, if you put effort into it. It's set up so that you can go as far as you're willing to go with it. No matter how good you are at Kate, there'll be a limit you'll hit someday as to how quickly you can point-and-click your way around. Probably took me over a year before I was Vimming at full speed, and I'm still learning more all the time. But simple it is not.

    I still have nightmares about Haskell.

  5. Jim says:

    I recommend shedding the arrow keys, and remember this commandment of vimming: I shall spend as much time as possible in _normal mode_, not insert mode.

    Think of as the key which means "I'm done inserting, now back to vimming". Get in the habit of hitting it, or using Ctrl-] if you prefer. (when you are in insert mode, remember ctrl-p and ctrl-n keyword completion)

    I use :wq to save and quit. or :w to save. Sometimes it is handy to save a file and quit all at once using the Shift-Z-Z.

    In vi compatability mode, using the arrow keys automatically moves you into normal mode. That should give you an idea, this input model was really designed for people to spend most of their time in normal mode, and that is where most of the power is, despite the continual improvements to insert mode.

    I use vim extensively, and i do very little configuring. A macro here and there, and i use some extra ftplugins with a few lines to set tab sizes and maybe add one or two macros. (I am also a haskeller, btw, so I commend you on your choice of editor and computer language ;) ).

    I'm very dependent on vim, its a pain at times when I try to use editors which aren't even vi-derivatives. But I am also addicted to many other fabulous must-have pieces of technology... refrigerators, plumbing, cars, microwaves, ...

  6. Hypocee says:

    I'm just a novice myself, but :x ([savingly] eXit) at least approximately equals :wq.