Monday, 18 May 2015

Emacs vs Vim

This is probably the oldest geek Holy War and I don't intend to join it. I just share my opinion and some (hopefully) objective reasoning why I ended up with Emacs.

From vim to Emacs

I used to be a vim user. Not a hard-core one, but I spent quite some time to configure my vim, setup some plugins and fixed a bug or two in a plugin when I needed it. Then recently I started learning Clojure. Because it is a Lisp dialect, Emacs is the most recommended and supported editor/IDE(?) for Clojure. Although by that time vim had an adequate Clojure plugin (vim-fireplace) I decided not to fight the community trends but give a go to Emacs. Even if that meant learning two (three, four?) new things at the same time: Clojure, Emacs, Emacs Lisp, ... It is not advised to put more obstacles in the way of learning, so I assessed the alternatives (Light Table, IntelliJ) but Emacs seemed the most beneficial to learn and Clojure is a good reason to learn Emacs the hard way.

I found Emacs a better tool than vim

I said "tool" not "editor". There is some truth in the old saying "Emacs is a great operating system, lacking only a decent editor." First thing first: Emacs is not a text editor. Emacs is a Lisp interpreter that runs whatever Emacs Lisp (elisp) programs are thrown at it. This is so much true that if you ask Emacs what function is executed when the right arrow key is pressed, the answer is:

<right> runs the command right-char,
which is an interactive compiled Lisp function.
It is bound to <right>.(right-char &optional N)
Move point N characters to the right
(to the left if N is negative).
...

As you can see even the cursor movements run elisp functions. And this leads me to one of the main reasons I prefer Emacs to Vim.

Code quality

With all respect to the maintainers of vim, but that code base - all written in C with amazing portability - is a beast: a very complex and hard-to-maintain code. I didn't read the source code deeply, but there is a project, Neovim, that started as a fork of vim and one of their first achievements were that they reduced 300k lines of "scary C89 code" to 170k lines. Not that surprised: vim has a function 400 lines long... to wait for keyboard input!

Emacs, on the other hand, has a core written in C. It is fundamentally just a Lisp interpreter and display management. For those who think that it must be a very complex code: Lisp interpreters are one of the simplest programs a student writes at the uni when they learn about compilers. All the other functionalities of Emacs are written in elisp. Including all the plugins. Yes, elisp is not the nicest Lisp out there, but it is WAY nicer than C or Vimscript.
"The Emacs thesis is that it is delightful to create composite programs based on an orthogonal kernel written in a low-level language together with a powerful, high-level extension language."

Plugins

Vimscript is the (primary) language of vim extensions. I have some experience with Vimscript and I can say that it is not a language I would like to write complex plugins in. It is a script language and in the good old days nobody really planned to extend vim with IDE capabilities. But in the recent years more demand raised to extend vim with complex features. Some plugins even cooperate with screen/tmux to meet modern expectations. Now, that is hacky!

And there is a huge philosophical advantage on the Emacs side: what other environments call "plugins" or "extensions" are here the part of Emacs. "Part" as in they are on the same level as the function that moves the cursor. I can't emphasise enough how amazing and powerful this is. No extension APIs, no restrictions, no limits.

The Emacs pinky

Yes, all those Ctrl/Meta(Alt)/Super key combos, they are almost a deal-breaker especially when compared to the fingers-on-the-home-row vim world. There are various workarounds for the Emacs pinky, some really strange ones. My solution is two-fold: I use evil mode, which incidentally is one of the best vim emulation out there. I also bind Cmd key (on Mac) to the Ctrl key of Emacs. The thumb is the most under-utilised finger during typing and this way I have a Ctrl on both sides in easy-to-reach distance.

Summary

I don't think emacs is the best text editor in the world. Far from that. But I would say it is the best open-source, extensible, multi-platform environment for coding. You can move to any OS, just get your configuration files, run Emacs and you are at home: never need to leave your familiar environment.