9

How can I selected, for instance, 5 lines of code, and replace all instance of myObject1 with myObject2?

If subsequent lines of code contain myObject1 reference that I wish not to change. This way I can quickly apply of find & replaces in this scope of code very conveniently without worrying about side effects.

Is this possible?

2 Answers 2

13

Yes, it is possible.


Classic UI

  1. Open the "Find and Replace" dialog box either by choosing in the top menu Edit > Find > Replace or using the dedicated shortcut Ctrl+R ( R on Mac).
  2. Enter your search and replace terms.
  3. Select some area in your code.
  4. Press the dedicated button to only search within a selection: enter image description here

Now if you press "Replace All", only the items found inside your selection will be replaced.


New UI

In the new UI of IntelliJ IDEA the "Search In Selection" option is in another place (kudos to @Chris for pointing this out):

  1. Open the "Find and Replace" dialog box either by choosing in the top menu Edit > Find > Replace or using the dedicated shortcut Ctrl+R ( R on Mac).
  2. Enter your search and replace terms.
  3. Select some area in your code.
  4. Open "Filter Search Results" dialog by pressing the dedicated button and click on the first option "Search In Selection". enter image description here

Now if you press "Replace All", only the items found inside your selection will be replaced.

NOTE

You can also open the "Filter Search Results" dialog using the shortcut (Ctrl+Alt+F on Windows/Linux, F on macOS), but in this case it is important that the cursor is inside the "Search" field.


In similar way you can perform just a search (i.e. without replace) inside a selected area in your files. Look inside the Edit > Find menu for more interesting options.

Here is the official IntelliJ IDEA documentation that covers this topic: Find in selection.

5
  • For everyone whos missing the icon shown here: In the new IntelliJ UI the icon is gone, and the action was moved under the filter icon instead. However the double CTRL + R (Command + R) still works.
    – Chris
    Commented Jun 13, 2023 at 8:38
  • @Chris Thank you for pointing this out. I have updated my answer for the New UI. Commented Jun 18, 2023 at 0:15
  • Beware, the filter icon is disabled during indexing. The message indicating this is shown only when using the keyboard shortcut. Commented Nov 20, 2023 at 15:48
  • By the way, are there more shortcuts to help? E.g. jumping to the "replacement" part of the thing is annoying. And a shortcut for "replace all button" in the end AND for jumping back would be also helpful
    – IceFire
    Commented May 22 at 8:02
  • @IceFire To jump from to the "replacement" part just use Tab, and Shift+Tab to jump back to the "search" part. To trigger "Replace All" button - press Alt (Windows and Linux) or Option (macOS) and a (e.g. option+a). See the following page on the JetBrains Support site for more details: Replace all keyboard shortcut. Commented May 22 at 16:10
-2

for me it is CTRL + H or CTRL + SHIFT + H

1
  • This answer is not correct. Ctrl + H by default opens Type Hierarchy (top menu Navigate > Type Hierarchy). Commented Mar 15, 2023 at 0:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.