Search code examples
emacsdired

Find Files: Getting a dired buffer of files specified by filter containing text


This seems like a pretty typical use case, but I'll be damned if I can figure it out. There must be something fundamental I'm missing.

find-dired is basically a front end to the find command that yields a dired-buffer of results

find-grep-dired seems to be more of a front end to grep more so than find. and yields a dired-buffer but it searches all files in the directory tree.

What I would like is the ability to start at a given path and search *.css for #some-id and have it give me a dired-buffer for further processing.

Seems all the pieces are there but I haven't figured it out. So I'm thinking it's something fundamental I may have missed.


Solution

  • It sounds like the function you are looking for is grep. It will call the grep utility with the expression you give it, and collect the output in an interactive buffer. You may select any of the match lines in the buffer to jump to that line in the file that the match is from.

    For example, if you run M-x grep, you should get the following prompt in the mini-buffer:

    Run grep (like this): grep -n

    Then you add the regexp and glob pattern that you want to pass to grep:

    Run grep (like this): grep -n #some-id *.css

    And it should give you a list of matches for #some-id in all the files matching *.css in the current directory. If you want to recurse through sub-directories, then you need to use rgrep instead of grep. Does this sound like what you are looking for, or have I completely misunderstood your request?