简体   繁体   English

如何获取 emacs grep 根据项目根目录搜索?

[英]How to get emacs grep to search based on project root?

I want emacs grep to always search from the root project directory, not the directory of the currently opened file.我希望 emacs grep 始终从根项目目录而不是当前打开的文件的目录中搜索。 The assumption here is that all of your coding projects are under the same folder (like ~/repositories/ ).这里的假设是你所有的编码项目都在同一个文件夹下(比如~/repositories/ )。

I tried to prefix the grep command with a command that cd s into the project root, but this leaves the emacs working directory variable untouched, so while the grep is performed in the correct filesystem location, jumping to search results is broken, because the partial file paths of the grep matches are appended to the emacs working directory.我尝试在 grep 命令前面加上一个cd进入项目根目录的命令,但这使 emacs 工作目录变量保持不变,因此虽然 grep 执行了正确的文件系统位置,但跳转到了正确的文件系统位置grep 匹配的文件路径附加到 emacs 工作目录。

To summarize, I want to be in ~/repositories/my_project/folder1/file1.c , invoke grep command which will search from ~/repositories/my_project/ , and have the jump to file functionality of the grep buffer work. To summarize, I want to be in ~/repositories/my_project/folder1/file1.c , invoke grep command which will search from ~/repositories/my_project/ , and have the jump to file functionality of the grep buffer work.

How can I achieve this?我怎样才能做到这一点?

This does not answer directly your grep question but especially when working with projects, I always find projectile to be a great tool. 这不能直接回答您的grep问题,但尤其是在处理项目时,我总是觉得弹丸是一个很好的工具。

Projectile, amongst other things, provides a grep , ack and ag search on a project level which is probably what you are looking for. 弹丸除其他外,在项目级别提供grepackag搜索,这可能正是您要寻找的。

Eg 例如

Cc psg runs grep on the files in the project. Cc psg在项目中的文件上运行grep

you can simply run this using project.el builtin project package manager in emacs project-execute-extended-command with keybinding Cx px and then execute the extended command rgrep or grep depending on what you want.您可以简单地使用project.el内置项目 package 管理器在 emacs project-execute-extended-command中使用键绑定Cx px运行此扩展命令,然后执行扩展命令rgrepgrep

So I normaly do Cx px -> rgrep -> enter-query et voila!所以我通常会做Cx px -> rgrep -> enter-query et voila!

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

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