A novice of the temple once approached the Chief Priest with a question. "Master, does Emacs have the Buddha nature?" the novice asked. The Chief Priest had been in the temple for many years and could be relied upon to know these things. He thought for several minutes before replying. "I don't see why not. Its got bloody well everything else." With that, the Chief Priest went to lunch. The novice suddenly achieved enlightenment, several years later.
One way to describe Emacs is that Emacs is a highly customizable text editor which uses a dialect of Lisp called Emacs Lisp (or Elisp) as an extension language. However, I prefer to describe Emacs as a work environment written in a dialect of Lisp. Naturally, Emacs has the features you would expect in a programmer's editor: syntax-based indentation, syntax highlighting, parens/braces matching, quick navigation among functions in a file, etc. But you can also tell Emacs to run your compiler for you, parse the compiler's error messages, and take you to the file an line the error occured on. Or you can run your debugger from within Emacs, and have Emacs display the currently executing portion of your code. Or, you can use Emacs to manage files, both on the machine you are currently logged in to and on remote machines. You can also use Emacs to run your shell, read mail and news, and even browse the web. Granted, you may want to use Emacs just as text editor, and choose to never explore those capabilities. But even if you only use Emacs as a text editor, it helps to have some appreciation of the facilities available to you, and more importantly, of the design that makes it possible for Emacs to provide all those facilities.
Like many interactive programs, Emacs spends most of its time in an event loop, similar to the programs most of you have written in your introductory programming classes (using the famous (or infamous) GP142 graphics package). The description that follows is an abridged version of the XEmacs From the Outside section of the XEmacs Internals manual.
Most of the time, Emacs is in a loop where it repeatedly:
It is not uncommon for applications to be structured around an event loop -- most GUI apps are. What makes Emacs special is that Emacs Lisp is a complete programming language. With many interactive programs, even if you can customize which action a program takes in response to your input, you are limited to a choosing from a set of pre-defined actions the program's creator has provided. Not so with Emacs. Because Emacs Lisp is a programming language in its own right, and because new Emacs Lisp programs can be loaded into Emacs while it is running, and because those new programs immediatelly become "part of Emacs", the range of actions Emacs takes in response to your input is limited only by your imagination. You can program Emacs to perform exactly the actions you want.
So, do you have to learn how to program in Emacs Lisp in order to be productive with Emacs? The answer is no, you don't have to. Many people have written (and continue to write) extensions to Emacs. Most of these extensions are free for anyone to study, modify, and redistribute. Majority of the time, you can just adapt existing Emacs packages to your needs, instead of writing your own packages from scratch.
Of course, you can still connect an input event to a pre-defined action by modifying the Emacs' keymaps. This kind of customization is very common and quite easy.
As of the time of this writing, you have three choices as to the version of Emacs you use. First, there is the "official" Free Software Foundation Emacs (aka "GNU Emacs") provided by the support folks. To start that version you just type emacs at the command prompt. Second, there is XEmacs. XEmacs development is mostly independent from the Free Software Foundation's Emacs line of development. XEmacs has better a GUI than the "official" emacs; it also supports colors when running in a terminal. It generally has a slightly different feel than the FSF emacs. Some people prefer XEmacs, others prefer FSF emacs. XEmacs is installed and manitained by the iuns group, so to run it, you either need to have /uns/bin in your PATH (in which case you can simply type xemacs to start XEmacs), or you can type the full path: /uns/bin/xemacs. Finally, there is the Free Software Foundation's "pretest" version of Emacs, Emacs 21. Its GUI is much nicer than the Emacs 20 GUI, it supports proportional fonts, and it supports colors when running in a terminal. This version of Emacs is also installed by the iuns group; to run it you can type emacs21 (if /uns/bin is in your PATH), or you can simply type /uns/bin/emacs21
When Emacs starts, it will attempt to read the .emacs file in your home directory. If the .emacs file has been read successfully, then Emacs will load this file (by executing, or, in Lisp lingo, evaluating any commands that are found in the file).
The file /uns/examples/tutorial.dot.emacs contains some common customizations for both FSF Emacs and XEmacs. The recommended way to use /uns/examples/tutorial.dot.emacs is to make a symbolic link from that file to the file .emacs in your home directory. You can then put your own Emacs settings in the file named .myemacs.post in your home directory. The following shell script is one way to start using /uns/examples/tutorial.dot.emacs fairly safely.
#! /bin/sh
dot_emacs=$HOME/.emacs
# If .emacs is a symbolic link, and it points to /uns/examples/tutorial.dot.emacs, then we are done.
[ -h $dot_emacs -a "`/usr/local/bin/readlink $dot_emacs`" = "/uns/examples/tutorial.dot.emacs" ] || {
# Otherwise, try to move .emacs out of the way.
# Moving .emacs out of the way will fail if you don't have a .emacs -- that's OK.
mv $dot_emacs ${dot_emacs}_`date '+%Y-%m-%dT%H:%M:%S'` || true
# now make the symbolic link.
ln -s /uns/examples/tutorial.dot.emacs $dot_emacs;
}
If you have already done some customization to your .emacs file, you
may want to merge your customization into your .myemacs.post.
Whether or not you choose to use the sample .emacs provided by iuns, if you use FSF Emacs, you may be interested in some common Emacs lisp packages we have installed below /uns. Currently, we have
These packages are currently for FSF Emacs only (XEmacs has its own packaging system). If you are using /uns/examples/tutorial.dot.emacs, that file already contains the proper incantation to make the iuns packages available. If you are not using /uns/examples/tutorial.dot.emacs, the recommended way to use the iuns packages is to put
(unless (featurep 'xemacs) (load "/uns/epkg/emacs_packages-0.0/iuns/load.el"))in your .emacs file.
If you get errors while using the tutorial.dot.emacs or the packages provided by iuns, you should email the current iuns Emacs maintainer. The closest thing we have right now to an iuns Emacs maintainer is me, evgenyr@cs.washington.edu
Now, with the administrative issues out of the way, let's look at actually using Emacs.
The best way to learn how to do basic editing in Emacs is to work through the on-line tutorial that comes with Emacs. It explains how to move around the file you are editing, how to do basic things, such as cutting, copying, and pasting text, how to open, save, and rename files, and how to use the online help, among other things. I cannot emphasize this strongly enough: work through the online tutorial. The hour or so you spend working through the online tutorial will be well worth the effort. You can start the tutorial by typing F1 t (or C-h t)from within Emacs, or by running Emacs like this:
emacs -f help-with-tutorial
Some of the terminology Emacs uses may take a little getting used to. Most notably, Emacs uses the term window quite differently from what you are probably used to. What Emacs calls "a window" is usually referred to as "a pane", and "windows" in the sense you are used to are known as frames in Emacs.
While the Emacs tutorial provides a very good base for learning how to use Emacs, there are a few useful features that the tutorial does not mention.
In addition to the non-interactive replace-string the tutorial mentions, Emacs also allows you to replace strings interactively. You start an interactive search-and-replace operation by typing M-% (query-replace). You will then be prompted for the string to search for, and for the replacement text. Once Emacs has found a match, it will prompt you for the action you want to take. It will also give you directions on how to get help on the possible actions. The most common actions are:
| Key | Action |
| y or Space | Replace current match |
| n or Delete | Don't replace the current match |
| ! | Replace until the end of buffer without prompting |
Giving a prefix argument to M-% ("C-u M-%") will replace only matches surrounded by word boundaries.
Just about anywhere in Emacs, you can remove the special meaning of a character by first typing C-q (quoted-insert), and then typing the character in question. For example, to search for "foo" followed by newline, followed by "bar", you would type "C-s foo C-q C-j bar"
Instead of typing in the entire name for a long identifier, you can type in the beginning of a name, then hit M-/ (dabbrev-expand), and let Emacs complete the rest of the name for you. Repeatedly typing M-/ cycles through the possible completions. Try it sometime -- you'll be amazed at the number of keystrokes this feature can save you.
Emacs can do syntax highlighting for you. The tutorial.dot.emacs file arranges to have syntax highlighting turned on. If you are not using tutorial.dot.emacs, you may still be able to copy the portion that turns on syntax highlighting. Just search tutorial.dot.emacs for the string "font-lock"
Emacs also supports syntax-directed indentation, it even offers you a number of predefined indentation styles for C, C++, and Java programs. If you are coming from MSVC environment, you will probably be quite happy with the "stroustrup" indentation style. You will probably also want to make Emacs indent the new line properly when you hit Enter. You can do both of these things by adding this to your .emacs:
(add-hook
'c-mode-common-hook
(lambda ()
(c-set-style "stroustrup")
(local-set-key (kbd "RET") 'newline-and-indent)))
The tutorial.dot.emacs file already does this for you.
A few movement and marking commands are most useful when you are editing the text of a program. You can move to beginning the current function by typing C-M-a (beginning-of-defun), and move to the end of the current function by typing C-M-e (end-of-defun). You can also move backwards to matching open paren (or brace, or square bracket) by typing C-M-b (backward-sexp), and move forward to the matching closing paren (or brace, or square bracket) by typing C-M-f.
You can mark the function's body by typing C-M-h (mark-defun). If the cursor is at the opening paren (or brace, or square bracket), you can mark up to the closing paren (or brace, or square bracket) by typing C-M-<SPACE> (or C-M-@) (mark-sexp) -- this makes it quite convenient to move or copy program's blocks. Finally, you can mark the entire buffer you are editing by typing "C-x h" (mark-whole-buffer).
The tutorial.dot.emacs file also arranges things so that when you type "C-c g" in C, C++, or Java file, you will be prompted for a function to jump to. Search the tutorial.dot.emacs for the string "my-setup-func-menu" if you want to see how this is done.
Once you have a portion of your buffer marked (by using C-<SPACE> or C-@), you can ask Emacs to re-indent the marked region by typing "C-M-\" (indent-region). To re-indent a single line, just hit TAB when the cursor is on the line.
Once you've marked a portion of the buffer, you can comment the marked region out by typing "C-c C-c" (comment-region). You can uncomment the region by giving a numeric prefix to the comment-region function, i.e.: "C-u C-c C-c"
Emacs can provide a convenient interface to many commonly-used Unix tools. In this section, I will look at some examples.
Compiling your C/C++ code from within Emacs is quite simple: you just type "M-x compile"; Emacs will prompt you for the compilation command. If your project came with a Makefile, then the default "make -k" should be fine. Emacs then creates a buffer named "*Compilation*" and puts the compiler's output in that buffer. You can then either type "C-x `" to go to the file and line of each error message in sequence, or you can simply switch to the *Compilation* buffer, and hit Enter on the error line of interest. (If you are running under X, you can also click the second mouse button on the error line).
To run gdb from within Emacs, you type "M-x gdb"; Emacs will then prompt you for any arguments you want to pass to gdb. When gdb stops your program, Emacs will show you the currently executing portion of the source code. Also, you can set a breakpoint on any line in your program by simply moving the cursor to that line in the source code and typing "C-x <SPACE>".
You can run a shell from within Emacs. This saves you from having to suspend Emacs just to get to the command prompt. To start a shell, type "M-x shell". You can use any command-line tools inside the *shell* buffer, but trying to run a full-screen program, such as vi or less will not do what you want. However, since you can use normal Emacs movement commands to scroll through the shell buffer, there isn't any real need to use a pager, such as less. If Emacs seems to be confused about what the current directory is inside the *shell* buffer, type "M-x dirs". If you want to run more than one shell, rename the *shell* buffer to anything other than "*shell*" by typing "M-x rename-buffer", and then start a new shell by typing "M-x shell" again.
Dired (DIRectory EDitor) is an Emacs mode for managing files and directories. You manage your files by "editing" the directory listing, for example to rename or move a file you place the cursor on the line containing the file name, and type "R"; to copy a file, you type "C", etc. If you use FSF Emacs, you may find the dired reference card helpful.
The really neat thing about dired is that it can give you a way to browse files on remote machines, when combined with a package like TRAMP, or ange-ftp (or efs). To see what I mean try typing "C-x C-f /ftp@ftp.gnu.org:/" in your Emacs. You can now interactively browse the GNU archive directories. Simply typing "C" on a file will transfer it to the machine you are running Emacs on. Who needs WS-FTP?!
Say you have two slightly different versions of the file, and you want to see exactly what parts are different. Running "diff -u FILE1 FILE2" is a pretty good way to see the changes, but if you want something a little more interactive (or a little more colorful), try Emacs' ediff. Just type "M-x ediff", and follow the prompts. If you want to see differences between buffers, type "M-x ediff-buffers" instead.
To read Info pages, just type C-h i. Emacs' info reader behaves the same as the stand-alone info reader (the program called named "info").
To read a man page, type "M-x man" than type in the name of the man page you want to read. One advantage of reading your man pages under Emacs is that you can follow the cross-references (found in the SEE ALSO section) simply by placing the cursor on the cross-reference, and hitting Enter (under X, you can also click the second mouse button).
FSF Emacs21 comes bundled with a neat package called WoMan (WithOut Man), which, as the name implies, reads man pages without the need for an external "man" program, you may want to try it some time -- in FSF Emacs21, just type "M-x woman" instead of "M-x man", and follow the prompts.
You already saw that Emacs gives you a convenient way to navigate to functions within a single file. Wouldn't it be nice, though, if you could also navigate to functions across files? Imagine you just unpacked a project tarball, and while you are reading the code in one file, you come across a reference to a variable whose type is struct Mystery. It would be nice if you could look up the definition of struct Mystery quickly and efficiently. The emacs tags facility provides a way to do just that.
What are tags? Tags are "interesting" pieces of your program's text. More specifically, in C, functions and typedefs are tags, as are definitions of structs, unions, and enums. #define macro definitons are also tags. In C++ member functions are added to this list. Tags are kept in a file commonly refered to as a tags table. The file is usually named TAGS. A program called etags is used to create or update the tags table. If you have a lot of files in your project, probably the simplest way to create a tags table is to type the following in the directory containing your code:
find . -name "*.[ch]*" -print | etags -
You may need to give etags a --language=c++ option, if your .c files are actually C++. Giving it a --members option makes it consider class member variables to be tags, and giving it the --declarations option makes functions and external variables declarations be considered as tags.
Once you have created a TAGS file, you can use the "M-." (Meta + dot) to look up the definiton of a function or variable. You can also use "C-x 4 ." and "C-x 5 ." to look up the tag's defintiton in a separate Emacs window or a separate Emacs frame respectively. Once you have looked at the tag's definition and wish to go back, you can use "M-*" to do so.
If Emacs hasn't loaded a tags table when you use "M-." for the first time, it will prompt you for a tags table to visit. You can also explicitely load a tags table by saying "M-x visit-tags-table".
Emacs is a big application, and I cannot possibly hope to tell you all there is to know about it. So instead, I will point out some ideas for how to get more information.