43

I have a text file with some sample content as shown here:

Sno = 1p
Sno = 2p
Sno = 3p

What i want is to remove the p from each of the columns. With this intention i write a macro:

M-x //go to buffer
C-x (//start the macro
C-s = // search for equalto sign
RET C-f C-f // reach to te alphabet 'p'
DEL // Delete
C-n  C-x )//go to new line and Close the macro definition 
C-x e

Pressing e twice will remove p, but in case i want to do the same stuff till the end of file, how can i do it i can't keep pressing e if i have 20000 such lines. What should be done??

Please donot suggest regex, as this is a sample example, not the actual case. Please donot suggest any elisp, i am comfortable with remembering shortcutf for emacs.

4 Answers 4

89

M-0 C-x e to repeat last macro until an error happens (after the final line, either C-s = or C-n will be an error).

You may hear an annoying beep.

2
  • 3
    this is the best answer, I just had to run a macro over 9000 times and this saved me from a lot of keyboard finger pain!
    – user9903
    Nov 15, 2017 at 18:10
  • 1
    Any way to get it to show the stuff happening while it works?
    – nroose
    Jul 20, 2018 at 0:17
21

You can use the "apply-macro-to-region-lines" function to apply the last defined keyboard macro to all lines in a region. So, the steps you follow are:

  1. Define your keyboard macro (which you had already done but I've added another C-f to the 3rd line):

    C-x ( C-s = RET C-f C-f C-f DEL C-n C-x )

  2. Select the whole buffer with the "mark-whole-buffer" command (by default, it's bound to C-x h). Alternatively, if you just want to change a certain number of lines, just select the lines you want changed.

  3. Run the "apply-macro-to-region-lines" function (by default, it's bound to C-x C-k r).

All the p's will be removed.

0
4

I usually give a name to the macro after defining it with M-x name-last-kbd-macro, so I can call it with M-x conveniently. And then I call it with a numeric prefix. E.g.

M-1000 M-x macroname

The files I work on usually don't have 1000 places where the macro can act, so 1000 is large enough and it will stop automatically at the end of the file. You can use, say, 1000000 as a prefix if you have larger files, and if it's still not enough then you can call it again with 1000000 as prefix.

-3

you may try: M-20000 C-x e so as to do the stuff for 20000 times, after you define the macro.

3
  • 20,000 was just an example, the macro i am writing is not meant for this simple thing, besides i donot know the number of lines, i wanted to do it till the end of line.
    – whatf
    Sep 24, 2011 at 16:29
  • Used Nietzche-jou's answer instead. That way I don't need to use a random number like 20000. Jan 13, 2015 at 20:23
  • 6
    You should mark Nietzche-jou's answer as the correct one.
    – rfcoder89
    Sep 26, 2016 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.