开发者

Running a macro till the end of text file in Emacs

I have a text file with some sample content as shown here:开发者_JAVA百科

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.


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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