简体   繁体   English

Emacs命令用于搜索文件

[英]Emacs command for search in files

I want to search in all files from current folder for macro CODE_INIT_PARAMETERS. 我想在当前文件夹中搜索宏CODE_INIT_PARAMETERS的所有文件。 I can do Mx occur CODE_INIT_PARAMETERS but this shows only entries from open buffers. 我可以做Mx出现CODE_INIT_PARAMETERS但这只显示来自开放缓冲区的条目。

Is there any way to search all files from current folder, from Emacs, without switching to Mx shell and then grep ? 有没有办法从Emacs搜索当前文件夹中的所有文件,而无需切换到Mx shell然后grep? I want to avoid grep, because for some commands (Mx occur) Emacs do jumps to offending code, and I want that too. 我想避免使用grep,因为对于某些命令(Mx发生),Emacs会跳转到违规代码,我也想要这样做。 Thanks. 谢谢。

You can try Mx rgrep . 你可以试试Mx rgrep

It will ask for: 它会要求:

  • the directory where you want to search recursively 要递归搜索的目录
  • a file pattern for the files you want to include in the search 要包含在搜索中的文件的文件模式
  • the pattern you want to search 您要搜索的模式

As an extra, it will exclude source control private directories from your search (like CVS, .svn or .git). 作为额外的,它将从搜索中排除源控制私有目录(如CVS,.svn或.git)。

Emacs provides a built-in command: Emacs提供了一个内置命令:

M-x grep RET CODE_INIT_PARAMETERS *.c

(and 'grep-find to search sub directories) (和'grep-find搜索子目录)

Though I prefer the interface provided by an external package igrep (which provides the commands igrep and igrep-find ). 虽然我更喜欢外部包igrep (它提供命令igrepigrep-find )提供的igrep

if you open a folder in dired, and mark all of the files (with 'm') you can run 'dired-do-search ('A' in my bindings). 如果您在dired中打开一个文件夹,并标记所有文件(使用'm'),您可以运行'dired-do-search(我的绑定中的'A')。 This will search all marked files. 这将搜索所有标记的文件。 To get to the next one, run tags-loop-continue (M-,) 要进入下一个,运行tags-loop-continue(M-,)

I have set up several elisp functions to mark various sub-sets of the files (.h files, .cpp files, etc) and to create a recursive dired to search a whole tree... 我已经设置了几个elisp函数来标记文件的各种子集(.h文件,.cpp文件等)并创建一个递归的dired来搜索整个树...

This is an improvement on Trey Jackson's suggestion. 这是Trey Jackson建议的改进。

Mx grep Mx grep

You will see the grep command, eg grep -nH -e 您将看到grep命令,例如grep -nH -e

Add R to the first set of flags (for recursive), and put your search term after -e 将R添加到第一组标志(用于递归),并将搜索项放在-e之后

grep -nHR -e CODE_INIT_PARAMETERS

Hit RET. 点击RET。 The results will be understandable by Emacs -- you will be able to click or otherwise navigate to them, like Mx occur. Emacs可以理解结果 - 您可以点击或以其他方式导航到它们,就像Mx一样。 You may need to put the search directory at the end of the command: 您可能需要将搜索目录放在命令的末尾:

grep -nHR -e CODE_INIT_PARAMETERS /path/to/root/of/search

Mx find-grep-dired的工作方式与rgrep类似

In cases where 在哪些情况下

  1. you may be searching repeatedly; 你可能会反复搜索; and
  2. etags will work etags 起作用

you might consider using etags and invoking either find-tag (bound to M-. by default) or tags-search (no default binding but can be continued with M-, ). 您可以考虑使用etags并调用find-tag (默认绑定到M-. )或tags-search (没有默认绑定但可以继续使用M-, )。

There is as well ack-grep mode for emacs which use the ack-grep tool which is specifically designed for ''grepping'' programming languages and IMHO looks nicer than the output of Mx grep. 对于使用ack-grep工具的emacs也有ack-grep模式 ,该工具专为''grepping''编程语言而设计,而IMHO看起来比Mx grep的输出更好。

But as mentioned earlier etags should be the proper way! 但如前所述,etags应该是正确的方法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM