POSTS

Emacs v. vi is rooted in the love of Lisp

Blog

In the unix / linux world ( and thus, by extension the Mac OS X universe as well ) there is much to-do made about the choice of text editors.

Why Does It Matter

Much of the configuration of these various systems is centered around the generation / manipulation of text-based configuration files.

For example, if you’re on windows and you want to change your timezone you must go to Start Button / Control Panel / Time and Date / Timezone. On Unix / Linux / Mac OSX you simply edit a text file, change the word from PST to CDT ( or what have you ) save the file and you’re done. More keyboard, less mouse, quick, elegant, easy to find. Done. Thus from a pure system maintenance point of view, a tool that helps you edit text files is vitally important.

Why does it matter to developers, in particular

If you develop software on these platforms, the question of your editor becomes even more important. For source code, which is in turn compiled ( or interpreted ) to make programs, is nothing but text. A text editing environment that helps the programmer most efficiently wrangle (or mangle) text, that doesn’t “get in the way” and “helps out” in the wrangling process is to be loved, cherished, and extolled.

What do I mean by “help out”. For example, say I define a function called

listen_to_network_for_traffic()

Now, on the one hand, if I need to know what this function does, its long-ish name is very instructive: it listens to the network. On the other hand, if I want to use that function name:

  • I can type listen_to_network_for_traffic over and over again. This would lose charm very rapidly
  • I could find the original use of listen_to_network_for_traffic, copy it, and paste it where needed. In so doing I would lose my place in the code ( say i’m writing at line 50, I defined this at line 900, that’s a lot of page-up and page-down ).

In some editors typing the word listen and then hitting some key-stroke would “auto-suggest” the remaining _to_network_for_traffic() - see the editor is helping me produce the text faster.

Naturally there are many other optimizations that increase the prestige or love of one editor over another. Auto-suggest, plugins, syntax correction, etc. It is on the strengths and merits of these augmentations to the “flat” text editing process that developers find themselves comparing the superiority of editor A to editor B.

What does this means from the perspective on a non-technical reader, a real-world example

A non-technical reader, may think “Well what’s wrong with using Word to edit text and using find and replace?” Well, first Word runs on Windows in a graphics heavy framework and, as we mentioned, the software we’re developing is not for windows. Secondly, let’s say you, in Word, wanted to change all occurrences of “Steven” to “Steve” in a text file in word you would do Edit / Find / Steven / Steve / Find next / replace-yes replace-no / repeat.

In the vim text editor you would simply type:

:%s/Steven/Steve/gc (enter)

This translates to: Escape, to enter command mode ( the place where we do operations to the text, not the mode where we enter text itself ), %=in this whole file, substitute Steven for Steve, in all global occurrences on a given line, and then check with me if you want to do it**.

After running this command, you would confirm changes with Y or N and viola you’re done! That’s a lot less mousing and clicking, no? If you’re in the business of mangling code for a living, the ability to change all variables called foo to bar in a dozen keystrokes is a literal difference of hours in the life of a coding project.

The Holy War

The pragmatic introduction complete, let us return to the point of this post. There is a my-dad-can-beat-up-your-dad holy war between the adherents of ’emacs’ and the adherents of ‘vim’. ‘Vim’ users value the elegance of their command set: “5dd” means “turn off text entry mode and go to ‘command mode’ and delete five lines and store it to a temporary buffer I can paste from”.

Listen to the rustle of the ferns in the ancient forest, the trickle of water down rocks, this is the Zen elegance of vi. The Tao is cryptic, but quickly one comes to accept its power.

Emacs users see this as silly and think: why not Control-U 5 Control-K (do the Control-K operation, cut one line, 5 times), what’s up with that “change modes” thing?

MIT brand of thinking says: Define a function for a ‘universal’ n-many operator ( Control-U ), pass that an iteration count integer, have that perform the pre-defined global kill-line-of-text operation ( Control-K ). It’s rational, it builds from atomic concepts like the Tractatus Logico-Philosophicus.

As I say elsewhere, I’m Switzerland in this battle. I used emacs from 2001 - 2005 and have been using vim for about 18 months now. The reason I switched was because emacs is built around key combinations and they were getting to irritate my fingers ( even with my Dvorak + swap-keys keyboard layout ). And lo, the repetitive stress issue is much lower with vim.

Yet I still feel the power of emacs and know that it has mas mojo and here’s why. Emacs is not just a text editor, it has the power of one of the most powerful programming languages conceived running inside of it: LISP. The crucial take-away is that every Emacs action is actually an eLisp function: the simple act of hitting Backspace maps to a function called “delete-backwards-character”.

For the non-technical, suppose that I told you that by writing a certain “spell” on a piece of paper and saying “evaluate” you could change variables that define the behavior of reality instantly. That’s right, you could utter:

