torsdag 3 februari 2011

Coming Home to Emacs

Some background

About six months ago I decided to switch editors from my old workhorse Komodo Edit to something a bit more powerful: Emacs. I don't remember exactly what triggered this change of heart. Maybe it was the fact that I was planning to give Clojure a shot, having installed ClojureBox a long time ago but never bothering to start it. Or maybe it was Steve Yegge's excellent blog post about why everybody should use Emacs. Because, you know, he works for Google so he should know what he's talking about, right?

The first week of using Emacs I was averaging about ten words a minute; between consulting Emacs cheat sheets and lying paralyzed on the floor from RSI-induced pain I didn't have much time left for actual typing. However, as time passed, I became more and more proficient using the keyboard. I remapped my Caps Lock key to Ctrl, installed a nice color theme and fell in love with org mode.

After about a month of Emacs usage my touchpad had started collecting dust and I was happily hammering away at my keyboard, confident that I had at last found the one true editor.

Enter Vim

In case you didn't know, there's a popular alternative to Emacs called Vim. There has been some debate online about which is the better editor, so in the interst of fairness I decided to try them both.

So, I downloaded Vim, got some plug-ins and went cold turkey. I decided I would use Vim for at least a month before I went back to Emacs. However, my stay in Vim-land became much longer than I had expected.

Emacs, while different from most GUI-based editors, doesn't look that unusual at first sight. The first stumbling block for Emacs novices usually doesn't come until they try to save their file by hitting Ctrl-S. Upon failing, there is usually some swearing, followed by even more cursing when they realize that saving is accomplished by entering the combination Ctrl-X-S. Informing them that releasing Ctrl before typing the S results in a different command usually doesn't help. And if you are really unlucky you'll tell them that Ctrl-X U undos, they try it but forget to release Ctrl, and get molested by a prompt that asks them strange questions about something called "uppercase-region". But I digress.

Vim, on the other hand, behaves very differently from GUI-based editors. Incredible as it may seem, it bests Emacs in the grief and confusion department by perplexing beginners from their very first keystroke. Typing in Vim seems to do nothing, or produces completely unexpected results. This is because Vim uses a very different conceptual model - modal editing.

Modal Editing

The basic idea behind modal editing is that entering characters is just one of many ways of manipulating text, which is especially true when coding. Following this line of reasoning, Vim is controlled primarily with the character keys on the keyboard, without the need for using modifier keys like Ctrl and Alt.

As an example, hitting the "j" key in Vim will not produce a "j" on the screen, but will instead move the cursor one line down. To insert text, one must enter Vim's insert mode, and then go back to normal mode to access features like moving around in the document, searching, replacing, etcetera.

This way of editing might seem terribly unwieldy at first, but it is incredible powerful once you get used to it. For example, if I want to swap the position of two lines I can just go "ddp" and be done with it. Or, lets say that I want to copy the text inside a pair of square brackets - "yi[" does it.

It was this incredbile editing power that got me hooked on Vim; at last, I thought, I had found the one true editor.

Coming home to Emacs

A couple of weeks ago me and some other interns at the company where I'm doing my thesis were tasked with making a game in a week. It was a fun challenge and a nice break from my thesis work. However, we had decided on targeting Android, which meant we would have to write the game in Java, something I was less enthusiastic about. I was even less enthusiastic about Eclipse's support for keyboard-only editing. Don't get me wrong, I think Eclipse is a very good IDE, but it's really not designed for use without a mouse. The closest I could get to a reasonable keyboard-only workflow was to use Eclipse's built-in Emacs key bindings. I still had to use the mouse now and then, but it was sufficient for the time being.

Having finished the project I realized the Emacs way of text editing wasn't half as bad as I had remembered it. Sure, it felt a bit uncomfortable having to hammer that Control key as if I was telegraphing my source code, but I could live with it. So I said to myself, "Hey, why not give Emacs another shot?". So I did, and it felt a bit like coming home to a warm cup of cocoa.

