如何递归搜索整个项目/文件夹中的单词?

时间:2011-10-31 06:30:15

标签: search vim find

假设我在一个文件夹及其子目录中搜索一个类JFactory

如何归档包含class JFactory的文件?

我不想替换该字,但我需要找到包含class JFactory的文件。

6 个答案:

答案 0 :(得分:73)

:vimgrep /JFactory/ **/*.java

如果您想要全字匹配,可以将模式/JFactory/替换为/\<JFactory\>/:vim:vimgrep的缩写。

如果JFactory\<JFactory\>是您当前的搜索模式(例如,您在一次出现时点击*),则可以使用空搜索模式::vimgrep // **/*.java,它将使用最后的搜索模式。方便!

警告:如果启用,:vimgrep将触发autocmds。这可能会减慢搜索速度。如果您不希望这样做:

:noautocmd vimgrep /\<JFactory\>/ **/*.java

会更快。但是:它不会触发语法突出显示或打开gz文件解压缩等等。

请注意,如果您希望外部程序可以执行以下操作:

:set grepprg=ack
:grep --java JFactory

Ack是一个Perl编写的grep替代品。请注意,您必须切换到Perl正则表达式。

一旦您选择的命令返回,您可以使用:help quickfix中Vim文档中描述的命令浏览搜索结果。查找:cfirst:cnext:cprevious:cnfile

2014年更新:现在有了使用the_silver_searcherthe_platinum_searcher以及ag.vimunite.vim个插件的新方式。

答案 1 :(得分:16)

从项目根文件夹中运行以下命令:

grep -H -r 'what_you_search' * | less

您将获得一个文件夹列表以及与该字符串匹配的行。

答案 2 :(得分:6)

银色搜索者(https://github.com/ggreer/the_silver_searcher) 强烈推荐,真的很快!

安装

sudo pacman -S the_silver_searcher  // arch linux 
sudo apt install silversearcher-ag  // ubuntu

<强>使用

$ ag keywords

与vim集成

rking / ag.vim(https://github.com/rking/ag.vim

安装后

:Ag keywords

答案 3 :(得分:2)

查看ctagscscope,它们可以跳转到类和函数定义,并找到这些函数/类的使用位置。

答案 4 :(得分:1)

此脚本可能有所帮助:Filesearch

答案 5 :(得分:0)

通过以下方式打开命令行窗口:

Esc - 确保您处于正常模式

输入q,输入:

命令行应该打开(就像一个tmp文件来编写你可以导航的命令,因为你可以在任何vim文件中正常导航...

输入i进入插入模式

此示例将针对类型&#39; .js&#39;的所有文件类型递归地搜索当前目录的to_srch字符串。和&#39; .java&#39;但省略包含字符串node_modules

的所有文件路径
:g/console.log/ | :vimgrep /console.log/ `find . -type f -name '*.js' -o -name '*.java' -not -path '*node_modules/*'`

现在轮到你了:你可以用箭头键在sarch结果中导航......

你也可以在.vimrc中设置它们

    " how-to search recursively under the current dir for the files of type js and java but omit the
    " node_modules file paths
    ":g/console.log/ | :vimgrep /console.log/ `find . -type f -name '*.js' -o -name '*.java' -not -path '*node_modules/*'`
    " reminder open the quick fix window by :copen 20
    " reminder close the quick fix window by :ccl

你可以省略第一个:q / to_srch /我用它来自动突出显示搜索结果,因为我有&#34;设置hlsearch&#34;在我的〜/ .vimrc

任何提示如何自动启用vimgrep或vimrc中的srch结果将受到高度赞赏......