How to exclude a file extension from IntelliJ IDEA search? - intellij-idea

Is there a way to exclude particular file extension from the results in IntelliJ IDEA's "Find in Path" dialog (invoked by CTRL + SHIFT + F)? I want to exclude all .css files.

In intellij 16 there is a section "File name Filter" to exclude an extension use !*.java. You can give more detailed patterns as well for example I use the pattern below to only return .java files except those with a name starting or ending with test.
Pattern: !*test.java,*.java,!Test*.java
In recent versions of Intellij the GUI has been updated a bit but the same still applies see the "File mask" on the top right hand corner see image below:

In "Find in Path" and "Find in Files" (Ctrl+Shift+F) you can use the "File mask" input.
For example, this would exclude all js, css and twig:
!*.js, !*.css, !*.twig
It's handy cause it also keeps a list of previous inputs, but it gets messy easily when you add a lot of extensions. If it gets too crowded, consider excluding some folders as well (right-click -> "Mark directory as" -> "Excluded")

You can create custom scope there:
In 'Find in Path' dialog you can check radio button 'Custom' and open scopes window.
There you can customize path and search pattern.
Examples of Patterns for Pattern field:
!file:*.css - exclude CSS files
file[MyMod]:src/main/java/com/example/my_package//* - include files from the directory in a project.
src[MyMod]:com.example.my_package..* - recursively include all files in a package.
file:*.js||file:*.coffee - include all JavaScript and CoffeeScript files.
file:*js&&!file:*.min.* - include all JavaScript files except those that were generated through minification, which is indicated by the min extension.
Or check the official documentation.
Good luck!

If you are like me that don't want to use the File Mask option or create a custom scope, etc. but just wanted to be able to add it to project settings one time and be done with it, then here's a solution for you.
I wanted my Find in Path to not search in lock files (auto-generated manifest file by package managers), here's what I had to do for that:
Goto File >> Project Structure (or just press Command+;)
Select Modules under Project Settings
Add a pattern or file names to the Exclude files text-box, in this case: *.lock;package-lock.json and hit Apply, and then ok.
Note The above option is available in IntelliJ 2019 and versions after that, not sure about older versions. For more info, https://www.jetbrains.com/help/phpstorm/excluding-files-from-project.html

WebStorm 2021
CTRL+SHIFT+F and follow the pictures

