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

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

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


当前回答

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

其他回答

另一个选择是使用Icicles搜索。这是一种不同类型的增量搜索,它使用对搜索命中的迷你缓冲区输入的完成。当您修改当前输入时,匹配命中的集合将在缓冲区*Completions*中更新。

您可以搜索任意数量的文件、缓冲区或书签位置,您可以使用迷你缓冲区模式(例如regexp)匹配进行选择。

当您访问一个搜索结果时,您可以根据需要替换整个搜索结果或匹配当前小缓冲区输入的部分。按需替换意味着您不会依次查询每个搜索命中;你可以按任意顺序直接访问你想要的点击率。如果要进行的替换数量有限,这种方法可能比查询-替换更有效:跳过详尽的y/n提示。

Search is over search contexts that you define -- you are not limited to searching all of the text in the target files (e.g., you can skip comments or particular kinds of program sections). A simple example of a search context is a line, as in grep, but a context can be any pattern-matched block of text you like. Typically you define the search contexts using a regexp, but you can instead use a function. In addition to defining your own, there are predefined Icicles search commands for different kinds of contexts: blocks of text properties or overlay properties, thing-at-point things, etc.

您还可以以各种排序顺序对搜索命中进行排序,以便于访问/导航。

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+。

所提供的答案都很棒,但是我认为我应该添加一个稍微不同的方法。

这是一个更具交互性的方法,需要wgrep, rgrep和iedit。iedit和wgrep都必须通过MELPA或Marmalade安装(使用M-x package-list-packages)

首先运行M-x rgrep查找要查找的字符串。

您将能够指定文件类型/模式和递归的文件夹。

接下来你需要运行wgrep,用C-s C-p启动它。

Wgrep会让你编辑rgrep结果,所以在字符串上设置一个区域来匹配并使用C-启动iedit-mode;(取决于你的终端,你可能需要重新绑定这个)

所有事件都可以立即编辑。C-x C-s来提交wgrep。然后是c x s !保存已更改的文件。

这种方法的主要好处是可以使用iedit-mode关闭某些匹配M-;。您还可以使用rgrep中的结果跳转到文件中,例如,如果您有一个意外的匹配。

我发现它对于在项目中进行源代码编辑和重命名符号(变量,函数名等)非常有用。

如果你还不知道/不使用iedit模式,这是一个非常方便的工具,我强烈建议你去看看。

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

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

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

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