Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a macro till the end of text file in Emacs

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.

like image 847
whatf Avatar asked Sep 24 '11 16:09

whatf


People also ask

How do I run a macro in Emacs?

To execute the macro, press C-x e . This command executes the last macro you have defined.

What is CX keyboard?

C-x ) can be given a repeat count as an argument. This means to repeat the macro right after defining it. The macro definition itself counts as the first repetition, since it is executed as you define it, so C-u 4 C-x ) executes the macro immediately 3 additional times.

What are macros in Emacs?

A keyboard macro is a command defined by an Emacs user to stand for another sequence of keys. For example, if you discover that you are about to type C-n M-d C-d forty times, you can speed your work by defining a keyboard macro to do C-n M-d C-d , and then executing it 39 more times.


2 Answers

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.

like image 61
Nietzche-jou Avatar answered Sep 20 '22 01:09

Nietzche-jou


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.

like image 45
zev Avatar answered Sep 19 '22 01:09

zev