For 2019.2, the pattern is not set in scope but in the right top windows "File mask" in Find in path dialog. You must tick it.
The syntax I tested are:
exclude files with extension css: !*.css
exclude all under out dir: !out/*
Patterns are separated by comma.
Reference: https://www.jetbrains.com/help/idea/finding-and-replacing-text-in-project.html

For 2021.3.2, just right-click to the folder and Mark Directory as -> Excluded. This will work for logs etc but be careful about source folders.

Related

How to find files ending with a specific string in IntelliJ

In IntelliJ, you can list files using the Navigate > File... option (keyboard shortcut Ctrl+Shift+N). Using this feature, if I try to search for Java files ending with IT (which are integration tests), searching for *IT.java will return IterableTransformer.java for example, which is obviously not what I want.
So my question is, I can I do that in IntelliJ?
You can use Find in Path functionality as a workaround: enter an empty search string there, and *IT.java in "file mask" field.

Hide files from /build directory in navigate class/file action

One of the most productive actions in Android Studio is Navigate action. The problem is it shows so much useless files. Sometimes it's hard to find the class/file you're looking for.
Take below image as an example. If you're looking for AndroidManifest.xml in module b you have to scroll down the list to find the file you want.
Is there a way to filter what this dialog shows, in particular exclude some directories from results? All of the files from the /build directory are completely useless.
I know that I can filter what types of files are shown but it doesn't help much.
Mark your build folder as excluded:
File > Project Settings > Modules > Sources > Mark as Excluded (red icon)
Alternatively, right click on your build 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
Another way is to define a custom scope as described here.

Ignore a folder in search results

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?
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.
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:
Edit -> Find -> Find in path or press Ctrl+Shift+F.
Choose Custom in the Scope section and then choose <unknown scope>
Now click on the + button to add a new local custom scope
Give the scope a name and save it.
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.
Note that you can even include or exclude libraries your project is dependent on.
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.
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"
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.
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.
As some have mentioned, simply setting a folder to 'Excluded' doesn't work in many versions. A combination of the following does for me:
Set the offending folder to 'Excluded' as suggested.
Use the pre-defined Scope 'Project Files'
Profit
Excluding files and folders from projects
Article last modified: 08 June 2020
Mark a file as plain text
In the Project tool window, select the desired file.
From the context menu of the selection, choose Mark as plain text:
Exclude files and folders by name patterns
In the Settings/Preferences dialog Ctrl+Alt+S, click Directories .
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.
Source: https://www.jetbrains.com/help/phpstorm/excluding-files-from-project.html
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 > Ignored Files and Folders
I can ignore the generated bla.html with this.
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".

Webstorm not recognising JavaScript file

Webstorm doesn't recognize one of my .js files as a JavaScript file so I'm losing syntax highlighting and being able to add break points. I've looked in the workspace.xml file and the file in question seems to have similar settings to other .js files that work correctly.
If I change the name of the file it works ok. So somehow Webstorm is stuck on misinterpreting the type of a file that has this name. Where else can I edit the project?
So I see three possible reasons for the problem:
The file was marked as 'Plain text'
There is a pattern for 'Text files' file type that matches this file (or back: file type 'JavaScript' exclude this file name). See image below
There is a custom plugin that overrides default behavior for files with this name (unlikely)
Updated: after several years I've found one more reason for the behavior and most likely it the main source of the issues. When you create a file without any extensions the "Register New File Type Association" is appeared. And you can accidentally specify a new file type, for some file name. It is can be fixed with (2) but it is the reason why the pattern was added there.
Note: the solution works for all IDEA-based IDEs: IntelliJ IDEA, WebStorm, RubyMine, PyCharm, PhpStorm.
File -> Settings -> Editor -> FileTypes -> Text files -> check for "ContentRepository.js" pattern there and remove it.
If the file was marked as "Plain Text", then this can be fixed as follows:
Right click on the file.
One of the menu options is "Mark as Javascript" . Just below "Delete".
Click that. Your file is now recognized as javascript by WebStorm.
Since WebStorm was not allowing screenshots after right click so couldn't add it here.
My problem was with Auto-detect file type by context
For me this did the trick
1) Preferences
2) Editor
3) FileTypes
4) Search for javascript
5) Add *.js to registered patterns
You can also try "Template data languages" (in file > settings) which will force a specific language synatax high-lighting on a specific file or folder. See screenshot below
For a block of code you can also use "language injection". Simply high-light the block of code, wait for the little lamp to appear then select the bottom choice and then the language you need (screenshot also attached)
In case it is helpful elsewhere (OMG this was scary).
For some reason IntelliJ was not recognizing any .js file in my project (even after .idea removal (rm -rf .idea)).
I went to Webstorm (in my case RubyMine): Webstorm > Preferences ... > Editor > File Types then went to Recognized Filed Types, picked Javascript (but whatever file type you are missing will do), then added back a Registered Patterns entry of *.js
Of course, press Apply and then Ok.
Wow, scary stuff. but solved my problem, hope it does for someone else.
I was always trying to add .js as a recognised file types, but somehow .js was added to Ignored Files and Folders.
I just removed .js from Ignored Files and Folders and it worked for me.
Preferences
Editor
FileTypes
Ignored Files and Folders
Remove *.js
2022 update!
Right click on file and choose JavaScript

PHPStorm navigate to file by name and path using fuzzy search

I am switching from Vim to PHPStorm. In Vim I had a plugin called CtrlP which could be used to search for files with its path using fuzzy logic.
For example, searching for the string "defaorderwid"
Would suggest the file "themes/default/views/order/widget.php"
(Sublime also has this functionality too ("Goto Anything"), available via Ctrl+P.)
How can I do something similar in PHPStorm? I am aware of Ctrl+Shift+N search, but it searches only names of files without paths.
It is important for me because in current project I have many files named "widget.php" in different folders.
Press Shift, Shift (twice). Then enter widget.php and prepend the folder, or parts of it. Works for me.
They call it "Search Everywhere", or "Double-Shift" feature.
If you want to use abbreviations, you can CamelCase or or CamelHumps or SnakeCase your search, or use Wildcards (*). Read here.
if you have files like:
/buildings/users/create.blade.php
/rooms/users/create.blade.php
/parks/users/create.blade.php
you can search like that:
1. double press SHIFT
2. type: buil/creat (first letters of folder and file)
3. PHPStorm will find: buildings/users/create.blade.php
If you are looking for a file (or directory) you can use:
Windows: Ctrl+Shift+N
macOS: Cmd+Shift+O
(Double tapping Shift works too, but it also searches for classes, symbols and actions, and so is slower.)
Despite what you say, it does search for fragments of directories and filenames --
but only if they're separated by slashes:
Developers have been asking JetBrains for this without the need for "/". Register your desire for it to be added here: IDEA-112841
If you are looking for file in a particular path use Ctrl+Shift+N
whith : themes/d/vs//widget Or t/d/v/o/wid etc ...
will display widget.php file in themes/default/views/order path