Vi Vs Emacs

Don’t care about the preamble? Head for the meat.

Preface

Almost a month ago, Iambe and I were talking and I casually mentioned that I was reading a book on learning Emacs. Subsequently, she wrote this column about me and my choice of editor. I took offense to this, in a humorous way, and decided to go and give them both a good try out to see how they were. Another person I knew was also on the Vi(m) side, and unable to see the merit in learning another Editor, quoting from the Bible of Unix, preaching of small tools all working together in unison, and of which editor was installed on 99.9% of all UNIX’s out there.

Emacs interested me though. Don’t get me wrong, vim (hereafter known as vi) was what I have used as an editor for the last 5+ years in Linux and Unix, but the hugeness and complexity of Emacs fascinated me, and the ability to learn such a tool would be a great skill I thought. Comes with the territory of being a “power user” I guess.

Preliminary Conclusions

After Iambe’s Column, there was a lot of email that was sent to me, and talk on talkback hit 45 comments. I promised to summarize here, and will do just that before I start talking about my views and experiences since this “challenge” was inadvertently made.

Stats

Talkback Responses

Vi(m) (X)Emacs Other Fence
Sitters
0 4 1 8

I admit I was kinda surprised by these responses. On one hand, I expected a slew of “VI sucks!”“Emacs sucks!” comments, which there were not. The comments that were there were more of a “Stay on the good side” or “good idea trying it out, you’ll like it” variety. This is good. Kinda disappointing for the purposes of this web page though.

Email Responses

Vi(m) (X)Emacs
4 10

The email side of things was more like how I’d expected it. A couple of don’t do it! in there, but generally either arguments (vi is on every system/emacs is more user friendly) or congratulations for learning a new tool, or noting that it’s all worth it after the learning curve.

Again, these are my thoughts as well.

Who Cares? Get to the meat!!

So you are probably wondering if I’m now a Emacs user, a vi user, a pico user, or if I’ve given up on editors all together and am doing everything via echo(1).

Well, I decided thanks in part to everyone telling me not to, Emacs. I have been forcing myself for the last few weeks to learn it (thanks to my O’Reilly book) and use it wherever I can. I still find myself using vi for quick and dirty little tasks like editing crontab or something, but I’m forcing myself more and more to use emacs when I can.

When I initially read about it they said it was an “environment” that you worked in, and that you never had to leave. More and more I’m realizing this. What do you use in a normal day of hacking code (me anyway)? Well, an editor for your work, xterms/rxvts for doing maintenance, a browser, email client, chat client, icq client, quake (of course). But that’s basically it right? Most things I do in a day can be done with those few things.

Details

