Emacs macros

jdavidb on 2002-10-25T20:18:02

I'm sure I'm not the only person out there still learning emacs.While cutting and pasting jjohn's 10-line code snippets to make the SOAP journal client (Don't waste an opportunity to learn! Examine and understand each piece before going on!) I wanted an easy way to delete the source code line numbers and associated spacing. In vi, I'm quite familiar with how I'd do that: 7x to delete 7 characters, followed by n, followed by dot to repeat. Unfortunately the same approach in emacs fails because the "repeat" command repeats the act of going to the next line.

With the help of a coworker and the documentation, I figured out how to get what I wanted: c-x ( starts recording a keyboard macro, then I hit c-a to go to the beginning of the current line (I'm writing this macro defensively :) ), then c-u 7d to delete seven characters, including the spaces and line number, then c-n to go to the next line. Finally I close out the macro with c-x ) . Now, I can execute my macro once with c-x e , or I can tell it to repeat ten times (say, each time I paste one of those snippets), using c-u, as in c-u 10 c-x e.

Have you ever noticed that emacs is cool?

Oh, and, if you hadn't guessed, this is my first post from emacs with the new client. Thanks, Joe!

Addendum-- or, if I'd waited until the end of the article, I would have learned that I could just download the program. But then I wouldn't have learned this really cool emacs trick, would I? I might look foolish at this point, but I'm satisfied. :)


or...

da on 2002-10-25T20:44:45

C-space to mark at the beginning of the first line;
cursor down to the 7th row of the last line;
C-X r k to delete the characters (mnemonic: region-kill)

oops...

da on 2002-10-25T20:53:29

Whoops, sorry; it's short for rectangle-kill.

Re:or...

kjones4 on 2002-10-26T00:05:17

Or, in VIM ctrl-shift-v (for visual block mode), then use your arrow keys, then x to delete. Newer versions of VIM use ctrl-v instead of ctrl-shift-v.

Re:or...

jdavidb on 2002-10-26T03:35:54

:) Sounds like emacs subscribes to Perl's TMTOWTDI philosophy.