150

How can I search and replace a String in all files of my current project?

Let's say I have the string "/sites/default/" now I want it to be "/public/sites/default/", but there are almost 1000 files.

chrisemb
  • 79
  • 5
RickMx
  • 1,519
  • 2
  • 10
  • 4
  • 3
    Little suggestion, consider to replace this string for some global variable or property. Replacing some path in 1000 files is sign of poor design. ;) – Radium Apr 03 '14 at 14:28
  • 1
    Possible duplicate of [Is there a way to find/replace across an entire project in Eclipse?](http://stackoverflow.com/questions/3426049/is-there-a-way-to-find-replace-across-an-entire-project-in-eclipse) – Chaminda Bandara Nov 09 '16 at 11:51
  • @Radium: But in few cases, it's handy like if I wanna change '@Test' to '@Ignore' for 10k test cases across the project, and then change one back to '@Test' which I am fixing right now. – Abhishek Singh Aug 08 '17 at 09:53

10 Answers10

271
  • Goto "Search"->"File" from menubar at the top left
  • Enter text, file pattern and projects
  • Click "Replace..." button at the bottom
  • Enter new text click ok

Voilà...

Mahi
  • 1,164
  • 17
  • 24
Tonny Madsen
  • 12,628
  • 4
  • 31
  • 70
  • 36
    Thank you I solved this way. 1.- Select the text to search 2.- Go to Menu Search >Text > Project, a listing of ocurrences pops up, within the search view. 3.- Right Click on the root of that list, 4.- Select Replace All... 5.- Input the text to replace for. 6.- Done ..... thank You.. – RickMx Jul 23 '11 at 17:54
  • Unfortunately this does not help for multi-line text! :( – Eric Tobias Jan 08 '15 at 12:57
  • 1
    @Eric It works if you use regular expressions! You can even highlight the text you want to replace, and as long as the Regular expression option is active before you run the dialog, it'll cover a lot of the formatting for you =) – nikodaemus Oct 06 '16 at 19:14
  • 1
    For more granularity you can do the "File Search" then make multiple selections, right-click and go to "Replace Selected...". For a full answer and example with screenshots see here: https://stackoverflow.com/a/50283848/4561887. – Gabriel Staples May 11 '18 at 01:53
14

Depending on the file type you are focused on, Ctrl+H will open up different types of search screens.

A more consistent hotkey would be using the Alt method: Tap Alt, then A, then F.

Efficient Order of Operations:

  1. Ctrl+C the text you want to do the replacing (if available)
  2. Highlight the text you want to be replaced
  3. Tap ALT, then A, then F. Brings you to File Search. The selection from (2) will auto-fill the search box
  4. In the “File name patterns” input box, type in “.java” for replacing all Java files or type in "" to replace in all files
  5. Click “Replace…”
  6. Ctrl+V (Paste). Or type in the value you want to do the replacing
  7. Enter

You can find more details in my blog post: http://blog.simplyadvanced.net/android-how-to-findreplace-in-multiple-files-using-eclipse/

Anonsage
  • 8,030
  • 5
  • 48
  • 51
  • step by step helped me, only In the “File name patterns” input box, type in “*.java” rather than .java and Ctrl+ H can use as shortcut – nick Aug 10 '17 at 07:04
6

Use Ctrl+H for opening Eclipse search dialog, select appropriate search tab and select "Replace..." to get you to the "Search and replace" dialog

Roland Tepp
  • 8,301
  • 11
  • 55
  • 73
5

Strange but it is a two step task:

  1. Search what you want
  2. In the search tab right click and select replace , or replace all:

A demo at:

http://www.avajava.com/tutorials/lessons/how-do-i-do-a-find-and-replace-in-multiple-files-in-eclipse.html

Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
5

Tonny Madsen said it right, but sometimes this is too simplistic.

What if you want to be more selective in your replacements since not all replacements are correct for what you're trying to do?

Here's how to get more granularity to do the replacements only in certain folders, files, or instances:

First, do like he said:

  • Click Search --> File... OR press Ctrl + H and choose the "File Search" tab.
  • Enter text, file pattern and choose your Workspace or Working Set.

Then:

  • Click Search
  • When your results come up, make some folder, file, or instance selections by Ctrl + clicking on the ones you'd like to select. Ex: here's my selection. I've chosen 3 instances, 1 file, and 1 folder:

enter image description here

  • Now, right-click on your selection and go to --> Replace Selected.... Here's a screenshot of that:

enter image description here

  • Enter what you'd like to replace it "With". In my case you can see it says it is "Replacing 190 matches in 4 files". Now click OK.

enter image description here

Voilà!

References:

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
3
ctrl + H  will show the option to replace in the bottom . 

enter image description here

Once you click on replace it will show as below

enter image description here

Nikhil Kumar K
  • 1,089
  • 10
  • 13
1

There is an option in search => file and shortcut is Ctrl+H. Go for further refer follow link. This is work fine with Eclipse Neon

Is there a way to find/replace across an entire project in Eclipse?

Chaminda Bandara
  • 2,067
  • 2
  • 28
  • 31
0

If you want to replace two lines of code with one line, then this does not work. It works in notepad++. I end up open all files in notepad++ and replaced all.

Feng Zhang
  • 1,698
  • 1
  • 17
  • 20
0

Ctrl+F gives me Find/Replace dialog box.

Or you can,

First Alt+A

Next Alt+F

Then press on Replace button.

If non of them worked:

Goto -> Window -> Preferences -> General -> Keys and search for replace then you will see binding for Find and replace. In the bottom of that window, you can add your key to Binding text box. There you can add or edit any keys as shortcut.

Blasanka
  • 21,001
  • 12
  • 102
  • 104
-8

I have tried the following option in Helios Version of Eclipse. Simply press CTRL+F you will get the "Find/Replace" Window on your screen

enter image description here

Chinmoy
  • 493
  • 1
  • 5
  • 16
  • 2
    this would not replace in all files – Sergei Krivonos May 12 '16 at 13:12
  • This is an option in search => file and shortcut is (Ctrl + h). Go for further refer follow link. [Is there a way to find/replace across an entire project in Eclipse?] (http://stackoverflow.com/questions/3426049/is-there-a-way-to-find-replace-across-an-entire-project-in-eclipse) – Chaminda Bandara Nov 09 '16 at 11:46