user4209701
user4209701

Reputation:

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. The assumption here is that all of your coding projects are under the same folder (like ~/repositories/).

I tried to prefix the grep command with a command that cds 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.

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?

Upvotes: 0

Views: 1793

Answers (2)

margolari
margolari

Reputation: 671

you can simply run this using project.el builtin project package manager in emacs project-execute-extended-command with keybinding C-x p x and then execute the extended command rgrep or grep depending on what you want.

So I normaly do C-x p x -> rgrep -> enter-query et voila!

Upvotes: 0

PericlesTheo
PericlesTheo

Reputation: 2469

This does not answer directly your grep question but especially when working with projects, I always find projectile to be a great tool.

Projectile, amongst other things, provides a grep, ack and ag search on a project level which is probably what you are looking for.

E.g.

C-c p s g runs grep on the files in the project.

Upvotes: 2

Related Questions