tulrich.com
☰ Menu+  instagram  tuffy  ringing ear  rentals 

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:

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

Undo

C-_

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.

dabbrev

M-/

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.

Autoformatting

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).

TAGS

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=/cygdrive/c/emacs-21.3/bin/etags.exe
    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:

Alternative indexing methods:

navigating and editing source code

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

artist-mode

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.

rectangle-mode

TODO

tu@tulrich.com |