Emacs search and open multiple files and search on all buffers
I'm currently in the beginning of a switching process forwards Emacs. However I'm having two basic problems.
How do I search for multiple files recursively from a specific path? I assume I have to use
find
/grep
/dired
but I'm not sure. For instance I would like to find all*.scala
files at pathC:/src/xxx
.When these files are found I would also like to open them all in the buffer at once. The only way I'm currently familiar with is C-xC-f.When all these files are in the buffer how do I then search across all the buffers, and get some kind of list of the result and/or perhaps able to navigate from result 开发者_如何学Goto result? Saying I would like to find all places with the text
case Int =>
occur.
I concur with phimuemue's answer, but I'll point out M-x rgrep
as well, which will run the necessary find/grep in order to present all of the matches without actually opening the files. Selecting a match then opens the relevant file at that line number. In some situations, this may be preferable to opening all of those files.
Also see these:
- Using Emacs to recursively find and replace in text files not already open
- emacs: interactively search open buffers
For Part A, you might look here.
For Part B you might have a look at multi-occur-in-matching-buffers
, which let's you specify which buffers you want to take (e.g. all buffers .*.scala
to look in all scala files) and what to look for (e.g. case Int =>
). This gives you a list of all occurences.
You're trying to find all occurrences of "case Int =>" in *.scala files in C:/src
The easiest way (assuming a default Emacs setup) is to use M-x rgrep
. It'll ask you for a search-string, file type and directory (in that order, and the prompts are labeled so there's no confusion). Just type in case Int =>
, *.scala
and C:/src/xxx
.
What you should see is a new buffer with a list of occurrences of "case Int =>" in all .scala files in that directory. If you click on an occurrence, Emacs will open that file and navigate to the line that contains it.
As a note, if you're trying to do search and replace across multiple files, you can do that using dired
options. You can find information on that option here.
*For Question A***
In Icicles, C-x C-f is by default a multi-command. That means that when you complete to a set of file names:
You can act on (e.g. visit) multiple candidates, selectively (e.g. C-RET, C-mouse-2).
You can act on all files whose names match your current input -- e.g., visit them all.
The same is true for other Icicles file commands, including those that let you match an absolute file name, meaning that your minibuffer patterns can match not just the non-directory part of the file name but directory parts as well.
For example, C-x C-f with a prefix arg matches absolute file names. And M-x icicle-locate-file
does the same thing for all files under a given directory.
(You can always use a multi-command as an ordinary command: C-x C-f acts normally if you use RET or mouse-2. If you don't use the extra key bindings to act on multiple files then you'll never know the difference.)
See http://www.emacswiki.org/emacs/Icicles_-_File-Name_Input
*For Question B***
What you want is Icicles search.
- Command
icicle-search-file
searches all files of a set you specify. - Command
icicle-search-buffer
searches all buffers of a set you specify. - Command
icicle-search
does both: files with a negative prefix arg, buffers with a non-negative prefix arg.
These commands let you specify a regexp to define the search contexts: the parts of the files or buffers that you want to search. For example, .*
means search each line.
After you define the search contexts you type some text in the minibuffer, and it narrows the candidate search contexts to those that match your text. You can hit M-SPC to combine multiple such patterns.
Then you can navigate to the selected search hits: C-RET or C-mouse-2 to visit, or cycle/visit using C-down. You can even sort the matching candidates in various ways, to compare them easily or to change the cycle order.
See http://www.emacswiki.org/emacs/Icicles_-_Search_Commands%2c_Overview
I think you're artificially constraining the answer. You don't need to load all the files into Emacs in order to find those occurrences. And, once you've found the occurrences of the regexp, you can easily jump to the line in the file with a keystroke.
My favorite way to do this is to use M-x igrep-find because I like the igrep
interface better than Emacs's grep-find
.
You can find the igrep library on emacswiki: igrep.el
And the usage would be
M-x igrep-find case Int => RET
which would populate a buffer with all the matches, and then (like in occur, grep-find, compilation, etc.) you can use C-x ` or M-x next-error to cycle through the matches.
精彩评论