Emacs

So you wanna learn emacs? Well, good choice. Emacs will serve you well while you're here. There are two flavors of emacs, GNU Emacs ( http://www.gnu.org/software/emacs/emacs.html) from the Free Software Foundation (http://www.gnu.org) and Xemacs (http://www.xemacs.org). Which one you choose is a matter of personal choice. Both are available on the EE/CIS machines. Xemacs has the nice pretty menu bars, but the new versions of GNU Emacs also has those (emacs-21). Personally, I use GNU Emacs. What follows should apply to both versions.

One of the more frequently cited reasons for not learning emacs is, and I quote:

I don't want to learn 1 million keystrokes

The people who say this are either going about learning emacs the wrong way, or are just making an excuse. There is no reason to sit down and try to memorize a slew of keystrokes. Most people learn better by taking a small bit at a time and working from there. Thus, it's in your interest to know a few keystrokes initially, then as you get used to emacs, you'll pick up others. Before you realize it, you'll know 20 or so of the more valuable keystrokes and that's pretty much all you need. When you want information on a more advanced topic, you can always look it up in the documentation.

Just to give you an idea of what emacs can do for you, here's what I use emacs for:

  1. Programming. Emacs has extensive capabilities to make editing of programs easier, including automatic indention (although you may have to tweak the settings to get what you want). Once the files are saved, I can launch a compile, saving the error messages in another buffer and then have emacs take me to the line with the error. Those of you taking AI can do some neat things like run the lisp interpreter in one buffer while making changes in another. Finally, the debugger, gdb, can be run within an emacs buffer allow you to step through the code.
  2. Checking news (the gnus package)
  3. Reading mail (vm)
  4. Running a shell to do whatever (yes, you can run a shell from within emacs).
  5. Editing directories (remove files, make directories, etc)
  6. Editing tar files. Yes, you can even edit a tar file by adding, or removing files from the archive, visiting files within the archive, etc.
  7. Read man pages.
And that's just a sample. Basically, emacs can edit just about any text file you can think of. If you've got a binary file, there's always hexl-mode to hex edit the file. There are quite a number of other things emacs can do as well.

Ok, so where to start? You need to know some basic emacs notation. A quick side note before we get into that. I'll put all the items you type in "code" type face, like this: C-x. Hopefully, that will help you differentiate between what is the command and what isn't. Now then, the keystrokes in emacs are done via holding the control key or by pressing the `meta' key. The symbol C-x means to hold down control and press x. M-x means to press the `meta' key (Escape on PC and SUN keyboards), let it go and then press x. The control keystrokes are just bound to run some particular command that can also be run by a M-x command. However, the reverse is not true; there are some M-x commands that have no control key shortcuts. You can get around this by binding particular keystrokes to run the commands, but that's an advanced topic that I won't go into here.

As an example, the keystroke to quit emacs is C-x C-c , which just happens to be the same as running M-x save-buffers-kill-emacs. As you can see, the M-x commands can get a bit long, hence the reason for having the keystrokes in the first place. Things could get ugly though for the M-x commands that have no keystroke. Thankfully, emacs provides tab completion. Try typing M-x sa and then hit the TAB key. Emacs will try to complete the command as best it can. If there are more completions, then hitting TAB twice will show them. (By the way, this is the same mechanism that the bash shell uses for file name completion. It's not a coincidence.)

One more note. Emacs edits `buffers'. This may seem like a pedantic point, but it's used everywhere in the documentation. When you want to edit a file, you first load it into a buffer, make changes, and then save the buffer. Now, don't be fooled. This is no different than what you do when you use any other editor. The reason emacs makes the distinction is that there is no particular reason to assume that a buffer is always attached to a disk file. Just look at the list above of the things I use emacs for. You can run a child process, such as a shell, debugger, or lisp interpreter, in another buffer. These things are obviously not attached to a particular disk file. Thus, it's important to realize emacs is manipulating buffers that may or may not correspond to a disk file.

Enough background. Here's some keystrokes for you to try:

See? 4 keystrokes, and one meta command to do some basic things. All of those things can be selected from the menu bar should you be in X windows. Go ahead and try a few of those commands (remember we're trying to learn things one bit at a time here). I'll wait.

Did they work? Great! Now, lemme tell you about a common mistake people first using emacs make. They treat emacs as if it can only edit one file at a time. Thus, they get in, make some changes, exit emacs. They then decide they have to edit another file. So, start up emacs again and edit that file. Quit and edit another file, etc. This is extremely absurd. Emacs is an extremely large editor and it does not start up instantaneously. Once running, it runs quickly, but startup is kinda slow. Thus, it's to your advantage to learn how to change between buffers. Personally, I log in, start emacs, and just leave it run until I log out. When I want to change something in a file, I use C-x C-f to find the file, edit it, and save it. If I want to come back to that file, I'll leave it in emacs and use C-x b to change back to it later.

Alright, so there's some basic stuff. What about programming? Emacs handles this by various `major modes' that give information about the particular programming language (actually, major modes are present for just about every type of file, not just programming files). For example, the c mode tells emacs information about how to indent a switch statement. Emacs tries to guess what major mode to use by the file extension. If it guess wrong, or if the file you are editing has a wrong extension on it, then you can change the major mode to whatever you want. Here are some commands:

You get the idea. But what about compiling? Here's some commands: Unfortunately, M-x compile and M-x next-error do not have any keyboard shortcuts. You can make your own if you wish (I have them bound to C-c c and C-c n respectively).

What about copying (or cutting) and pasting? This can get a little complicated, so I refer you to the documentation for a more in depth discussion. What you do is cut/copy the area between `mark' and `point'. Point is the spot just before the cursor. Mark is a spot in the buffer that you define. These two spots define a region that you can cut or copy. You typically cut one thing at a time (though you can get around this). You can copy more than one thing at a time (this ain't windows). Emacs maintains a series of `registers' that you can use to save arbitrary amounts of text. After cutting or copying, move the cursor to where you want the text and paste it in. Here's the commands:

You'll notice that so far I haven't mentioned anything about how to move the cursor around. You can just use the arrow keys, page up/page down, etc. for this. However, emacs does have bindings for these functions that you may find useful: Again, you can simply use the arrow keys for basic movement.

Well, that covers some basic stuff. For more information on emacs, see the tutorial. Type C-h t to start up the tutorial within emacs. It will go over alot of the basic stuff I've covered here. When you're done with that, you may want to check out the Emacs for Beginners Howto at http://www.tldp.org/HOWTO/Emacs-Beginner-HOWTO.html. It'll cover a few more things. Finally, when you're done with all that, it's time to check out the online documentation. The authors of emacs have written extensive documentation that you can buy at a bookstore like Borders, or you can just read from within emacs. To do so, you need to use the info mode. Type M-x info to get into it and then select the Emacs option. Start reading. There's a nice index that you can use to find a particular item.

Finally, here's some links for a few things you find useful.


Ben Breech
breech@cis.udel.edu