There is always more to learn about Emacs

Most people know how to use Emacs in the same way that they know how to use Notepad or any other text editor. They can move the cursor in four directions one character at a time and use the backspace key. Some people even know how to save and exit emacs. If you can do this you could sort of say that you know how to use Emacs. But you will have missed out. It's a lot more than a text editor that seems to balance your parenthesis by default and realizing this will make you a much more efficient computer user.

This is because text processing is still very important if you want to better understand how to use your computer. Perhaps not everyone is interested in this level of interaction with their computer, but if you want to be a more advanced user you will need to get comfortable with editing text. A lot of people do tedious work (the kind of work that computers were invented for) because they don't recognize the power of text processing. If you find yourself doing tedious work with your computer (for example updating the same link on more than three webpages) I recommend that you take a step back and ask if your task can be automated (perhaps with a quick recordable emacs macro?). Even if it takes you longer to figure out how to automate something for the first time, you'll be ahead of the game for the next time. (On a side note, an operating system can be thought of as nothing but a collection of text files, thus if you can interface well with text files you will be a much more productive computer user overall. ) I am not saying that Emacs is the only way to master text: there are lots of good ways to do this and Emacs is one of them.

I realized how powerfull Emacs was when I saw an experienced Emacs-user edit a file. It reminded me of how strange it seemed to watch someone edit a file with vi. E.g. the way that a vi users hands move in relation to what you see on the screen seems foreign if you are used to thinking of a text editor in the same way that the average user thinks of a word processor. I was used to seeing such things on vi, but not Emacs and I then realized that there is something more to Emacs.

Emacs is an extensible, customizable, self-documenting real-time display editor!

One of the consequences of this is that any small bit of Lisp that you find useful for the editing of your document can be bound to a set of keystrokes, just by editing your .emacs file. And like most Lisp based systems, Emacs contains Lisp, so it is truly extensible. Not just extensible in some superficial language that the system wasn't actually written in. I don't yet comprehend it a posteriori, but I have some idea that this is just how Lisp itself works (see "New Techniques" under Ch. 1 of Paul Graham's ANSI Common Lisp). Emacs is also an excellent learning tool. As you use it, it will show you its own code for a particular command in another buffer. If that command doesn't do something that you want it to you can update that command (I'm not on this level yet).

If I want to learn Emacs better how should I start?

My friend Richard gave me some tips on how I might become a better Emacs user. Note that these tips will use a shorthand to indicate a series of key-strokes that you can type. For example:

The above commands would be spoken as Control x or Meta x respectively.

A note about the Meta key

If your alt key does not translate to Meta (i.e. you should see a M-x in the minibuffer when you hold down alt and type "x") then you can use the escape key. To do this, simply type the escape key, release it and then type "x". This will produce the exact same effect as typing M-x.

Here are the tips:

I. Use One Emacs Session and Multiple Buffers

Learn how to split your screen and get around in it
(C-x 2 Horizontally, C-x 3 Verically, C-x o jump between, C-x 1 back to one screen, C-x 0 close the screen you're in )

II. Stay in Emacs

Don't cd around the directories to find what you want to edit and fire up emacs for each file. Instead just start Emacs when you login and use C-f to find the files. You can even do this on multiple systems by using network tricks.

III. Run multiple shells within Emacs, not multiple Emacsen within one shell

There are two ways to do this:

a) Inserting shell output:

Emacs can run shell commands (in most modes, shell commands can be invoked at the command line with M-!). The command's (or pipeline's) output will be inserted into a buffer called *Shell Command Output*. For commands for which either you don't care about the output or the output isn't likely to need to be piped anywhere (informational commands like apm, from, who, etc.), it's handy to be able to just have emacs run the command for you, rather than stop what you're doing, bring up a terminal emulator, run the command, etc. It's not so great for all commands, but it's good for some. Also, it allows you to stay inside Emacs, and therefore have a single unified interface to work with.

b) Actually run a shell:

Let's suppose that you've learned to jump around in emacs and that you can jump back and forth quickly (since you can do it with your keyboard and you no longer need move your wrists to reach for the mouse). You can do the same with multiple shells within emacs by typing:

  1. M-x ansi-term
  2. /bin/bash

I find that ansi-term is nicer than M-x shell or M-x eshell since you have command line history that is more like a standard bash session and you can have multiple instances.

