作为这个问题的后续,它试图找出如何做这样的事情,这应该很容易,这尤其阻止我更习惯于使用Emacs,而是启动我已经熟悉的编辑器。在编辑多个文件时,我经常使用这个示例。

在Ultraedit中,我用Alt+s然后p来显示一个带有选项的对话框:查找(包括跨多行使用正则表达式),替换,在文件/类型中,目录,匹配大小写,仅匹配整个单词,列出更改的文件和搜索子目录。通常我会先用鼠标点击-拖动选择我想要替换的文本。

只使用Emacs本身(在Windows XP上),不调用任何外部实用程序,如何替换所有的foo\nbar与bar\nbaz在*.c和*.h文件中的一些文件夹和它下面的所有文件夹。也许Emacs并不是最好的工具,但是如何用最少的命令就能轻松完成呢?


当前回答

投射弹真的很不错: C-c pr执行投射-替换命令

其他回答

M-x find-name-dired RET it may take some time for all the files to appear in the list, scroll to bottom (M->) until "find finished" appears to make sure they all have loaded Press t to "toggle mark" for all files found Press Q for "Query-Replace in Files...": you will be prompted for query/substitution regexps. Proceed as with query-replace-regexp: SPACE or y to replace and move to next match, n to skip a match, etc. Type ! to replace all occurrences in current file without asking, N to skip all possible replacement for rest of the current file. (N is emacs 23+ only) To do the replacement on all files without further asking, type Y. Call “ibuffer” (C-x C-b if bound to ibuffer, or M-x ibuffer RET) to list all opened files. Type * u to mark all unsaved files, type S to save all marked files * * RET to unmark all marks, or type D to close all marked files

这个答案结合了这个答案、这个网站和我自己的笔记。使用Emacs 23+。

投射弹真的很不错: C-c pr执行投射-替换命令

我想推荐一个还没有被提及的伟大工具,即Helm。

它是许多标准Emacs操作的一个很好的替代品,包括补全、搜索等。特别是,helm-find-files允许在多个选定文件中执行查询替换(包括regexp)。

只需打开helm-find-files,用M-SPC标记相关文件,然后使用F6或F7在所选文件中运行query replace或query replace regexp。

M-x find-name-dired:将提示您输入根目录和文件名模式。 按t“切换标记”找到的所有文件。 按Q“查询-替换文件…”:系统将提示输入查询/替换regexp。 继续执行query-replace-regexp: SPACE替换并移动到下一个匹配,n跳过一个匹配,等等。 按C-x s保存缓冲区。(然后你可以按y表示yes, n表示no,或者!对所有人都是)

M-x project-query-replace-regexp RET(确保文件已保存!) M-x rgrep RET然后在每个缓冲区内查询-替换。这很好,因为你可以跟踪所有发生的事情,还因为它让你把搜索限制在某些扩展范围内。