Keyboard Macros in Emacs

A very useful tool in Emacs is the keyboard macro. Every so often I need to perform a repetitive task that is slightly different each time. This time, I was changing casts again, in C++. I had a series of casts, that looked like this:
(MeasureLine *)(annot->newCopy());

and I needed to replace them with this:
dynamic_cast<MeasureLine *>(annot->newCopy());

So I defined a keyboard macro, where I first selected 'MeasureLine *', and the macro would cut that text, type in 'dynamic_cast<', paste, then type '>'
The keyboard macro is started by the keystroke 'Ctrl-x (', and ended with 'Ctrl-x )'. It is run by 'Ctrl-x e'

When I found another spot in my code that looked like this:

I could drag over 'ConstraintPlane*' with my mouse, and hit 'Ctrl-x e', and it turned into:

Done. Another trick is that 'Ctrl-u' followed by a number, then followed by 'Ctrl-x e' will run the macro many times in a row. Good if you include a 'Ctrl-s' incremental search in your macro.
Post a Comment

Popular posts from this blog

Inno Setup custom data directory location

Inno Setup MSVC vcredist without bothering your users

Followup: Ubuntu persistent USB console boot, or why won't my car read this USB stick?