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. S开发者_高级运维o I'm thinking it's something fundamental I may have missed.
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?
This is a bit late, but here is the emacs-lisp function I use regularly:
(defun find-iname-grep-dired (dir pattern regexp)
(interactive
"DFind-name (directory): \nsFind-name (filename wildcard): \nsFind-grep (grep regexp): ")
(find-dired dir (concat "-iname " (shell-quote-argument pattern) " "
"-type f -exec " grep-program " " find-grep-options " -e "
(shell-quote-argument regexp) " "
(shell-quote-argument "{}") " "
(shell-quote-argument ";"))))
It combines find-grep-dired
and find-name-dired
with case-insensitive file name matching.
I'll give A. Levy credit. But I think I figured it out. It seems that find-dired is indeed the tool for the job, but on my windows machine it doesn't pre-fill the args with "-exec grep" and it didn't dawn on me to do that. My linux machine however does pre-fill it with grep.
So....
#on Linux
M-x find-dired
Run Find in Directory: <current_path>
Run find(with args): -type f -exec grep -q -e \{\} \;
#on windows
M-x find-dired
Run Find in Directory: <current_path>
Run find(with args):
#Solution
M-x find-dired
Run Find in Directory: <current_path>
Run find(with args): -type f -iname "*.css" -exec grep -q -e #some-id \{\} \;
You said (in a comment):
the only thing missing is outputting to a dired buffer so I can do things like select/move/copy all those files at once
Sorry I didn't see this question until now. That feature is available with Dired+. Command diredp-grepped-files-other-window
opens Dired on the files indicated by grep
hits. (It also works for any other mode derived from compilation-mode
.)
精彩评论