Bug Fix:

You might have trouble running ansi-term depending on the version of emacs that you are using. As of today I am using:

GNU Emacs 21.2.1 (i386-debian-linux-gnu, X toolkit, Xaw3d scroll bars) of 2002-03-22 on raven, modified by Debian

and I had to modify my emacs slightly (under the guidance of Richard) to get ansi-term to work as nicely as I have just described.

Here is one way to get ansi-term working (given the above version of emacs):

  1. Put this Elisp file (provided to me by Richard) in a directory (in this case I'm putting it in ~/elisp).
  2. Tell your emacs session to use this file by adding the following lines to your .emacs:
    (add-to-list 'load-path "~/elisp")
    (require 'termfix)

IV. Use Macros (and other tricks) When Doing Short Repetitive Tasks
Suppose you have to wrap quotes around some strings such that:

would become:
This may seem tedious but it is similar to something that you might have to do if you wanted to turn a buch of URLs into HTML links.

Instead of doing the same thing four times, record a macro and use the universal argument to make it happen that many times. For example type C-x ( and then record what you would do once (C-a, ", C-e, ", C-n) then type C-x ) to stop recording your macro. You could then type C-e to execute that macro, but instead of typing it 4 times you could tell Emacs to apply that macro 4 times by using the universal agument. E.g C-u 4 C-e.


One other neat trick to simplify repetitive tasks is using the idea of the rectangle. Let's assume that we want:

To become:
Note that the as was removed. One way to see this operation is to look at the as characters as rectangles, i.e. you should be able to see the bold characters forming a rectangle in this block of text:
If you put the point (a.k.a. cursor) in front of the first "a" and set the mark (C-<space-bar>) and then put the point after the last "s" you would denote a rectangle with that region.

To make Emacs delete the characters in that region type: C-x r k

You can also exchange the above "k" for a "y" to yank and do use other commands.

V. Take advantage of Emacs modes

Learn how to run programs that you use with a unified interface. I do a lot of work with MySQL. I used to run multiple xterms and have a MySQL prompt in one and have my table definitions in another. I would then paste my table definitions into the second window to run my queries. This is not an ideal environment. After using it for a while you might come to think the following:

Luckily there is a better way to work which will satisfy the above. Try setting up your MySQL environment like this:

  1. Open your table_definistions.sql file (in Emacs).
  2. M-x sql-mysql
  3. You will then be prompted for the User:, Password:, Server:, and Database: in the mini-buffer
  4. Your emacs screen will then split into two buffers:
    • Your original table_definistions.sql file.
    • A new MySQL prompt
  5. You can then type queries in your table_definistions.sql file, evaluate them, and then see the results of your query at the MySQL prompt (if you're working with PHP you can then yank those queries into your code).
  6. You can use the following commands to evaluate your SQL statements:
    • C-c C-b: sql-send-buffer
    • C-c C-r: sql-send-region
    • C-c C-c: sql-send-paragraph
    For one-line queries C-c C-c will execute what is on the line before. For longer multi-line queries you can set the mark at the start of the query, put the point at the queries end and then type C-c C-r.

The last step of the above explains some of the commands that will help you use this mode. However, you can find them for yourself (and you should learn how to find them for yourself for other emacs modes that you might use). To find out information about the emacs mode you are in type F1 m (then jump to the new buffer and move around as you would any file).

VI. Learn Elisp and Apply it

See the Programming in Emacs Lisp Manual (related item: Learning Lisp).

One very basic application of reading the above that helped me relates to the following quote:

"If I had a nickel for every time I've written "for (i = 0; i < N; i++)" in C I'd be a millionaire."

- Mike Vanier

I feel the same way about some PHP code I have to type often. To save myself some time I've done something about it.

Here is some simple Elisp code which you can add to your .emacs file. After you add it you should be able to type:

M-x php-for-loop

(note that typing the above is easy since emacs has tab completion) and have the result be:

for ($i = 0; $i < $j; $i++) {


No big deal, but it might save you some time. There is a small collection of common PHP lines in the above file which I got sick of pasting from

Update: 12/08/2005

Zachary Kessin's PHP Code Generation with Elisp tutorial has ideas like this, but with slightly smarter code generation.

Learning to use your tools more effectively will help you focus on the more important things.