Advanced emacs usage tips

Home : Emacs tips : Advanced


These are the things that, in my opinion, makes emacs worthwhile; I use them constantly.

Table of contents

  1. Advanced emacs usage tips
    1. Table of contents
    2. Running a shell within emacs
    3. Viewing man pages
      1. Why I don't use pagers: A digression
    4. Editing Tcl source code
    5. Editing directory listings with C-x d
    6. Finding definitions in source code with M-.
    7. Finding regular expressions in source code
    8. Editing remote files via ange-ftp
    9. Other resources for more advanced emacs

Running a shell within emacs

emacs supports a mode called shell-mode (with variants) that let you run a command interpreter in an emacs buffer. (shell-mode is as opposed to sh-mode (aka shell-script-mode), which is for editing shell scripts.) Other modes with similar functionality (e.g. matlab-shell provided by matlab-mode.el) are usually built on top of this, and provide a similar user interface.

Commands:

Viewing man pages

Most Unix command and system API documentation comes in the form of "man pages," which are available online by invoking the man command
[0]. emacs provides a user interface to man that formats the page (which may actually be many pages if printed) and puts it in a buffer, where you can search, scroll, print, cut & paste, etc. Successive M-x man invocations result in successive buffers, which can be viewed in parallel.

Commands:

Why I don't use pagers: A digression

If you invoke man from a shell (as the hardcore Unix hackers do), then the formatted manual page comes to the standard output, flying rapidly off the top of the window[1]. In order to be able to actually read the page, it used to be that one had to pipe the output through a "pager" program, e.g. more (or the FSF analogue less), which stopped and prompted the user after each screenful, and also allowed context searching, among other features. It amazes me that even now, with bitmapped displays and cheap memory, people still use such things[2]. Given that I am already in emacs, I find it so much easier to use the shell buffer for running shell commands, and to look at man pages in man-mode buffers using the M-x man command.

Similarly, instead of doing "ls -l | less" to see a long-format directory listing of the current directory, in emacs you can do "M-x dired RET RET" (or "C-x d RET") to see the same listing in a "dired mode" buffer. The dired buffer can be scrolled forward and backward as by less, and searched somewhat more efficiently. Better still, dired mode also allows you to manipulate the files (edit, rename, delete, change permissions, etc.) with single-character commands. See the "Editing directory listings with C-x d" section for more information.

The other approach is to turn off paging entirely (by doing "setenv PAGER cat" in csh), and view the whole man page or ls listing in the shell buffer. But for most purposes, I find it easier to keep these in separate buffers, where they are randomly accessible and don't interfere with whatever I'm trying to accomplish in the shell.

Note that it doesn't make a difference whether you are running emacs under a graphical user interface, or through the rawest terminal emulator over a telnet connection. The emacs user interface looks almost the same, and a pager is more of a hindrance than a help in either case.

Footnotes:

[0]
Note that the Free Software Foundation considers man pages obsolescent, preferring info documents instead, so this statement is less true of (e.g.) GNU/Linux systems than of "traditional" Unix distributions. Although the FSF ships man pages with most of their applications, the info documentation is usually more complete, often containing tutorial material with examples, and may be more up-to-date.

[1]
Newer versions of man invoke the pager for you, unless you explicitly say otherwise, so this is no longer strictly true. It used to be that you had to explicitly pipe the output of man through the pager:
       man cmdname | more
But on most systems, this is no longer necessary.

[2]
OK, even I find it necessary to use a pager now and then when I'm using the raw console TTY in single-user mode. But even in those rare cases, it's usually more convenient to do it all within emacs.

Editing Tcl source code

[in progress]

Commands:

Editing directory listings with C-x d

dired is another extremely useful useful emacs feature that provides the ability to "edit" directories as if they were specially-formatted files. When dired is invoked, usually by typing C-x d, it asks for the name of a directory, and then shows the contents of that directory as a "long" ("ls -l") format listing, indented by two spaces. Files can then be manipulated, and subdirectories opened, by using single-character commands. Two styles of operation are supported: immediate operations, and mark-now/execute-later.

dired is an ancient and well-developed feature of emacs; the summary below only scratches the surface of what it can do. [and needs more work in any case. -- rgr, 19-Feb-01.]

Commands:

Finding definitions in source code with M-.

Once you set it up properly, emacs can cruise through source code hypertext style.

Commands:

Setting up:

This sets up a tags table, conventionally called TAGS, for finding stuff in a given directory. Let's assume that the "~/c-prog/" directory contains a program written in C.

  1. Go to the appropriate directory, and make the tags table:
           cd ~/c-prog
           etags *.[ch]
    
    You need to create the TAGS file initially, and may need to redo it periodically if emacs can't find something (usually because you've moved a definition from one file to another).

  2. Visit the tags table in emacs:
           M-x visit RET ~/c-prog/ RET
    
    If you are already in the ~/c-prog/ directory, then M-x visit RET RET" will be sufficient. In any case, you must do this every time you restart emacs.

  3. Or, to get this tags table automatically every time you restart emacs, put
           (setq tags-table-list (list (expand-file-name "~/c-prog/TAGS")))
    
    in your .emacs file.
Once you've set up one of your program directories this way, try M-. on one of the function names to test it out. etags understands C, Perl, Lisp, Fortran, Pascal, and a host of other programming languages.

If you install the mouse commands in the rgr-hacks package, you can click M-mouse-1 (that is, hold down the "Meta" modifier while clicking the left mouse button) to invoke find-tag on the definition name under the mouse.

Setting up an ArsDigita bootcamp machine:

This sets up a tags table, conventionally called TAGS, for finding stuff in the ACS system, as modified by you on your machine. You should be able to do this with a command like "etags *.tcl", but etags doesn't know about Tcl syntax, and certainly not about proc_doc, hence the arcana.

  1. Copy the sample makefile to your top-level server directory (/web/studentXX/), renaming it from makefile.text to makefile. You only need to do this once.

  2. Go to the server directory, and make the tags table:
           cd /web/studentXX/
           make
    
    You need to initialize this once, and may need to redo it periodically if emacs can't find something (because you've moved a definition from one file to another, etc.).

  3. Visit the tags table in emacs:
           M-x visit RET /web/studentXX/ RET
    
    You must do this every time you restart emacs.

  4. Or, to get this tags table automatically every time you restart emacs, put
           (setq tags-table-list '("/web/studentXX/TAGS"))
    
    in your .emacs file.

  5. Try "M-. ad_permission_p RET" to test it out.

Finding regular expressions in source code

The Unix grep command may be used to search for regular expressions in source files. For example,
    grep -n 'room-view' *.tcl
finds all occurrences of the string "room-view" in all tcl files in the current buffer's default directory (so it matters what buffer you're in when you invoke this command). These are presumably links that invoke the room-view.tcl script. grep uses a more powerful regular expression language than tcl (but not as powerful as emacs or perl); see "M-x man RET grep RET" for more information.

By itself, grep is quite useful, but emacs makes it more so. M-x grep runs grep itself to search for occurrences of a regular expression in a set of files, putting the result in a buffer. Then, C-x ` visits the lines where the regular expression matches (called "hits") one at a time.

Commands:

Hints:

Editing remote files via ange-ftp

[finish. -- rgr, 8-Nov-99.]

Other resources for more advanced emacs

Online documentation:

Printed documentation:


Bob Rogers <rogers@rgrjr.dyndns.org>
Last modified: Sat Jun 25 18:36:50 EDT 2011