Freiheit
Freiheit

Reputation: 8757

How can I tell IntelliJ's "Find in Files" to ignore generated files?

I need to do a find in files. I want to ignore or exclude generated files, like JAX-WS artifacts or classes in target folders. How can I tell IDEA to exclude these files from the find?

Upvotes: 141

Views: 46653

Answers (7)

KyleMit
KyleMit

Reputation: 29909

Update Version 2024.3

The Find in Files feature has been enhanced with a new search scope, Project Files Excluding Git-Ignored. This option excludes any files ignored in .gitignore files from your search results, helping you focus only on the relevant code when searching through your project.

screenshot of find in files with scope set to Project Files Excluding Git-Ignored

This will automatically take care of filtering out generated files, assuming they've been added to your gitignore.

Upvotes: 2

Paul Chu
Paul Chu

Reputation: 54

Just put the target folder outside your project folder could solve the problem.

Upvotes: -1

Rob Kielty
Rob Kielty

Reputation: 8152

Create a Custom Scope defining the set of files to include/exclude from your search.

  1. CTRL+SHIFT+F for the Find in Files dialog. (Mac users press command+shift+F)

  2. Click on Scope

    Click on Scope

  3. Choose a scope from the drop down list or create a Custom Scope by clicking on the ... button to the right of drop-down.

  4. In the dialog that appears, click on the + button on the top left hand and select Local and give your Custom Scope a name (you can change it later)

  5. The buttons on the right allow you to Include and Exclude individual files and Recursively Include or exclude all files beneath a folder.

    adding a custom scope for handwritten files

The Filter Button icon, bottle filter is useful; clicking on it toggles the file display to show what files are present in the Scope as you create it. Toggle the Filter on and off to ensure that your Custom Scope is as expected.

The Pattern field can be manually tweaked if you needed to refine the files contained in the custom scope further (say if you wanted to target files that were not yet present on disk)

Once created, subsequent Find in Files can make use of your Custom Scope to restrict which files are searched as per your stated needs.

For more details of the 2024 features around Custom Scopes visit

https://www.jetbrains.com/help/idea/2024.1/settings-scopes.html

and for documentation on the pattern syntax used by Scopes see

https://www.jetbrains.com/help/idea/2024.1/scope-language-syntax-reference.html (thanks Didier L for this ref and the feedback that triggered the 2024 update)

Upvotes: 118

yoAlex5
yoAlex5

Reputation: 34225

IntelliJ Skip generated files pattern during searching

I use the next pattern to exclude generated files

!file:*intermediates*/&&!file:*generated*/&&!lib:*..*

[How to add a new pattern follow and skip tests pattern]

Upvotes: 15

nemoo
nemoo

Reputation: 3319

I use this filter to create a custom scope to exclude the target folder in play framework applications:

(src:*..*||test:*..*)&&!file:target//*

Also, this is the language reference for the scope definition language: https://www.jetbrains.com/help/idea/scope-language-syntax-reference.html

Upvotes: 3

alex.b
alex.b

Reputation: 1528

You can also put the search file filter starting with ! sign to exclude. Example to search code not in Test Java files: !*Test.java

If you have a few types of files you can separate with , sign. Example to search in Kotlin and Groovy files only: *.kt,*.groovy

This might be also helpful.

Upvotes: 32

Vala
Vala

Reputation: 5674

I know this is late to the party, and Rob's answer is a decent one. I'd just like to add the following alternative though: if you chose the Custom scope (as in Rob's answer), then leave the selection at Project Files, this will make IntelliJ search a bit more selectively than by default. I don't know what the exact differences are, but of particular interest is that if you mark a directory as Excluded either using the Modules tab in the Project Structure settings, or by right-clicking on a directory and selecting Mark Directory As -> Excluded.

If the files you want to exclude are in a single or relatively few directories so you can easily manually set up these exclusion rules, this is a really nice way of getting the same result without needing to configure a custom scope.

I tested this in IntelliJ Ultimate 14.1.4. I have no idea how it behaves in other versions, but I suspect most of v14 at least will behave the same.

Upvotes: 61

Related Questions