302

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?

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
Eric Francis
  • 23,039
  • 31
  • 88
  • 122
  • For more current versions of Intellij see the last answer https://stackoverflow.com/a/64927182/426805 – jgreen May 20 '21 at 17:18

9 Answers9

433

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.

Darek Kay
  • 15,827
  • 7
  • 64
  • 61
  • 1
    I don't have File > Project Settings in intellij 14. I have excluded some folders and they are not included in search. – Eric Francis Mar 12 '15 at 16:26
  • 34
    Including a folder from project is only acceptable if that folder should be really ignored. Sometimes you don't want to exclude the folder, but you want simply to exclude it from the search. – Nader Ghanbari Mar 14 '15 at 14:19
  • 59
    I just tried this in IntelliJ 15.0.6 and excluded folders still show up in search results. – Steven Byks Jul 27 '16 at 15:32
  • 2
    Most likely if you're asking the question that OP is asking, this is actually the answer you're looking for. – subdigit Aug 18 '17 at 20:19
  • This works. It is not "Project Settings" in my version of intellij (2017.2) but "Project Structure" – pcatre Oct 07 '17 at 08:18
  • 1
    how do you exclude regex match folders? I want to exclude all folders named `.blah` in my entire project from search. there are many of these folders name this under many modules – red888 Jan 15 '19 at 00:21
  • File > Settings > Directories – Paul Brittain Jun 21 '19 at 15:30
  • 21
    This doesn't appear to be working in Intellij 2019. Excluded folders still show up in search results. – jmrah Mar 31 '20 at 12:13
  • 1
    unfortunately this cannot be put into version control, as it is stored to a file that has hardcoded local directories.. any solution to this? – Paschalis Apr 22 '20 at 16:49
  • 9
    Sadly this doesn't work in Ultimate 2020.2. My search results are often swamped with results from node_modules. – mcv Dec 04 '20 at 10:01
  • 5
    @mcv I've noticed this behavior recently, but it depends on the location type. "In project" ignores my node_modules folder, but when choosing the root "Directory" (which contains node_modules), it will include the results from node_modules as well. – Darek Kay Dec 07 '20 at 10:38
  • @DarekKay Exactly. My problem is that I'm working on a code base that consists of several node projects, so I frequently want to search in only the back-end or only the front-end, and I immediately get tons of results from the node_modules for those. I can understand that sometimes you might want to search those too, but it would be nice to have that option in the search popup. – mcv Dec 07 '20 at 13:12
  • 2
    Intellij 2020.3 was showing issues after this, but I fixed it by REMOVING and RE-ADDING excluded marking to my directories from Project Structure (I hope that will work from anywhere). Just remove the exclusion, click apply, mark again as excluded and hit apply. – Bhavesh Agarwal Apr 06 '21 at 13:05
  • This answer worked for me. Well...not right away. When I first tried it, I saw files in my search results that lived in directories I had marked Excluded. After struggling for a while, I reached out to Jetbrains support. They confirmed that the behavior I was expecting was correct. They suggested that I first try restarting PyCharm. I'd already tried that. They then said that as a last resort, I should **clear all of the PyCharm Caches.**. I did this, and now Excluded directories are not searched by any of the standard PyCharm search modes. – CryptoFool Aug 24 '22 at 23:53
137

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

Nader Ghanbari
  • 4,120
  • 1
  • 23
  • 26
  • 2
    Looks like Idea 15 has you to click on "..." beside _Custom_ instead of choosing ``. – mikebridge Dec 04 '15 at 20:30
  • how can i exclude files from search everywhere? – ycomp Aug 18 '17 at 05:45
  • Waaay too much work! [allkenang's answer](https://stackoverflow.com/a/44984669/715077) below solves the problem in **two** clicks. That is faster than reading the short version of this answer. – Superole Sep 04 '17 at 15:31
  • 6
    @Superole true, the same as the accepted answer, but it does literally exclude the folder from the project (it won't be a source or test folder anymore) and that's not what you always want. Excluding a folder totally from a project or just excluding it just for a specific search are two different things (that's why some people find this answer useful). By the way on a side note, if you use Maven for Java projects or SBT for Scala (or the like of these for other langs) you would probably never want to exclude a folder because everything is already nicely done for you automatically. – Nader Ghanbari Sep 28 '17 at 20:34
  • Thanks for teaching me about scopes, @NaderGhanbari! I didn't realise IntelliJ had this functionality. – Jan Molak Jun 11 '20 at 21:08
  • For IntelliJ v.2021.3.3, Step 2: Click `Scope` and then click on `...`(three dots) – Sameer Jun 29 '22 at 12:21
46

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"

allkenang
  • 1,511
  • 15
  • 12
  • 4
    Ahh. Thank you! This was a million times easier than [the accepted answer](https://stackoverflow.com/a/29049768/715077)! I tried creating a scope first, but that was a nightmare of scrolling, clicking, expanding, toggling, including and excluding. – Superole Sep 04 '17 at 15:24
  • In a Maven project, this setting doesn't persist after changing branches with a separate app. – Pr0methean May 21 '18 at 21:46
  • 24
    Doesn't appear to be working in IntelliJ 2018 - it is marked as excluded but still shows in results :/ – Chris Nevill Oct 15 '18 at 12:36
  • You are the best, buddy!! – JGutierrezC Jan 05 '21 at 03:57
36

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

Mark D
  • 919
  • 8
  • 6
  • 1
    That's currently the most useful one imo. The rare occasions you need to access a file in build folders, node_modules, etc. you can also use the file explorer. – Jakob Apr 25 '22 at 15:13
  • This looks like the best solution to me, just add build to the ignore list and happy days. – D.Rosado Dec 20 '22 at 15:30
  • this method solved the problem with incorrect/useless `find method usages (Control+LMB)` search results, it was showing me useless search results inside compiled js code in `dist`, context: typescript project, WebStorm 2022 – grexlort Jan 16 '23 at 09:33
11

This one the only thing that works for me on IntelliJ 2022.3.1, even when marking directory as excluded didn't work (it only change the icon as red, but still shows up).

File > Preferences > Editor > File Types > Ignored Files and Folders

I can ignore the generated bla.html with this.

the only one that works

Kokizzu
  • 24,974
  • 37
  • 137
  • 233
  • 1
    I believe it's `File > Preferences > Editor > File Types > Ignored Files and Folders` – red Jul 30 '23 at 14:30
6

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
Kevin Page
  • 191
  • 2
  • 7
5

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

Mahdi Bashirpour
  • 17,147
  • 12
  • 117
  • 144
5

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.

mcv
  • 4,217
  • 6
  • 34
  • 40
0

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".

Zsolt Z.
  • 592
  • 6
  • 17