IntelliJ IDEA 2018.3 Help

Structural Search and Replace

A conventional search process does not include the syntax and semantics of the source code. Even if you use regular expressions, IntelliJ IDEA still treats your code as a regular text. The structural search and replace (SSR) actions let you search for a particular code pattern or grammatical construct in your code considering your code structure.

IntelliJ IDEA finds and replaces fragments of source code, based on the search templates that you create and conditions you apply.

Search for a target structurally

  1. From the main menu, select Edit | Find | Search Structurally.

  2. In the Structural Search dialog, do one of the following:
    • Create your own template from scratch.

      In the editor area, enter the code pattern ($variable$ that represent your code), click the artwork studio icons logcat toolbar settings icon and select Save Template to save it for future use.

      Structural Search dialog
      IntelliJ IDEA adds the created template to the existing template list (User Defined node) in the Existing Templates dialog.
    • Use one of the existing templates to act as a prototype.

      Click the artwork studio icons logcat toolbar settings icon, select Existing Templates to open the list of predefined templates and select the appropriate one.

      For example, you have the following fields in your code:

      public class MainActivity { public static final String this_is_wrong = "Hello"; public static final String THIS_IS_CORRECT = "world"; }

      Let's find a certain field in the class.

      In the list of existing templates, click Java and open the Class-based node (since we need fields in the class), so the fields of the class template would be our target.

      Existing Templates dialog
      Click OK to return back to the Structural Search dialog to work with the template you have selected further.
  3. If you need to add some condition for the template, for example, add some regular expressions or a script constraint, in the editor area, place a caret at the code item in question. The popup with the Edit Filters link appears.

    Edit filters popup

  4. Click the link to open the filter area where you can add your conditions. For example, let's add a condition for the $Field$ variable.
  5. In the filter area, click the Add icon to add a new condition. If, for example, you need to add regular expressions, select Text. You can also add other conditions depending on your variable.

  6. In the field that opens, type your condition.
    Add regular expression to the filter dialog

    For example, let's type the following regular expression:


    In this case when you select the Match case checkbox in the Structural Search dialog, IntelliJ IDEA will only search for lower case words.

    Also note, that by default, the Recursive checkbox is selected. That means that IntelliJ IDEA performs the recursive search and all nested items will be included in the results.

  7. Specify the following options to narrow the search further:
    • File type - use the drop-down list to select which file types should be a part of the search. In our case, it is Java.

    • Scope - specify where to search: in a project, module, directory or select a custom scope.

    • Search target - you can select Complete match to match the exact template or other available code elements (in our case it is Field).

      Search target

  8. Click OK.

IntelliJ IDEA displays the results in the Find tool window.

Find tool window results

You can add the newly created search template to structural search inspections as a custom template and use it later to inspect your code.

Replace a target structurally

  1. From the main menu, select Edit | Find | Replace Structurally.

  2. In the Replace Structurally dialog, add new or existing templates to the search and replace template areas. You can save the replace template the same way as the search one.

  3. If you need to add a filter for the variable in the replace template, place a caret at the variable of interest and in the popup that appears, click the Edit filter link.

    Replace template filter

  4. In the filter area, depending on what your chose as a filter, specify the condition.

  5. To narrow down your replace results, select the following options:
    • Shorten fully-qualified names. Replaces fully qualified class names with short names and imports.

    • Reformat. Reformats the replaced code.

    • Use static import. Uses static import in replacement when possible. For example, a method call to a static method Math.abs(i) becomes abs(i) if this option is selected.

    After you are done specifying the necessary options, click Find. IntelliJ IDEA displays the results in the Find tool window.
  6. In the Find tool window, your can work with the results further, replacing found items one by one, all of them at once, or you can check a preview of your potential changes.

    You can also add the replace template to the structural search inspections and use it as a quick fix for your code.

Last modified: 1 February 2019