RubyMine 2026.3 Help

Structural search and replace

A conventional search process does not take into account the syntax and semantics of the source code. Even if you use regular expressions, RubyMine 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.

RubyMine 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. In the main menu, go to Edit | Find | Search Structurally to open the Structural Search dialog.

    To quickly switch to the Structural Replace dialog, click the Switch to Replace icon.

  2. In the Structural Search dialog, do one of the following:

    • Create a template from scratch: select Draft Template from the list of templates and enter the code template (for example, $variable$ to represent your code) in the editor area.

      To save your custom template for future use, click the Save Template icon (the Save Template button) on the dialog toolbar. You can also choose to save the template as an inspection.

      Structural Search dialog

      RubyMine adds the created template to the Recent node in the template list.

    • Use one of the existing templates to act as a prototype: select the necessary template from the list of available existing templates.

      Consider the following style definitions added to an HTML file:

      <style> #element1 { display: inline-block; width: 25%; margin-top:90px; } #element2 { display: inline-block; width: 40%; margin-top:10px; } </style> <div id="element1">Microposts</div> <div id="element2">Followers</div> <div id="sidebar">Profile</div>

      Let's find all the references to the style elements included into the <div> tags of your HTML file.

      From the list of existing templates, select html attribute value under XML /HTML.

      the Existing Templates dialog

      Click Find to search for these constructions in the entire project.

      RubyMine instantly highlights the found code occurrences in the editor.

  3. The Structural Search dialog shows the selected template and the values of its filters. You can edit existing filters or add new conditions, such as regular expressions or script constraints. Place the caret on a code variable and use the filter area to manage its filters.

  4. In this example, set the following conditions:

    • for $tag$ – text=div

    • for $attribute$ – text=id

    • for $value$ – text=element[1-9]

    Add regular expression to the filter dialog
  5. Specify the search scope: a project, module, directory, or a custom scope.

  6. Click Find.

    RubyMine shows the results in the Find tool window.

    Find tool window results

    You can add the new search template to structural search inspections as a custom template. To do this, click Create Inspection from Template in the Find tool window. You can then reuse it to inspect your code.

Replace a target structurally

  1. In the main menu, go to 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 the caret at the variable of interest and use the filter area to manage filters.

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

  5. In the Replace template area, enter the code to replace the found fragments with. You can reuse the variables from the search template. You can save the replace template the same way as the search one.

    Search and replace templates
  6. To format the replaced code according to your code style settings, select Reformat. Then click Find. RubyMine displays the results in the Find tool window.

  7. In the Find tool window, you can work with the results further, replacing found items one by one, or all of them at once, or previewing your potential changes.

    Replace preview

Share search templates

You can share a search template with your peers by exporting or importing it.

  1. In the Structural Search dialog (Edit | Find | Search Structurally), create a new search template or use the existing one.

  2. To export a template, click the Export Template to Clipboard icon. RubyMine adds the XML representation of the template to a clipboard (press Ctrl+Shift+V to see the clipboard's content). You can share this representation with other developers in chat, email, or a forum.

    To import a template, copy (Ctrl+C) the shared XML code from anywhere (email, chat, or a forum) and in the Structural Search dialog, click the Import Template from Clipboard icon. RubyMine takes the XML code representation and converts it into a template including variables and a scope if it is present.

29 September 2026