23 May 2012

From Vim to Emacs

If you're reading this, you should probably have heard of either the Vim or the Emacs text editors. In that case, you should probably also have realized that one does not simply switch from Vim to Emacs or from Emacs to Vim, as each has a massive learning curve (an investment of time that one does not easily "throw away"), and there is a bit of a religious war over which is better.

Boromir

Both are absolutely incredible text editors (text editing is complex? Who knew?), with somewhat different approaches in achieving the same goal. What's the difference? How about saving a file?

  • Vim: :w
  • Emacs: C-x C-s

Simple enough. How about changing the line of text "The quick brown fox jumps over the lazy dog" to "The lazy dog jumps over the FOX!"?

  • Vim: ^wd2wdw$bbPd$^ep$bve~a!
  • Emacs: I have absolutely no idea, but I'm guessing the wrong answer is "just retype it to avoid RSI"

The question:

Why does that look like the most useless way in the world of editing text?

That's a fair question. The main counter-point being that if you do everything on the keyboard, you don't have to lift your hands to the mouse, click, drag, miss, backspace, type, click, drag. You just keep typing. All those squiggles and dollars and hats are just Vim's way of saying "move the mouse and click the cursor here". It's precise. The behaviour is exactly the same every single time, unlike a mouse and Window's weird way of "compensating" for your apparent lack of motor control.

This isn't a tutorial on Vim, but just to highlight that there is possibly still a shred of sanity in a modal, command-based editor, let's break that last "command" into chunks. It isn't actually a single command at all. Using Vim is kind of like constructing English, except the "tools" you're using aren't exactly in the Oxford English Dictionary. Actually, I changed my mind. Any attempt at explanation will just make it seem more insane than it already is. The point is, this method of editing is especially useful for editing code, and once you get "into" this way of editing, the speed gains are enormous.

So what about Emacs?

I'm going to be honest here. I didn't really switch to Emacs. The first thing I did was skip the tutorial on how to use Emacs's editing features, and jump to the part where they mention installing plugins. Then I installed Evil. As dasterdly as that sounds, Evil is actually just a plugin for Emacs that emulates Vim behaviour. It is apparently the successor to the old Vimperator plugin, so I used it without asking any questions.

Why would anyone switch to a new editor, only to try their best to make it replicate their old editor? Well, there are many reasons, but the ones most applicable to my situation are:

  • 2 years of Vim muscle memory
  • A lack of interest in learning Vim Script
  • Dodgy plugin support (before flaming, no, I'm not listing examples. Vim plugins just feel a bit "unpolished" to me)
  • SLIME, "a Emacs mode for Common Lisp development"
  • Clojure, "a dynamic programming language that targets the Java Virtual Machine"
  • Customizability
  • A new interest in Lisp

Actually, to be completely honest, I didn't even really switch from Vim. I do most of my development in Netbeans with a Vim editing plugin (I'm quite the Vim heretic, I must say). In my defense, one of the first things I install on a new linux box is Vim proper, and it's just fantastic when you use SSH. I prefer it as an editing paradigm rather than a core environment.

How painful is it?

As far as switching to a new environment goes, meh, not bad at all. The main justification is Lisp, which is a story for another day.

Actually, it's fun. I haven't hit any major snags yet (which I'm sure there are), but dayum, what other editor lets you tinker with Lisp while setting your colour theme?

A copy-pasted part of my config file that shows enabling paredit-mode when editing any Lisp files:

;; Use automatically with Lisp modes
(autoload 'paredit-mode "paredit"
      "Minor mode for pseudo-structurally editing Lisp code." t)
    (add-hook 'emacs-lisp-mode-hook       (lambda () (paredit-mode +1)))
    (add-hook 'lisp-mode-hook             (lambda () (paredit-mode +1)))
    (add-hook 'lisp-interaction-mode-hook (lambda () (paredit-mode +1)))
    (add-hook 'scheme-mode-hook           (lambda () (paredit-mode +1)))

Some modern general-purpose programming languages don't support Lambda functions (which isn't punishable by death, by any means), but here are lambdas in the damn config file. What fun :D

The reason for this is that Emacs itself is implemented in Emacs Lisp, a dialect of Lisp that was developed specially for this purpose. All configuration, plugins and extensions are also written in this format. Being a Lisp, it is enormously flexible, and this has led to Emacs getting described as "a great operating system, lacking only a decent editor" [source].

I'm not going to use Emacs as my web browser, but as far as learning Lisp goes, using a Lisp while setting up your Lisp environment is just great.

evil-mode does not work perfectly, though. Once you have internalized just how many escapes and keypresses get you into exactly what mode in Vim, any deviation becomes a bit frustrating. My reflex of hitting ESC to kill a badly-typed command kind of backfires. ESC is perfectly useful as part of an Emacs command, and commands are canceled using C-g. Then again, this is Emacs, where anything is possible, so I think a simple remap is in order.

I'm also really bad at pressing Alt with my left thumb. It makes my knuckle feel funny (too much clicking as a kid?). So I'm just going to ignore the fact that Emacs is an Editor as well as an Environment, at least for now.

How's it look so far?

I decided to go with my usual choice of the Solarized colour scheme created by Ethan Schoonover. It's absolutely amazing, and I'd really recommend giving it a shot.

Otherwise, the out-of-the-box defaults are pretty sane. Code indentation works really well without tweaking. Setting up tabs to expand to 4 spaces was really simple. Syntax highlighting is functioning.

Solarized

That might be pretty weak list of what's "going well" in an editor as powerful as Emacs. I think I'm going to try and discover useful plugins organically on an ad-hoc basis as I find myself repeating tasks too much for comfort. I'm also quite keen to do some manual customization in a language that's interesting to learn, rather than hack away at some Vim Script. I'll also see how much of the common behaviour I use in Netbeans can be easily (or not easily) plugged into Emacs. My guess so far is everything.

Just an aside, I probably could've hidden more of my traitorous tendencies and cropped out the Windows window border, but I think that from this article it is already clear that I am quite "international" when it comes to my software setups. I am planning on re-installing Arch Linux again at some stage.

Conclusion

So what was the point of all this? The folks over at gitvsmercurial sum up the idea quite well:

Who the **** cares? Use what YOU like, not what someone on the internet tells you to.

I wasn't such a "heavy" Vim user in the sense that my day-to-day workflow depended on a ton of plugins and custom scripts. That probably eased the transition quite a bit. I'll never give up Vim completely. It's damn useful and a damn fine piece of software. I don't think I'll ever stop programming use "Vim editing".

Either the religious editor zealots have way too much coffee in the mornings, or I just wasn't ever as into Vim as I thought I was (but then again, who's to judge anyway). My take on the matter is:

The switch really is possible. As long as you can bring Vim with you.

blog comments powered by Disqus