Posted in Development | Posted on 03-16-2009 | 11,566 views
Ok, a big thanks goes out to Scott Stroz for this one. I've been using Eclipse for a while now but never needed to do a multi-file search and replace until last week. Turns out it is rather simple, but I had never really noticed the feature before. I figure a quick blog post may help others, and since I know I'll probably forget in six months, it will help me as well.
First, select the root folder in Navigator:

Second, go to Search/File in the navigation menu.

Enter your text in "Containing text" (I entered UDF). Note the Scope. This is critical. It should have "Selected resources" picked. If it is disabled, it means you didn't click in the Navigator first. This bugs/confuses the heck out of me. If you do 2 multi-file search and replaces, it is easy to forget to click back here. It is like Eclipse 'forgets' you had chosen a folder before.
Now, instead of hitting Search, click the Replace... button. Obvious, I know, but I swear I had never noticed it before.
Eclipse will now automatically run the search. You will be given a list of results, and the chance to enter your replacement term.

Just type in the term and hit Replace or Replace All.
So far this has worked perfectly for me. I did screw up one time and forgot to change the value on the "With" field, but that was entirely user error, and not Eclipse's fault. Also don't forget what I said about clicking back in the Navigator before you do the search.


I use the Workspace Scope (search all open projects) and the Enclosing Projects ( current project you are working with ). Also the shortcut to this dialog is Ctrl-H (win)
What kills me about the search view is I can not for the life of me find out what the shortcut is to easily jump to the next (or previous match). I know it is ctrl+K for a file search, but that doesn't work in the search view across multiple files.
Another thing that bunches my panties is the single file search doesn't start at the top when it reaches the bottom of the document so you have to remember to Ctrl+Home to start at the top before searching. Ahhhhh!
If anyone has solutions for the last two questions, please share.
Once the list of results is displayed in the search view, you can right-click on an item (or whatever you Mac guys do) in this view and both "Replace Selected..." and "Replace All..." will appear as options in the context menu. I find this to be more flexible, as you can Ctrl-click to highlight multiple specific matches, and also remove individual matches from the search results before performing the replacement.
The enhancements to the search view are actually one of the biggest reasons I prefer 3.4 to previous versions of Eclipse, as the individual "hits" for each file are displayed, whereas in 3.3 and earlier there is only a count of the instances found in each file, and no context is shown.
@Dallas, if the search view is active, Ctrl+. and Ctrl+, will move you to the next and previous matches, respectively. Also, regarding your question on the Find command (the single file search), you can check the "Wrap Search" box, which I think will do what you want (jump to the top of the file after reaching the end).
http://cfteeps.blogspot.com/2009/01/eclipse-annoya...
Although, sadly ctrl + and - are not working for the search view. Any ideas what I might be doing wrong? I've even tried the keys with the search view maximized to make sure it was active. Nothing happens.
msthanks.
The "Replace..." button should have additional spacing to set it apart from Cancel/Search. Also, the (mostly useless) "Customize..." button adds to the clutter making the useful button harder to find.
Only the positive side, Eclipse does get the button ordering consistent with the OS by putting the Search button far right on OS X and the Cancel button far right on Windows.
First, make sure you are using the correct shortcuts -- the "next" command is ctrl-period, and the "previous" command is ctrl-comma (from your comment, it looks like you are trying to use ctrl-plus and ctrl-minus instead).
Also, which version of Eclipse are you running? I typically run 3.4, where this behavior works as expected, but I just tried it in 3.3, and the results were inconsistent (e.g., focus was sometimes changed from the search view back to the editor, at which point the shortcuts no longer work). I note that under 3.4 these keyboard shortcuts are listed next to the commands in the search view context menu, whereas in 3.3 they are not...
@Juerg, you can restrict a search to a selection of directories/files by defining a working set. In Ray's screen shot of the Search dialog, note the "Working set" option in the Scope section -- click on the "Choose..." button, and you will be able to define a working set that consists of only the resources you wish to include.
Actually ctrl+k behavior is controlled by the settings in the find dialog (ctrl+f). So in find dialog if "Wrap search" is selected, Ctrl+k will start searching from beginning of file when it reaches the end.
[Add Comment] [Subscribe to Comments]