在 Emacs 中运行宏直到文本文件末尾
我有一个文本文件,其中包含一些示例内容,如下所示:
Sno = 1p
Sno = 2p
Sno = 3p
我想要的是从每列中删除 p
。 出于这个目的,我编写了一个宏:
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
按 e
两次将删除 p
,但如果我想做同样的事情直到文件末尾,我该怎么做如果我有 20000 行这样的行,我就不能一直按 e
。应该做什么?
请不要建议使用正则表达式,因为这是一个示例示例,而不是实际情况。 请不要建议任何 elisp,我很乐意记住 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
M-0 Cx e
重复上一个宏,直到发生错误(在最后一行之后,Cs =
或Cn
将是错误) 。您可能会听到烦人的蜂鸣声。
M-0 C-x e
to repeat last macro until an error happens (after the final line, eitherC-s =
orC-n
will be an error).You may hear an annoying beep.
您可以使用“apply-macro-to-region-lines”函数将最后定义的键盘宏应用到区域中的所有行。因此,您遵循的步骤是:
定义键盘宏(您已经完成了,但我在第三行添加了另一个 Cf):
CX (
铯=
RET 比照比照
删除
Cn Cx )
使用“mark-whole-buffer”命令选择整个缓冲区(默认情况下,它绑定到Cx h)。或者,如果您只想更改一定数量的行,只需选择要更改的行。
运行“apply-macro-to-region-lines”函数(默认情况下,它绑定到Cx Ck r)。
所有的 p 都将被删除。
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:
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 )
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.
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.
我通常在用
Mx name-last-kbd-macro
定义宏后给它起一个名字,这样我就可以方便地用Mx调用它。然后我用数字前缀来调用它。例如,我处理的文件通常没有 1000 个宏可以起作用的地方,所以 1000 个就足够大了,它会在文件末尾自动停止。如果您有较大的文件,您可以使用 1000000 作为前缀,如果仍然不够,那么您可以使用 1000000 作为前缀再次调用它。
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.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.
您可以尝试:
M-20000 Cx e
,以便在定义宏后执行 20000 次。you may try:
M-20000 C-x e
so as to do the stuff for 20000 times, after you define the macro.