You hear about the great macro feature of Emacs all the time. You see cheat sheets for Emacs key combinations all the time. But you never quite see the keyboard macro trick demonstrated clearly. Here's how it works:
You begin with "C-x (" - that's Control+X, then shift-9. The message buffer will say "Defining kbd macro..." Then you type in whatever series of keystrokes you will want to call with one keystroke. When you're done with the series, hit "C-x )" (open and close parenthesis, just like Lisp, get it?) and the message buffer will now say "Keyboard macro defined". Now you can call the macro at any time with "C-x e" and it will execute just like you typed it all in from scratch!
A real-world (set-up!) example:
Ugh! My lousy HTML editor formatted my code listing example wrong! I want to get rid of the paragraph tags so I can just wrap the code in "pre" tags instead. I have a bunch of places where this happens in the file, and I'm looking to spend all day deleting "<p>" and "</p>" tags:
<p></p> <p>;; Factorial function</p> <p></p> <p>(defun factorial (n)</p> <p>(if (<= n 1) 1</p> <p>(* n (factorial (- n 1)))))</p> <p></p>
First (I'm working in XEmacs), I hold down shift+arrow keys to highlight the block I want to affect. Now before I type my key sequence, I just throw in a "C-x (".
My key sequence will be:
- M-x replace-regexp
- :return: x2 (I want them replaced with "nothing"!)
This will strip both "<p>" and "</p>" tags from the block:
;; Factorial function (defun factorial (n) (if (<= n 1) 1 (* n (factorial (- n 1)))))
Happy ending, but don't forget to type "C-x )" now to end the macro! Now I can go to all the other code blocks and highlight them, hit "C-x e", and the bad tags vanish.
vi users, I'm sure you have a way to do this too, but the sed sequence escapes me...
blog comments powered by Disqus