me on G+
rants & musings
  current atom
misc creative
  movie reviews atom
  Botswana writing
  audio clips
  more recent
  older Africa pics
  Soul Ride
  Ringing Ear
  xootr & voloci
  Tectrix VR

"Powered" by Textweb

geekstuff | gameswf | Textweb | Chunked LOD | dumbuild

Cheat sheets: CVS | cygwin | Debian | Emacs | MythTV

GNU Emacs Cheat Sheet

I'm trapped in a love/hate relationship with Emacs. Emacs has a lot of virtues, but the greatest two, in my opinion, are:

  • works virtually everywhere

  • you can touch-type everything (I get RSI if I use the mouse too much, plus I hate fumbling back and forth)

Emacs has a lot of drawbacks too. The biggest problem is the learning curve. Here are some useful things I've learned about using it:

Emacs/Perforce Integration

(require 'p4) can be intolerably slow when my perforce server is remote. For some reason, it got so bad today that I tracked down the apparent problem -- p4.el is contacting the perforce server to get help text for each defined p4 command! To fix this, go into p4.el, find (define p4-help-text ...) and comment it out. Then add this:

  (defun p4-help-text (cmd text) text)

(require 'p4) is now almost instantaneous. The emacs p4 integration seems to work just fine. If I miss the help text, I'll type "p4 help ..." into a shell.

Emacs 21.4 For Windows

see my notes & download

Emacs Wiki

You can learn lots of esoteric but useful things from Emacs Wiki



Well, this is really basic. Emacs' undo is a little quirky in one respect: there's no separate "Redo" key. Instead, if you want to redo, you first press C-x, and then press C-_ . That changes the "direction" that the undo goes. You can change direction as much as you want, although I find it's pretty easy to get disoriented when doing some complicated partial undo.



I press this a lot. It's the built-in autocompletion key. It took me an amazingly long time to discover this. It doesn't require any setup, and it doesn't know anything about programming languages, but that doesn't really matter. It indexes all the tokens in all the currently open buffers, and tries to complete the current word you're typing when you press M-/ . If you don't like the completion, hit M-/ again for the next one, etc. Works for text too. bash also has autocompletion on this key.


For paragraph text, use M-q . It's a little bit smart about preserving prefixes, which you can use to your advantage when replying to emails that quote previous text with '>', or when indenting paragraphs. You have to manually put blank lines between prefixed paragraphs, before formatting, otherwise Emacs thinks it's all one paragraph. Also usually does the right thing with numbered/bulletted paras.

For code, use C-M-\ to re-indent the current region. You probably want to fix the built-in code formatting rules, since they're based on the retarded GNU conventions (see below).


Use tags to navigate source code. It's not hard to set up. This takes advantage of a popular tool called "Exuberant Ctags" (AKA ctags, or etags) that scans your source code and indexes the symbols into a TAGS file.

Note: emacs comes with a tool called "etags" that does almost the same thing as Exuberant Ctags. In cygwin, the "etags" binary is actually Exuberant Ctags. Confused yet? My advice is, ignore Emacs etags, and use Exuberant Ctags, whatever it happens to be called in your part of the universe.

To generate a TAGS file, do this in the root of your code tree (stick this in a script or Makefile):

    ETAGS=etags	# Exuberant ctags
    rm TAGS
    find . -name '*.cpp' -o -name '*.h' -o -name '*.c' -print0 \
    | xargs $(ETAGS) --extra=+q --fields=+fksaiS --c++-kinds=+px --append

Then, when you're reading code and want to see the definition(s) of a symbol:

    M-.       goes to the symbol definition
    M-0 M-.   goes to the next matching definition
    M-*       return to your starting point

One pretty annoying thing about ctags is that it only indexes declarations and definitions of symbols, not invocations. Fortunately emacs has a built-in workaround for this, called "tags-search". This is basically a grep that looks through all the source files mentioned in your TAGS file. It's fast, so you can pretty quickly zip through all the matches in your codebase:

    M-x tags-search <type your regexp>       initiate a search
    M-,                                      go to the next match

You can search for any regexp, not just source code symbols.

Bugs/annoyances with ctags:

  • doesn't understand C++ scoping. So if you're looking for SomeClass::IsValid(), you get shown every damn class in the codebase that has an IsValid() method, every IsValid member, global function, etc. This makes ctags useless in some situations.

  • tags-search doesn't seem to have a "go backwards" command.

Alternative indexing methods:

  • "Ebrowse" comes with emacs, and is a ctags-like tool that purports to understand C++. It seemed to basically work on some test code, but it crashed on the first large codebase I tried it on.

  • "ECB" is pretty fancy; it makes emacs look kind of like a GUI IDE with all kinds of browser windows & junk. It seems to be emacs' answer to Visual Assist, but I've never really been able to get comfortable with it (it partly ruins the uncluttered charms of the emacs interface, for one thing). Also it's not very easy to install, but maybe it'll mature into something great.

  • There's also this thing called "OO Browser" which is an amazing kitchen-sink parses-every-oo-language-ever piece of work. Again, I haven't been able to get comfortable with it; like ECB, it seems to require more mental attention and care & feeding than plain old ctags.

  • GNU id-utils This is sort of like ctags, but more general-purpose. It indexes all the words in a collection of files (not just symbol definitions), and gives you fast search ability keyed off those words. Emacs has some built-in stuff to assist with browsing the index. It seemed to work well on Linux, but I had little success/patience building it on Windows. The code hasn't been touched in almost 10 years, and doesn't build as-is under cygwin, so I gave up on it.

  • GNU GLOBAL This thing looks promising. I didn't have much success on Windows when I tried it, but it seems to be actively developed and maybe it'll show up in cygwin one day and solve all my problems.

navigating and editing source code

TODO (C-c C-u , C-c C-p , C-c C-n , C-M-\ , etc)


artist-mode is amazing. Use this when you want to post an ASCII diagram to gd-algorithms (or to put in a code comment), but don't feel like spending an hour typing it in.

1. Make some space in your document, to hold the diagram

2. do "M-x artist-mode" to turn artist-mode on.

3. draw with the mouse. The middle-mouse button gives a context menu, to select the drawing tool (line, elipse, box, erase, etc). For example, this stupid picture took me about 30 seconds:

      .     .. .  ..          .
...  ..  . .  . . .   .. ..  ..   ..  .
 ..... ... ...... .  .. .. .... ... ...
 \                \        /            |
  \                \      /             |
   \        +------+\    /+------+      |
    \       |      |      |      |      |
     \      |  ..  |      |  ..  |      |
      \     |  #.  |      |  #.  |      |
       \    +------+      +------+      |
        \             /                 |
         \           /                  |
          \         ----                |
           \                            |
            \      -------------        |
             \    (    HEY!     )       |
              \    -------------        |
               \                        |

4. do "M-x artist-mode-off" to get back to regular editing.

5. You might want to untabify your diagram at this point; select the diagram and do "M-x untabify".

Yeah, it's not Visio, but it's still pretty fantastic.



tu@tulrich.com |