[英]How do you grep through code that lives in many different directories?
I'm working on a Python program that makes heavy use of eggs (Plone).我正在开发一个大量使用鸡蛋(Plone)的 Python 程序。 That means there are 198 directories full of Python code I might want to search through while debugging.
这意味着有 198 个目录充满了 Python 代码,我可能想在调试时搜索。 Is there a good way to search only the .py files in only those directories, avoiding unrelated code and large binary files?
有没有一种好方法只搜索那些目录中的 .py 文件,避免不相关的代码和大型二进制文件?
grep -r -n "PATTERN" --include="*.py" DIRECTORY
我强烈推荐ack ,一个 grep 替代品,“针对拥有大量异构源代码树的程序员”(来自网站)
I also use ack a lot these days.这些天我也经常使用 ack。 I did tweak it a bit to find all the relevant file types:
我确实稍微调整了一下以找到所有相关的文件类型:
# Add zcml to the xml type:
--type-add
xml=.zcml
# Add more files the plone type:
--type-add
plone=.dtml,.zpt,.kss,.vpy,.props
# buildout config files
--type-set
buildout=.cfg
# Include our page templates to the html type so we can limit our search:
--type-add
html=.pt,.zpt
# Create txt file type:
--type-set
txt=.txt,.rst
# Define i18n file types:
--type-set
i18n=.pot,.po
# More options
--follow
--ignore-case
--nogroup
Important to remember is that ack won't find files if the extension isn't in its configuration.重要的是要记住,如果扩展名不在其配置中,ack 将找不到文件。 See "ack --help-types" for all the available types.
有关所有可用类型,请参阅“ack --help-types”。
I also assume you are using omelette so you can grep/ack/find all the related files?我还假设您正在使用煎蛋卷,以便您可以 grep/ack/find 所有相关文件?
This problem was the motivation for the creation of collective.recipe.omelette.这个问题是创建collective.recipe.omelette的动机。 It is a buildout recipe which can symlink all the eggs from your working set into one directory structure, which you can point your favorite search utility at.
这是一个构建配方,可以将您工作集中的所有鸡蛋符号链接到一个目录结构中,您可以将其指向您最喜欢的搜索实用程序。
find <directory> -name '*.py' -exec grep <pattern> {} \;
There's also GNU idutils if you want to grep for identifiers in a large source tree very very quickly.如果您想非常快速地在大型源代码树中查找标识符,还有GNU idutils 。 It requires building a search database in advance, by running mkid (and tweaking its config file to not ignore .py files).
它需要通过运行 mkid(并调整其配置文件以不忽略 .py 文件)来提前构建搜索数据库。 z3c.recipe.tag takes care of that, if you use buildout.
如果您使用 buildout, z3c.recipe.tag会照顾到这一点。
Just in case you want a non-commandline OSS solution...以防万一你想要一个非命令行的 OSS 解决方案......
I use pycharm.我使用 pycharm。 It has built in support for buildout.
它内置了对构建的支持。 You point it at a buildout generated bin/instance and it sets the projects external dependencies to all the eggs used by the instance.
您将其指向构建生成的 bin/实例,并将项目外部依赖项设置为实例使用的所有蛋。 Then all the IDE's introspection and code navigation work nicely.
然后所有 IDE 的内省和代码导航都可以很好地工作。 Goto definition, goto instances, refactoring support and of course search.
Goto 定义、goto 实例、重构支持,当然还有搜索。
And simply because there are not enough answers...仅仅因为没有足够的答案......
If you're developing routinely, it's well worth the effort to install Eclipse with Pydev (or even easier, Aptana Studio - which is a modified Eclipse), in which case the find tools are right there.如果您正在进行常规开发,那么使用 Pydev 安装 Eclipse 是非常值得的(或者甚至更简单,Aptana Studio - 这是一个修改过的 Eclipse),在这种情况下,查找工具就在那里。
OpenGrok is an excellent choice for source searching and navigation. OpenGrok 是源搜索和导航的绝佳选择。 Runs on Java, though.
不过,它在 Java 上运行。
I really wish there was something like https://oracle.github.io/opengrok/我真的希望有像https://oracle.github.io/opengrok/这样的东西
My grepping life is way more satisfying since discovering Emacs' rgrep command.自从发现 Emacs 的 rgrep 命令以来,我的 grepping 生活变得更加令人满意。
Say I want to find 'IPortletDataProvider' in Plone's source.假设我想在 Plone 的源代码中找到“IPortletDataProvider”。 I do:
我愿意:
Mx rgrep
The results appear in a new buffer.结果出现在新的缓冲区中。 At the top is the
find | xargs grep
顶部是
find | xargs grep
find | xargs grep
command Emacs ran. find | xargs grep
命令 Emacs 运行。 All matches are highlighted.所有匹配项都突出显示。 I can search the buffer using the standard text search commands.
我可以使用标准文本搜索命令搜索缓冲区。 Best of all, I can hit Enter (or click) on a match to open that file.
最重要的是,我可以在匹配项上按 Enter(或单击)以打开该文件。
It's a pretty nice way to work.这是一种非常好的工作方式。 I like that I don't have to remember
find | xargs grep
我喜欢我不必记住
find | xargs grep
find | xargs grep
argument sequences, but that all that power is there if I need it. find | xargs grep
参数序列,但如果我需要它,所有这些功能都在那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.