“Evaluate pull_of_gravity = 31.0m/s/s”!

And instantly some very bad things would start to happen to reality.

Now let’s try saying instead of “reality” you’re working in emacs, and instead of using the word “evaluate” you use a built-in emacs function called “eval”. You can do the same thing. You can, in effect, edit the rules and physics of the emacs environment, by saying to emacs “hey emacs, read this lisp file I wrote”.

Emacs is an amusing program written in a self-expanding, recursive language that is used to model AI. Earlier I mentioned that Backspace was bonded to “delete-backwards-char”. Say you were doing to be doing a lot of ‘delete three words’ operations. You could “modify your text editing reality” and tell emacs to bond your freshly written “delete_three_words” function to backspace. It would comply( much like your re-configuring gravity example earlier ).

Yet ‘delete_three_words’, handy as it may be, is absolutely trivial. The power of LISP is staggering, it’s so staggering in its complexity, beauty, and power that many programmers never even scratch the surface of its implications. With LISP, and emacs, you can re-build your universe from the fundamental concepts. delete_three_words is silly, why not parse a database hierarchy, insert the name of every column, capitalize it, insert a number in front of each, and wrap it in the HTML code for an unordered list and bond that to Control-X Control-D". With emacs, this is trivial as well! Why not tell emacs “instead of looking at files, look at activity on such-and-such network port”. A few other functions later you’ve turned your text editor into an AOL IM client.

Emacs, a text editor underneath a Holodeck, or a flexible reality framework

Perhaps now you might realize that emacs isn’t truly a text editor, although it acquits itself nicely in this regard, but it’s a flexible environment for modifying how it handles data you give it. It’s effectively an operating system masquerading as a text-editor that is written in a stunningly powerful programming language. As one uses Emacs / learns to edit eLisp, it becomes apparent that you’re meddling with the powers of the Gods. You become aware, over time, that you’re messing with the powers that could create amazing web applications or make SkyNet.

The more you edit, the more you become aware of this power and the more convinced you become that emacs is the ’true way’. And honestly, once you taste the waters from this fountain, you become intoxicated, amazed, addicted; and I know, it is intoxicating.

Vim responds…

The vim user would reply: we can do plugins, we can do extensions too! We have our own meta-syntax which will allow us to change what’s under the hood.

“Ah yes, grasshoppers”, could opine the Lisp guru using emacs, “but you have not understood, your plugin language can emulate and produce much, but it lacks the ability to do the truly amazing that emacs Lisp possesses, in part owing to the blub paradox.”

The love of lisp, the root of “Emacs v. Vi”

And so this love of Lisp produces emacs fans knowing the secrets of the recursive magic of Lisp being inured to criticism of the actual editing function of emacs. For what criticism could possibly assail a world in which you have the full power of rewriting your universe at your disposal ( as a refutation to the vim camp emacs actually has a mode, written in eLisp, which emulates the command set of vim )?

The vim users will counter, “But why must you work and agitate so hard with the editor ( they are mistaken, as we have discussed, it’s not “an editor” )? Why can’t it be simple, expressive, and uniform, with minimal gyration?” And most emacs users will admit, without their custom modifications to the emacs reality ( aka a user’s .emacs file ) emacs doesn’t work like they like it to, or like they expect, it’s much less portable.

And thus the holy war can never be resolved because the discussion isn’t really about editors at all. Emacs users argue, waxing poetic, in a LISP-intoxication of their editor’s merits when they should be proselytizing LISP while the vim community is discussing editors in themselves.

Personal Stance

So what do I use? It depends on the job. Perl = vi. Lisp = emacs. Anything on my Mac: TextMate.

Recently at my first meeting of the Silicon Valley Cocoaheads another attendee was talking about the excellence of the OSX editor TextMate and how it can be used to spit out a lot of code quickly. I believe that he said its macros were unsurpassed by any other editor. I saw my neighbor’s countenance get the unmistakable cloud of “yeah right” and I knew him for an emacs user on sight. He had apparently touched that same mystical heart that lurks within emacs ( there was even an mp3 a few years back called “God wrote the world in Lisp” ).

The speaker, when engaged on this point, then countered that the interface for generating such intelligence in TextMate was more accessible and I suppose that, in this, he isn’t wrong … but it’s not LISP ;) .

Closing

So that’s how an editor debate is really a ‘how do you conceive your world’. Do you want to have the keys to reality and build whatever you want? Or do you just want a low-footprint fast, uniform editor? Do you want your editor to fold and mutate into whatever you want, or do you want the reliability of your editor being mostly the same everywhere on every machine? Is Kant right, that there is a noumenal world beyond perception by sense data, or is the world Hegellian and full only of phenomena? Is it Great Taste or Less Filling?

The great thing about the world of ideas is that we can all explore all of them and find the one that works best for us.