15

Very often while coding, I need to search for keywords which are common and happen throughout a large file, but I am looking for an instance within a function.

The default search functionality often gives me all results starting from the top. Is there any way/workflow to search only within a specific method/function or block of code?

tsar2512
  • 2,826
  • 3
  • 33
  • 61
  • 1
    There is an option to search **In selection** when searching text [within a file](https://www.jetbrains.com/help/idea/finding-and-replacing-text-in-file.html). Or use [Find in Path](https://www.jetbrains.com/help/idea/finding-and-replacing-text-in-project.html) (you can select current file as a scope) and it's preview pane to quickly eye-filter the found matches. – Andrey May 31 '18 at 16:11
  • **In selection** option is automatically enabled when you select the portion of text and then Ctrl/Cmd+F. – Andrey May 31 '18 at 16:31
  • @Andrey - if you do that it tries to search for the entire text you have selected. Could you show using some screenshot? Perhaps I am not correctly understanding what you mean? – tsar2512 May 31 '18 at 18:37
  • Oh sorry, looks like it works only for Replace action and when you select one or more rows :( – Andrey Jun 01 '18 at 08:14

4 Answers4

6

Update

  • Do Ctrl + F
  • Then Ctrl + Alt + E
  • Type the text to search

On MacOS, it's cmd instead of Ctrl


Original Post

You can do it like this (mentioned keymaps are for Linux):

Let's take as an example the following code, where we will be searching for the word "key" inside the method "clear". As you can see there are many occurrences of that word (in red, highlighted the ones we want in our results, and in blue other occurrences, which we do not want to appear in our search results)

Image of IntelliJ IDEA Editor with the searched word

1- Select the area you want to limit the search scope to.

Image of IntelliJ IDEA Editor with the searched word scoped inside some area

2- Press Ctrl+Shift+F. It will pop up a search window with the selected text pre-filled in the search box.

3- Select the options "Scope" and, in the combo next to it, "Selection" (if they are not selected by default)

Image of the pop up shown by IntelliJ IDEA for searching the word in the selected scope inside the current editor

4- Type the word(s) you want to search (in this example, the key word).

Example of a search by the key word

5- The results shown will be limited to the selected area. Notice how there are only 9 matches in the upper right corner ("Match case" option is checked), despite in the same file there are many other occurrences of the same word.


However, I was neither able to find a key combination in order to do this as a single action nor assign a new one. Also, notice how the search includes the text in the method documentation.


Tested using IntelliJ IDEA 2018.1.4 (Ultimate Edition), Build #IU-181.5087.20 on Ubuntu 18.04

lealceldeiro
  • 14,342
  • 6
  • 49
  • 80
  • 1
    This is a pretty detailed answer, and I think the best possible way to do the search action given their current mechanisms. Thanks for the effort! – tsar2512 Jun 01 '18 at 12:10
3

Looks like we're finally getting this feature in IDEA 2019.3:

https://blog.jetbrains.com/idea/2019/09/whats-new-in-intellij-idea-2019-3-eap-2/ (Ctrl-F for subtitle "Ability to search in a selected area when using the Find action")

It’s now possible to search only in a selected area using the Find action (cmd+F / Ctrl +F). Simply select the code piece you need in the editor and invoke the Find action and the IDE will perform the search only in the selected area.

Screenshot from the article (was a GIF originally)

enter image description here

leveluptor
  • 3,144
  • 3
  • 14
  • 21
2

There is a way to search inside a method, however, it doesn't seem to work for other scopes.

When searching in file as usual, click "Find all" find all button in search toolbar

You'll get a list of results in the bottom panel. If you enable "Group by file structure" (I had it disabled by default), you can get a list of matches for a particular method. group by file structure button in search results panel

leveluptor
  • 3,144
  • 3
  • 14
  • 21
  • thank you ... but uggh.. nothing with key mapping ? :P – tsar2512 May 31 '18 at 15:19
  • @tsar2512 yeah, not the most convenient way :) don't know about single-keybind solutions, unfortunately. I saw your comment for https://stackoverflow.com/a/6211012/2807168 and didn't find this functionality in my IDEA too. – leveluptor May 31 '18 at 15:27
0

On IntelliJ in Mac:

  1. In a file select the specific block in which we want to find something
  2. Press CMD + F
  3. Press CTRL + OPTION + G
  4. Notice that the cursor has automatically landed in the find toolbar's text field
  5. Type in what is intended to be found
  6. Press enter to move the cursor to the first occurrence.
NSaran
  • 561
  • 3
  • 19