Emacs tips

My experiences with Emacs:

This document is meant to be tantalizing, but not comprehensive. It's an ecletic description of hints, tricks, things I learned later than I wish I had, etc. Before revision it had a file date of 2/23/03. I am revising it on 3/15/11, but it might still read in places as it was originally written.

Before getting too far along, I better say that the on-line documentation is very good. The keystroke "C-h" initiates it. My current use of Emacs is largely on a Dell desktop running Windows XP Professional at work; at home I use it on a Dell laptop running Windows XP Home.

There are several variants of Emacs. At the time I first wrote this I was using GNU Emacs, version 19.34. I am now using verion 23.2.1. Another variant is XEmacs. more (the linked file is dated 12/15/99 and hasn't been updated). I used XEmacs for along time and have version 21.4.21 installed. I am now also using Emacs, since I am recommending it to a user who isn't that familiar with it and I consider it to have fewer "loose ends" (compared to XEmacs) .

"C-h t" gets you started on the Emacs tutorial, which I found quite useful when I first started with Emacs.

"C-h i h" gets you started on the Emacs Info tutorial, which is a good way to learn how to use the tree-structured documentation.

"C-h m" gives help that is context dependent. It utilizes the documentation included with each function definition for whatever mode you happen to be in in a particular buffer.

There is an Introduction to Emacs Lisp that should be more digestible than starting off with the Emacs Lisp Reference manual. Each of these is available in Info format.

C-h is a good start for learning anything in Emacs, since it gives you various choices of what to learn.

I better put here the name convention for control character and "Meta" character. "C" is the control key, so that, for example, "C-c" is control c (sometimes indicated as ^c). The "Meta" character on my keyboard is the escape key (on Windows PCs it is also the Alt key). It is indicated as "M", so that, for example, "M-x" is the escape key followed by "x". On X Windows I am told that there is a real Meta character. I indicate "M-C" where the documentation indicates "C-M". This is because, without a real Meta key, the order is escape key first, then control key held down while striking the next letter. [this stuff is also discussed when you first invoke emacs].

Emacs features:

I use the following features in emacs:

I might add at this point that the hanging indent text shown above is produced by utilizing the mode "Indented Text Fill" in GNU Emacs. This is useful for simple documentation. The indentation is produced automatically as one types--styled upon the previous line. [you won't see the hanging indents on a browser, however, since it ignores such things--you'll have to look at the file itself]

Interactive Shell

The interactive shell has many nice features. It is invoked by typing "M-x shell". One can have several interactive shell buffers going at once, by using the "M-x rename-uniquely" command (since Emacs has name completion in its command minibuffer, one doesn't have to type each of these words in full). Assuming one gets a bunch of output from one command executed from the *shell* buffer, one can jump to the beginning of the output by the keystroke "C-c r". After looking at the output, if one wishes to discard all of it, one types "C-c o".

One disadvantage of the *shell* buffer is that it acts like a "dumb" terminal. This is disadvantageous when utilizing help from PAW, Adamo, or dbx--but not fatally so. Utilizing paw, adamo, or dbx when I want to make extensive use of help is an occasion when I will suspend Emacs and directly use tcsh.

The *shell* buffer redefines paragraphs to be a command and all its output. This is something I have only recently begun to use. Movements by paragraph are keystrokes "M-{", "M-}".

The "M-x cd" command is useful if one finds that file name or directory name completion is not working as expected in shell mode. Normally, Emacs tracks the use of the "cd" command in shell mode. However, under circumstances I don't understand, the tracking doesn't work. For a while I thought that "M-x pwd" would somehow correct the problem, but it just shows which directory the interactive shell is using. "M-x cd" shows you what directory the interactive shell is using, and allows you to change it.

tcsh

If one's shell is tcsh when emacs is invoked, there is one bizarre thing that happens when one goes to the interactive shell in Emacs. Every line of output from a command invoked from the interactive shell has ^M appended to it. However, not to worry, Emacs has a solution. It is the line:
(add-hook 'comint-output-filter-functions
                    'comint-strip-ctrl-m)

inserted into your ".emacs" file.

Every week this is an item that gets into the newsgroup "gnu.emacs.help" in some fashion or another. The solutions are quite creative.

Info:

Within an info "document", there is a tree structure that is given by "menus" within the text. One may choose from the menu and make a jump. There can also be individual cross references within text that can be followed. There is a method of back-tracking along the path of jumps that one has taken (the letter "l" does this).

Info trees typically have indices. The letter "i" allows you to start a search for an index entry. The character "," continues the search--once it is started. Of course, you may view the index directly and search it by other means, but I have found the "i" and "," search to be very powerful.

As an example of the use of Info, let me take CVS (Concurrent Versions System). I use this because for a long time I didn't realize that CVS is distributed via a FSF license (Free Software Foundation--the people that distribute emacs, gcc, gdb, gawk, etc.). I was using what I found on WWW for CVS documentation.

Even though the HTML CVS documentation is hyperlinked, there were some things there that looked like cross references that weren't hyperlinked. These always puzzled me, until I read somewhere that CVS is distributed from FSF. That was a great big "aha", since then it was a pretty sure bet that CVS Info files existed--perhaps even somewhere on FNALU. Sure enough, I could find them and link them into the Info dir structure. With that, I could see that the puzzling "cross references" were indeed Info cross references that hadn't been implemented as such in HTML. The CVS Info documentation was equal to or better than anything I had bookmarks for on the WWW, and it was a lot more convenient for me

Inside Emacs, the top level of Info can point to all manner of documentation. That for Emacs is one menu item on the menu that is displayed. One can link in any Info files one finds useful. I now keep all manner of personal notes in Info format. A copy of my personal notes dated 3/15/11 is available, in texinfo format. This can be converted to linked Info files, as explained in the texinfo Info manual.

One can jump directly to an Info node via the letter "g". The node can be qualified by its file name.

Dired:

I use Dired buffers constantly. They can be created by the keystrokes "C-x d" and then specifying a directory. One is then in "Dired" mode, and there are a whole set of neat operators defined. One can "mark" several files and perform an operation on all of them at once. As an example, one can mark a set of files with "m", and then use "R" to rename them all to another directory.

Etags:

Etags is very useful for exploring code that you haven't personally written. It is even useful for jumping to locations in code that you have written yourself.

"M-x tags-search" will ask you to specify a regular expression for a search in all the files mentioned in your etags file. For example, if you have a collection of Fortran files with which you are working, you can build an etags file for them. Then you can use that etags file to specify the set of files in which to do the "tags-search". "M-," continues the search after each instance of finding the desired expression.

Customizing Emacs:

Emacs can be customized by entries in the "$HOME/.emacs" file. Emacs is written in C and Emacs Lisp. What appears in the ".emacs" file is Emacs Lisp expressions. While that may seem "off-putting", the syntax of Emacs Lisp expressions can be picked up readily from the documentation--at the level necessary to add things to the ".emacs" file. Emacs always starts up a buffer which has "Lisp interaction" as its mode. In this buffer, one can try out Lisp expressions before institutionalizing them in the ".emacs" file. "M-C-e" goes to the end of the Lisp expression, and "C-j" evaluates it. The result of the evaluation is displayed in the buffer.

Keyboard Macros:

Emacs has a facility for creating keyboard macros. These can be temporary or can be saved in the ".emacs" file. They are combinations of keystrokes (which can be explicit emacs commands). I have several saved in my ".emacs" file. One jumps to column 72 on a line. Another copies one line of text from one buffer to another (with my screen vertically split into two windows). Another copies multiple lines of text from one buffer to another. Still another puts a buffer into Indented Text Fill mode. Another still shifts what is displayed by 30 characters, so that I can see more of lines that exceed 80 columns (my typical window width setting). A final one sets lines so that they appear truncated at the right edge of the window, instead of having them wrap around to the next line.

I should remark here that the keystrokes "C-x C-b" (or "M-x buffer-menu") produce a list of buffers in a special buffer (which of course has certain letters having operator meanings peculiar to it). This buffer is by its nature "truncated", so that lines do not wrap. The keyboard macro that shifts characters left by 30 is particularly useful with this buffer, so that I can see what file corresponds to what buffer. I did learn to set the buffer-local variable "truncate-lines" to "nil" for this buffer, so that the lines would wrap, but I often prefer to shift what is displayed in the window.

Documentation:

I have already mentioned the online Info documentation for Emacs. This is also printed in book form by the Free Software Foundation, and can sometimes be found in the computer section of bookstores like Borders or Barnes & Noble. The Info files and TeX files can be generated from the same, master texinfo file. A portion of the purchase price supports their software writing efforts (which Fermilab cannot do directly!). The book comes in handy when one is away from a computer terminal; for example, on an airplane trip one could read about Emacs features one hasn't yet discovered via online reading. This book has a GNU Emacs Reference Card bound into it; this is a one page, double-sided summary of Emacs keystrokes and commands. It also comes as a TeX file (or postscript) that can produce one page, double-sided output on a laser printer with the duplex printing feature.

RMAIL & VM

RMAIL is but one choice for reading mail within Emacs. It comes with the Emacs distribution. RMAIL does not send mail; sendmail does that (from within Emacs); one can reach sendmail from within RMAIL by typing the letter "m". In general, sendmail is entered via C-x m.

RMAIL uses mail structured with a mode called babyl, to separate mail messages in a file. VM (View Mail) can also work with mail files structured with babyl. In addition VM can work with IMAP servers and can deal with MIME headers in a mail message.

Modes

There are a number of different modes in Emacs--both major and minor. An example of a major mode is Fortran mode. In Fortran mode, Fortran source code may be semi-automatically indented to show nesting of "if" - "endif" combinations, "do" - "end do" combinations, etc. The keystroke combination "C-c w" puts a marker on the screen at column 72, which is useful if the compiler is going to ignore columns 73-80 and beyond. A "region" of source lines may be defined, and all turned into comments with an Emacs command--or vice versa. From Info node "(emacs)Fortran Comments":

`C-c ;'
     Turn all lines of the region into comments, or (with argument)
     turn them back into real code (`fortran-comment-region').

Emacs invokes Fortran mode for files with suffixes ".f"; it can be told to do so for files with suffixes ".F" (useful for Unix flavors where f77 doesn't automatically invoke CPP ahead of the compiler).

Another nice feature of Fortan mode is that one can break a line nicely, with the next line already set up as a continuation line ("M-C j"). Each major mode tends to redefine the meaning of certain keystrokes--especially the ones starting with "C-c". The current definition can be seen by typing the keystrokes "C-h m" (the use of "C-h m" is something I stumbled onto not too long ago--even although it can be found in the documentation).

For those persons writing Perl scripts or shell scripts, Emacs has modes that correspond. I haven't discovered the particular benefits of these modes yet. Of course, there is a C mode and a C++ mode. I mentioned earlier a HTML mode, as well as TeX mode.

View mode:

A file can be "viewed" in a buffer, instead of "visited". When it is "viewed", it is read-only, and there are some additional keys defined for moving around. One reason I mention this is that I found that the "M-C s" regular expression search doesn't seem to work in standard fashion when "viewing" a file.

Matching (){}

Along with indentation features that understand language syntax Emacs has commands that can find matching parentheses or brackets. This is explained in Info node "(Emacs)Programs". The commands are C-M-f and C-M-b. This feature can be quite useful when conditional constructs get quite convoluted. A syntax table is used for this purpose; it varies with choice of major mode.

If one gets to the point of trying to understand elisp programs written by others, there are a pair of useful commands that understand the structure of an elisp function. They are C-M-a and C-M-e. These can be very useful for finding the beginning and end of an elisp defun expression.

Rectangle editing

Emacs has rectangle edits, with cuts and pastes. Cut text can be stored in registers, and recalled from registers. The same registers can be used to mark locations in a buffer (in addition to this there is a permanent bookmark feature which is described under a separate heading).


Comments to: Alan Wehmann(wehmann@fnal.gov)
Modified: Friday, July 13, 2001 & lately on 3/15/11.