Ignore a folder in search results

Intellij IdeaIntellij 14

Intellij Idea Problem Overview


I'm searching for a string and getting matches in a source folder, and a build folder (file in source gets copied to build during build).

I do not need the build folder result.

Vim has wildignore which helps filter out results.

Is there something similar in intelij?

Intellij Idea Solutions


Solution 1 - Intellij Idea

Mark your build folder as excluded:

File > Project Structure > Modules > Sources > Mark as Excluded (red icon)

You can also just right click on your folder and select Mark Directory As > Excluded.

> Excluded folders (shown as rootExcluded) are ones that IntelliJ IDEA "partially ignores". Very limited coding assistance is provided for files in excluded folders. Classes contained in excluded folders don't appear in code completion suggestion lists, references to such classes are shown in the editor as unresolved. When searching, IntelliJ IDEA doesn't look in excluded folders, etc.

Source

Note: See the answer by Nader Hadji Ghanbari for another approach using Scopes.

Solution 2 - Intellij Idea

Short Answer

By defining a Scope when searching, you can include/exclude arbitrary files/folders from that scope.

Detailed Answer

One way to achieve your requirement (excluding files and folders from a search) is to define a custom scope. This is specifically useful because sometimes you just want to exclude a folder from your search and not from the whole project.

Follow these steps:

  1. Edit -> Find -> Find in path or press Ctrl+Shift+F.

  2. Choose Custom in the Scope section and then choose <unknown scope>

Search scopes

  1. Now click on the + button to add a new local custom scope

Adding a new custom scope

  1. Give the scope a name and save it.

Saving the local custom scope

  1. Now you can include and exclude directories from this scope. You can first add everything by choosing the include recursively and then exclude one by one by choosing exclude or exclude recursively.

enter image description here

Note that you can even include or exclude libraries your project is dependent on.

  1. When searching you can choose the effective scope by in Scope section in Find in Path dialog.

More info

You can check the JetBrains docs on Scopes for more info. Scopes can be used not only when searching but also in a bunch of other use cases in IntelliJ IDEA.

Patterns

You can use Patterns to define a scope which makes them even more powerful and future proof.

using patterns is another way to exclude files and folders. For instance file:src/main/java//*&&!file:src/main/java/my//* will exclude all files in my folder.

enter image description here

Solution 3 - Intellij Idea

In Intellij 15, to exclude a folder just do this:

in the Project window, select the folder then right click and choose "Mark Directory As" > "Excluded"

Solution 4 - Intellij Idea

A number of answers work well for older versions of IntelliJ, but in 2020.2, and possibly other recent versions, I keep seeing excluded folders in my search results. In my case: node_modules.

I found something that does work, though. For javascript projects, at least. There might be something similar available for other languages.

  • First, of course, exclude the directory from your project.

Hopefully this works, but if it doesn't, and you're using Javascript, continue:

  • Go to Settings -> Languages & Frameworks -> Javascript -> Libraries
  • There, uncheck all the directories you don't want included in your search.

Whether this is a good idea remains to be seen; IntelliJ doesn't merely exclude them from your search, but from all its code parsing, which means it might not recognise imports from that directory either. For me it seems to work fine at the moment, but I give no guarantees. Try it, but remember how to revert this.

It's an ugly work around. IntelliJ clearly needs a better way to exclude directories from searches.

Solution 5 - Intellij Idea

Excluding files and folders from projects

Article last modified: 08 June 2020

  • Mark a file as plain text
    1. In the Project tool window, select the desired file.
    2. From the context menu of the selection, choose Mark as plain text: enter image description here

  • Exclude files and folders by name patterns

    1. In the Settings/Preferences dialog Ctrl+Alt+S, click Directories .
    2. In the Exclude files field, type the masks that define the names of files and folders to be excluded, for example, *.min.js or lib. Use semicolons as separators.
      • If the name of a file matches any of these patterns, the file is treated as excluded.
      • If the name of a folder matches a pattern, all the files in this folder and in its subfolders are excluded.

enter image description here


Source: https://www.jetbrains.com/help/phpstorm/excluding-files-from-project.html

Solution 6 - Intellij Idea

IntelliJ IDEA 2021.3.3 (Ultimate Edition)

In my case, all the generated files in .tox and _build kept showing up in my Find results. It may be a blunt instrument but I added these two folder types to the Preferences -> Editor -> File Types -> Ignored Files and Folders: .tox, _build. For whatever reason, adding _build will not be accepted as part of the ignored set.

Caveat, you will not see these files and folders in your Project navigator. However, it’s a small price to pay for not having them show up consistently in my search results.

enter image description here

Solution 7 - Intellij Idea

As some have mentioned, simply setting a folder to 'Excluded' doesn't work in many versions. A combination of the following does for me:

  1. Set the offending folder to 'Excluded' as suggested.
  2. Use the pre-defined Scope 'Project Files'
  3. Profit

Solution 8 - Intellij Idea

If you are searching for usages, open the "Find Usages Settings..." , by default Ctrl+Alt+Shift+F7, to narrow down the scope from "All files" to "Project files".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEric FrancisView Question on Stackoverflow
Solution 1 - Intellij IdeaDarek KayView Answer on Stackoverflow
Solution 2 - Intellij IdeaNader GhanbariView Answer on Stackoverflow
Solution 3 - Intellij IdeaallkenangView Answer on Stackoverflow
Solution 4 - Intellij IdeamcvView Answer on Stackoverflow
Solution 5 - Intellij IdeaMahdi BashirpourView Answer on Stackoverflow
Solution 6 - Intellij IdeaMark DView Answer on Stackoverflow
Solution 7 - Intellij IdeaKevin PageView Answer on Stackoverflow
Solution 8 - Intellij IdeaZsolt Z.View Answer on Stackoverflow