Emacs VS Vim

In my opinon there are several areas in which Emacs trumps Vim and vice versa. The great shame here is that many of the great features of Emacs are orthogonal to the ones in Vim, which means that it wouldn't be impossible to combine them to make one beast of an editor. There have been attempts to do this, but none of the flavours I've tried has stuck with me the way pure, undistilled Emacs or Vim did.

Emacs and usability

What really struck me when I came back to Emacs was that it felt a lot more usable than Vim. Take buffer switching, for example. In Vim, if you want to jump to a buffer but you forgot its name (or maybe you just restarted Vim and don't remember whether you opened the file or not) you have to start typing the name and see if it will tab complete. Or you can list all the open buffers with "ls", then switch buffers.

In Emacs, the buffer switching command lists all buffers, and will highlight them as you type, indicating which buffer it will switch to if you hit Enter. By default, hitting Enter without typing anything will switch back to the last visited buffer, which is often the behaviour you want.

Another example is Emacs' incremental search feature: Typing in the incremental search prompt will highlight all matches in the buffer, with the next match in a different colour. This makes it easier to see where you can jump to, and exactly how many more characters you need to type.

There are more small features like this (for example how emacs defaults to the directory of the current buffer when you open a file), that might seem insignificant by themselves, but they add up to a very pleasurable user experience.

Emacs and utility

Emacs comes with batteries included. You get a "scratch" buffer for jotting down notes you might want to throw away later. You get an email client, a file browser and if you install OrgMode you get a really cool task management program - all within Emacs. Why would you want to do all of this inside your editor? Because all of these applications are text-driven by nature, and Emacs is made for manipulating text. This is the origin of the phrase "Emacs is a great operating system – the only thing it lacks is a decent editor".

Emacs is also a fully-featured LISP environment, which is the source of much of its power and flexibility. It is fully programmable, and the tight integration with LISP makes it easy to extend or expand upon Emacs' functionality.

Emacs and flexibility

When I first tried out editing Python in Vim I was pretty disappointed---line breaks in function calls wouldn't wrap to PEP8 standards, there was no way to toggle comments, sometimes it wouldn't indent blocks properly, etcetera. Of course, there are plugins to address these issues, and others, but in Emacs it just works, right out of the box.


The problem with this whole Emacs vs Vim argument is that both editors are so good. If one of them were crap, its followers would (hopefully) realize the error of their ways. This also leads to a condition known as "editor envy" where Vim users envy Emacs users for things like OrgMode, and Emacs users envy Vim users because their hands haven't turned into claw-like appendages from overuse of modifier keys.

And so, the fervor with which these two camps defend their editors of choice does not stem from a deep inner conviction that they are right; rather, it comes from a nagging doubt that says they might be wrong.

Ironically enough, after writing this blog post and using Emacs at my internship for a week, I started to develop pains in my arms and wrist---the bane of all Emacs users. So, since my plans of switching to Emacs have been foiled it's back to Vim again!

17 kommentarer:

  1. The main reason to learn Vi/Vim is that if you are plunked down onto a random Unix box someplace, you'll alway have vi on hand to edit with. This has happened to me. A little vi knowledge goes a long way.

  2. Try sticky keys, it's way easier on your wrists. It lets you hit Ctrl, then the key rather than the two together.

  3. Set up your Caps Lock key to act as Control. Much easier on the wrists.

  4. Every keypress in emacs triggers a function call. Letter keys trigger self-insert-command by default, but it is straightforward to make them act like they do in vi ( Assigning a chord to turn viper-mode on and off gives you the best of both worlds and makes emacs the best editor for your needs.

  5. Agreed. Xmodmap your pains away. Hopefully you're not on windows.

  6. I'd like to mention that Emacs/Vim is a largely unnecessary argument. They are both so powerful now that one can become the other most of the time.

    Also, some of your complaints about vim hold weight in that emacs does some of these things better "by default." However, a lot of the preferences you have can be actualized in vim via configuration. For example, vim can absolutely do incremental search with highlighting.

  7. You don't necessarily need to use Emacs/Vim to have a 100% keyboard driven *and* usable editor.

    I use Kate, plus this little LD_PRELOAD hack:
    (note: Dvorak keyconfig)

    I can move my cursor with ALT+jkli (left, down, right, up), ALT+qa (page up, page down), ALT+uo (home, end), ALT+h (backspace), so I effectively don't have to move my hands off the "main keyboard" to edit the text. I also have a handy file switcher on CTRL+1 which works just like the one you've described for Emacs. I once wanted to try to switch to Emacs/Vim, but now - I don't think I will gain anything at all by that, as I have no idea how I can improve my editing experience anymore. (And if I'd knew I would just either extend my LD_PRELOAD hack or extend Kate itself instead of spending a month to learn a dinosaur editor.)

  8. :b shows all my open buffers without typing any part of any buffer names. Maybe you just haven't turned on wildmenu

    vim also has incsearch for incremental searchmatching. It doesn't highlight the "next" word differently though.

    Turn on autochdir to make vim "change the current working directory whenever you open a file, switch buffers, delete a buffer or open/close a window."

    Install the nerdcommenter plugin to easily comment/uncomment lines in nearly any language.

    The biggest advantage I have heard of in favor of emacs is that you can probably write slightly better plugins for it.

  9. Tried ErgoEmacs? No pain for your wrists, but completly rebinds all key mappings

  10. "I was even less enthusiastic about Eclipse's support for keyboard-only editing. "

    Have you tried Eclipe's Quick Access (bound to Ctrl-3)?

    It allows you to quickly run any command, switch to any view, edit any preference, etc. Just Ctrl-3, and type what you're looking for. It also shows the keyboard shortcuts for the commands, so you can directly use the shortcut later.

  11. Your journey is incomplete.

    You now need to go back to Vim, and see if it also feels like a cup of cocoa.

  12. Den här kommentaren har tagits bort av skribenten.

  13. I have enabled incsearch and highlighting, but I still can't get it to highlight ALL matches as I type. When I've entered my search, it highlights all matches, but during typing it will only highlight (and jump to) the next match. Am I doing something wrong? Here's the lines I'm using in my .vimrc:

    set incsearch
    set showmatch
    set hlsearch

    Samu: Thanks for the tip! Now directory switching works the way I want :)

    Corrie: Coming back to Vim feels more like being reinitiated into a secret ninja temple where you are forced to tread the path of shadows until you have acquired superhuman speed and agility.

  14. The injury from typing thing is more related to bad keyboard posture than editor.

    I type more than anyone I know, and I've been a software developer and writer non-stop since 1990, yet I've never even been sore, even after epic days of typing - I've sat at the keyboard for as long as 8-10 hours at a stretch.

    The #1 way to prevent strain and/or injury is to be sure that you use proper forearm posture. Your forearm should be in a straight line with your wrist and hand - a good side view of this is here:

    From above it should be the same. And this is why the Windows-style "Caps Lock to the left of the A" key is so bad for you - Caps Lock is used very rarely compared to CTRL, so you are repeatedly twisting your wrist to use CTRL. Swap the two, and CTRL turns into a slight movement of the pinky.

    Anyways, that's my 2 cents :)

  15. Fuzzy finder is more than you need for buffer management =>

    it does fuzzy matching, so if your filename is foobar_funky.c hitting ,fb+ky.c+Enter would open that buffer (,fb is my keybinding to open fuzzy buffer finder). You can checkout my complete .vimrc with plugins here There are lots of great .vimrc files on github. VIM has some of the most powerful plugins. Checkout this baby => for instance.

  16. "Ctrl-X-S"?
    And people wonder why Windows rules...

  17. vi (vim is an implementation of vi) does not have an "insert mode". It has an "insert command". The distinction is subtle but important. Type "3a1234567890^[" to see the difference.

    Also, try nvi as well as vim.