What is emacs?

The simple answer would be that emacs is a text editor. Of course anyone who uses emacs knows this is not true. Emacs is a operating system, documentation viewer, and the lisp programming language all rolled into one .... and much more!

I personally use the "vi" editor, and heartily endorse the view that the best editor to use is the one you know. I learned "vi" back when I was using Unix edition 7 and was just glad beyond words to actualy have a screen editor and stop having to thrash around using "ed". Now using "vi" is more instinct than knowledge and I always have some interesting problem to work on, and learning a new editor like emacs would just be an unwanted interruption.

However, I have discovered that emacs can make a pretty spiffy code indenter and cleaner upper. So what follows is my short foray just far enough into the world of emacs to get it to do what I want.

First of all, I recommend xemacs. And for the single and simple reason is that you have a menu with an exit function to get back out of it. A great game to play with someone new to computers is give them the challenge of starting emacs and figuring how to get out of it. A window system is great here since you can just use the window manager to kill the window. If all else fails you can reboot the computer. There may actually be some way to use keystrokes to exit emacs -- at least there are people who seem to use it on a daily basis without having to reboot their computers all the time.

And now to get it to do indentation, you have to get it into the mood you want. In my case, this is indenting by 4 spaces. I add the following lines to ~/.xemacs/init.el

(setq font-lock-use-default-fonts t)
(setq font-lock-use-default-colors t)
(setq font-lock-maximum-decoration t)
(require 'font-lock)

(setq ruby-indent-level 4)
(setq c-indent-level 4)
(setq c-basic-offset 4)
(setq indent-tabs-mode nil)
After doing this, an entire file can be re-indented by pulling down the edit menu and doing "select all", then typing:
Ctrl-Meta-\
Then save the file and call it good. Note that setting c-indent-level didn't seem to do a bit of good. It seems that c-basic-offset is the order of the day.

This brings up what seems to be the best guarded secret in the emacs world, namely, "What the heck is the meta key?!!!!". Apparently it is any handy key other than Control, and it is typically ALT on a PC keyboard. Escape will probably also work, and may be the only thing that works on some old keyboards. I am told that some people make the stupid "windows" key perform the Meta function, through some trickery that doesn't interest me at this point enough to track down.

And this is a great time to wander off topic and discuss the whole business of code indentation and tabs. If you live in the cloistered world of your own favorite editor, you will probably find this discussion mildly perplexing. The first difficulty you may encounter is when you try to print something and find your nice indentation all botched on the page. You will find big issues when you collaborate on a project and your coworkers are using a different editor and different indentation conventions and tab schemes.

One scheme is to use a mixture of tabs and spaces in the source file, and tell the editor to indent by 4 say. In this scheme a tab will be equal to an 8 space indent and confusion will be minimal.

The ugliest scheme is to use tabs everywhere in the source file and tell the editor to render them as some fictional number of spaces (say 4), this will lead to many unpleasant surprises outside the world of illusion you have created.

The brute force scheme is to abandon tabs altogether and use spaces in the source file and just say what you mean. This works for me (and you can get this from "gvim" by using the "expandtab" option). Note that expandtab does not affect existing tabs, the :%retab command will convert them all to spaces, given that expandtab is set.


Have any comments? Questions? Drop me a line!