I've noticed keyboard macros running very slowly in emacs 24
for example - I just tried running a macro to append a '0' to the end of each line in a 105615 line text file
C-x ( C-e TAB 0 C-n C-a C-x )
then
C-u 105615 C-x e
takes about 5 mins to complete
I never noticed them running so slow before, wondered if it had anything to do with upgrading from 23 to 24?
Is there anything I can do to improve performance?
This answer certainly won't always be applicable, but if the major mode happens to be irrelevant for the macro in question, then a good way to improve the execution speed of a keyboard macro is to switch the buffer to fundamental-mode first. In some circumstances that can provide dramatic speed increases, as you eliminate the overhead of the major and minor modes.
If changing major modes isn't possible, you may still get a significant benefit from disabling some of the minor modes for the duration.
However, as per the accepted answer & comments, if a search-and-replace will suffice, that will always be far quicker than any macro.
In your keyboard macro I see two potential sources of slowness:
TAB
: depending on the major mode, this may perform a lot of work, e.g. to figure out the indentation to use.C-n
: since Emacs-23, this tries to move to the next display line, so it can require a lot more work than before (it has to take into account the display rendering, with details such as variable-width fonts, images, ...). Also it is not reliable for your use, since on a line longer than the display, it C-n
will move to the next display line but stay on the same logical line, such that the subsequent c-a
will just move back to the beginnning of the same line (because C-a
works on logical lines, not display lines). Of course, all this depends on line-move-visual
and visual-line-mode
.For the TAB, I don't have a good recommendation because I don't know what it's intended to do (depends on the major mode), but I'd replace C-n C-a
with C-f
which will be much faster and more reliable.
Yes, you are right, it's damn slow.
For this particular task you can use replace-regexp
.
M-x buffer-disable-undo
M-x replace-regexp $ <ENTER> C-q TAB 0 <ENTER>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With