bph
bph

Reputation: 11258

Emacs 24 Keyboard Macros Running Slow

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?

Upvotes: 6

Views: 1791

Answers (5)

learner
learner

Reputation: 391

Even in fundamental-mode, some macro I wanted to run was very slow (not usable). I've found that firing a quick emacs -nw -q in terminal an opening my file and running my macro there was extremely quick !

Not a proper solution, but can be a useful workaround for those who occasionally have to execute a macro a large number of times, and don't want to spend time figuring out what is causing the slowness in their setup for a particular macro execution.

Upvotes: 0

phils
phils

Reputation: 73236

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.

Upvotes: 10

Stuart Hickinbottom
Stuart Hickinbottom

Reputation: 1360

My reading of info suggests that (forward-line) may be a better replacement for C-n C-a because this ignores any visual line breaks (so may be more efficient), and moves to the start of the line (saving the C-a).

Upvotes: 0

Stefan
Stefan

Reputation: 28521

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.

Upvotes: 5

Oleg Pavliv
Oleg Pavliv

Reputation: 21152

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>

Upvotes: 4

Related Questions