将字符附加到Emacs中每行的末尾

假设我有一个包含内容的文本文件

1 123 12 12345 

如果我想在每行的开始处添加一个“a”,我可以简单地使用string矩形(Cx rt),但是如果我想在每行的末尾附加一个“a”,之后文件应该成为

 1a 123a 12a 12345a 

谢谢。

您可以使用replace-regexp来达到此目的, $ regexp元字符匹配行尾。 转到缓冲区的开始,然后执行Mx replace-regexp ,然后回答$和(您的文本)到两个提示符。

或者,在emacs-说,为您的具体例子添加a

M-< Mx replace-regexp RET $ RET a RET

Emacs键盘macros是你的朋友。

 Cx ( Ce a Cn Cx ) 

它只是通过以下方式设置键盘macros:启动键盘macros( Cx( ),到行尾( Ce ),插入一个a ,到下一行( Cn ),然后结束macroslogging( Cx ) )。

现在你可以执行它( Cx e ),并且保持对你想要运行的每一行按e ,或者你可以在Cx Ck r的区域运行它。

如果你这样做了很多,你可以保存macros,或者你可以写一个函数。 这将是一个这样的function:

 (defun add-string-to-end-of-lines-in-region (str be) "prompt for string, add it to end of lines in the region" (interactive "sWhat shall we append? \nr") (goto-char e) (forward-line -1) (while (> (point) b) (end-of-line) (insert str) (forward-line -1)))