So what does emacs provide to do these things? There is a calendar/diary system (though a little simplistic for my tastes, perfectly fine for jotting down appointments and so on, shell access, so you never have to pop up and rxvt to launch programs. There are a few emulation problems inside the emacs shell, but that’s easily cured when I get back to the section in the book that addresses those issues.

For file maintenance there is “dired”, the program that does directory maintenance. You can go through a list of files and hit “d” to mark for deletion, or “~” to tag all <filename~> files, or “#” for all emacs backup files etc. Then “x” will actually execute the events you’ve tagged to happen. I’ve only begun to play with this, but it’s pretty powerful.

Bookmarks are really neat too. Instead of typing in “C-x C-f /arcterex@ufies.org:/var/www/index.html” I can just type in “C-x r b ufies” and my “ufies” bookmark will come up. Nice shortcuts to actually typing in the complete filenames for commonly used files!

The shell mode is accessed with “M-x shell” (alt/meta+x, then type in shell). It also has some cool features. Unlike a normal shell, it’s just another file buffer, so you can move all around it to cut/copy information from shell command output. Executing a command, then moving back “up” the shell output to the command and pressing enter on it will let you execute the same command again. There are also keys to hit to remove output from commands, and do other neat things.

Multiple windows are nice. Once you get the hang of it they are nice and easy to work with. C-x 0 (ctrl-0 (zero)) to remove a window, C-x 1 to expand a window to full screen, C-x 2 to split a buffer window in 2, or C-x 3 to split horizontally. Creating another X window is done with C-x 5 2 (yes, there are weird ass key combos, more on that later). Buffer (the files you have open) control is pretty easy too. Hit C-x b to switch (hit tab for a list, and don’t forget that tab completion is your friend) or C-x C-b to get a buffer list, and you can go through it and hit “d” do tag buffers for deletion, then “x” to do the deletions.

Similar functions are found in vim with the :split command. I admit I haven’t played with vim’s multi-window capabilities much, but they are comparable to text mode emacs. The grow/shrink in vim is C-w + and C-w – so don’t tell me that C-x ^ is that weird OK?

There are a lot of add-ons to emacs, and, in xemacs anyway, they are accessed via M-x pui-list-packages. BTW, in case you were wondering, tab completion runs rampant in emacs, so you’d hit M-x pui<TAB>l<TAB><ENTER>. There you can upgrade, install and uninstall packages such as dired, vm (mail reader), gnus (news reader), and various add-ons etc.

I ran across some problems in the initial install, as I couldn’t use the package manager without a package that I couldn’t get without it, or something like that, and had to install the “sumo” package, with is all packages, untarred into /usr/local/lib/xemacs/. Worked well though. I still come across some problems with it telling me that it has problems with the pgp signature of the packages index, but I seem to be able to get around this most times.

File access is wonderful. Editing a file (C-x C-f) has tab completion, but also lets you edit remote files (as I’m doing now) by editing /username@host:<path-to-file>. You are prompted for your password and voila, you edit away, and saving the file runs an FTP program in the background that uploads the changes. I had some issues with the FTP program timing out and the save procedure giving me strange results (stuck on ‘renaming to editor.html~’ or something) but hitting C-g (quit current operation) and then just hitting C-x C-s to save again worked fine. When you use tab completion you can move around the results, go into directories etc, using the dired program.

Modes are cool. When you edit a html file emacs will load html mode (well, you can set up what it loads for each one actually). Each mode (and there are lots gives you access to shortcut keys, syntax highlighting, menu options etc to help you in that. In html-helper-mode for example I can type in C-x < and it inserts the < character for me. The same type of shortcuts work for editing C, C++, Fortran, Cobol, lisp and a host of other file types.

Emacs main claim to fame, as I mentioned before is the integration of everything together. Probably it’s downfall too, as it tries to be almost too much when you want to just log onto a server and check a config file. I want to mention gnus and vm though. I use gnus now as my main news reader. It threads, it slices, it dices, it does everything I want it to. VM is cool, has threading stuff (unlike pine) but will not replace XFmail as my mail client yet. Yet. Depends on how much I can squeeze out of it.

To control all of this sort of thing you need a .emacs file (here is mine). It is scary to look at, but a lot of things are generated by the customization module (M-x customize) or by looking at a packages documentation and just copying what they tell you to do. I’m sure mine is quite simple compared to some, and it definitely is not organized. I kinda know what is going on in there, but not really, you know?

One last thing, the thing that I really really like and may be converted by… not only does emacs have a GUI mode that is very attractive, but it works identically in text only mode, so I can use the same program anywhere, even remotely, so when I’m at Linux World Expo I can telnet into my home box and check my mail and news with it without the need to change over anything because I’m not actually at my work station.

Bad Stuff

So you love emacs now don’t you arc? Well, yes and no. I’m warming up to it and enjoying it as a work environment. There are some things I’ve had problems with though.

  • The package issue kinda sucks, I’d rather install what I want and not get a “pgp amor” message when I try to update my package list.
  • Syntax highlighting doesn’t seem to always work, especially over a line break. Also, sometimes the syntax indenting will go wonky. For example if you look at the source of this page, you’ll see the line where I was talking about dired above where the indentation went from tabbed to non-tabbed for no apparent reason.
  • Key combos that hurt to do!
  • FTP transfers timing out and renaming a file but never completing the save till you C-g and re-save.
  • Perl mode doesn’t seem to auto-indent. You can hit <TAB> anywhere in the line to indent it properly, but why won’t it do that automatically!?!?
  • Not mutli-threaded. It really sucks to have your other window useless if one emacs window is doing a download of newsgroup headers or something. You’d think that would be a forked off process or something.
  • The old axion about Eight Megabytes And Constantly Swapping is still kinda true. I’ve seen it go up to 65 megs memory usage with many files open.
  • It just takes too long to start up if you just want to edit a file. If it’s already open it’s fine, but that is something you have to train yourself into.

Comparisons

Finally, the (X)Emacs/Vi(m) comparison! They are basically apples and oranges, but I’ll try my best.

First, key combinations. Both editors have kinda wonky key commands to do things. While saving in vi is :w or :w <filename> (for save-as) emacs uses C-x C-s or C-x C-w. C-x 2 or :split, 😡 or C-x C-c… your choice. One theory I do have though is that vi uses keystrokes that make sense from a finger point of view. Movement via hjkl is easy, your hands are on home row. If the ESC key was where caps lock is (where it should be) you’d never have to remove you hands from home row to do anything. Emacs on the other hand uses keys that make sense from a logic point of view. C-f for forward, C-b for backward, C-n for next line, C-p for previous line… M-x prefixes most modes or commands (ie: M-x ispell-buffer which I’ll do in a minute or 2 (I love the integration!)) and C-x or C-c prefixes a lot of common commands such as saving, cutting, marking, and pasting. They make sense from a logical point of view but are quite frankly painful on the hands. I have trained my fingers for a lot of emacs but have yet to write 10,000 lines of code in it. After that we’ll see.

Emacs is just too big for one person to remember everything! Who can remember that C-c C-s C-s is for sorting by subject in gnus, and M-x w3-hotlist-add-document-at-point is for adding a bookmark in w3 (the integrated web browser). I suppose with lots of practice, but gads…

Now vi has a lot of weird keystrokes too, but since I have used it to write 10,000 lines of code (surely more) my fingers know them better.

Vi is an editor, plain and simple. You can access shell commands with it (:! <command>) run shell commands on sections, have syntax highlighting, have it do auto-indenting, multiple windows, a GUI mode, it runs on all OSs, but it’s just an editor. Emacs is an environment, with little bits integrated in, including an editor, lisp interpreter, calendar, hooks for debugging with make/gdb/gcc, web browser, news reader, mail readers…. the list goes on.

Vi is fast to start and stop, you can go onto any server (yea yea yea, the vi is on every server argument) and edit. Emacs is something you start at the beginning of the day and close at the end.

Key combinations are easier for me (though I have 5 years on vi and 2 months on emacs). Hitting “V” and then “j” twice to select two lines of text is far easier than hitting C-@ to start the mark (that’s nasty on the fingers, you try it!) C-n twice, then C-x C-x to select, then C-w or ESC-w to cut or copy. Emacs is like a B-52, a rumbling heavy, where vi is a fighter plane, zipping in and out.

I am learning however. ESC-> and ESC-< for going to the start or end of a file are not as alien as they once were…. (though not as easy as ESC 1G or ESC G in vi).

Having everything built in has problems. Size, memory use and so on. And what if you don’t like the news reader or email client, or can’t customize the built in ones to your liking? Choice is a good thing of course, but XFmail has what I need, and vm doesn’t. So I lose out in that respect.

Sometimes too much control is too much. I have about 2 lines in my .vimrc file. One to turn on auto-indent, and one to turn on syntax highlighting. You can see I have far more than that in my .emacs file. The fact that the config file is a lisp program file is kinda funny in fact. Reminds me of the joke about emacs being a lisp interpreter with a fairly nice editor built in!

Conclusion

I’ve barely scratched the surface of (X)Emacs. I can handle myself in the editor, the NeWS-reader, and in most functions, moving around, and managing to do what I want. There is somuch I haven’t seen yet though. Efficient use of dired for one. When I first started I didn’t know it existed and when to M-x shell mode to do my file operations… now suddenly you have an integrated file browser. I have no clue as to half the shortcuts. I know there is a C-c C-p b mode to bold text, but will my fingers remember that? Not yet.

I’ve used vi exclusively for almost everything for the last 4-5 years of my Linux life, and it’ll take a bit to pry it out of my fingers. However, I am giving Emacs a shot. You may say that “how can you do work on a remote server in Tunisia with emacs cause vi is sure to be installed there!”… well yea, but if Emacs makes me a more efficient then I want it. And from what I can tell it could. Now it’s just a matter of me learning it well enough. If nothing else I now have enough knowledge of another program to say I’ve learnt something.

Arcterex, Aug 9, 1999