[Arcana] A nice macro technique.

Karl Fogel kfogel at red-bean.com
Tue Jun 30 14:12:37 CDT 2009

No doubt all of you thought of this long ago, but I'm slow, and I
think I just started doing it.  (I dunno, maybe I did it years ago and
just forgot about it, you know, like with most of my .emacs.)

Anyway, just in case anyone else hasn't tried this, here's a
delightful little macro technique my father brought with him from the
old country, smuggled in a trumpet case, or so the story goes.

I often find myself iterating down a list of files, performing a macro
on each one.  You know the routine:

  1. Switch to '*shell*' buffer, 
  2. Run some 'find' command to produce a list of files, one per line.
  3. Put your cursor at the beginning of the first line, and open up a
     macro with 'C-x ('...
  4. Find the file, edit it, save the buffer (*), kill the buffer, make
     sure you're back in the shell buffer, 'C-n' to next file, 'C-x )'.
  5. Lather, rinse, repeat.

The problem with this is, you can never be sure your edits did quite
what you wanted.  Depending on the variability of the file contents,
your keystrokes may or may not have had the desired effect in every
file.  But since redisplay doesn't happen until the macro is done
playing, you can't easily check.

Solution: Duff's device, as applied to editing macros.

Start the macro at the "(*)" in Step 4 above, and end it in the same
place next time around.  That's the point where the buffer is saved,
and point is right where you just finished the edits.  Assuming the
relevant text all fits in the window (which is the common case), you
can inspect it.  Then you just run the macro and inspect the next one.
Lather, rinse, repeat, at whatever speed you want.  All your fingers
have to do is type 'e' (usually) or 'C-x e' (for those cases where you
have to stop and clean up an edit).

Go on, laugh.  Next week: I just learned about this great feature
where you can type part of a filename and hit TAB, and Emacs will
*figure out* the rest of the filename if it can...


More information about the Arcana mailing list