ChatGPT解决这个技术问题 Extra ChatGPT

Limit File Search Scope in Sublime Text 2

In Sublime Text, I often use Cmd+P/Ctrl+P to search and jump between files.

Often, it would pick up temporary or cached files like .scssc or things in the /tmp folder.

Is there a way that I can limit what is shown in the search result?


A
AGS

Add and edit this in your ~/Library/Application Support/Sublime Text 2/Packages/User/Preferences.sublime-settings file.

// These files will still show up in the side bar, but won't be included in
// Goto Anything or Find in Files
"binary_file_patterns": ["*.jpg", "*.jpeg", "*.png", "*.gif", "*.ttf", "*.tga", "*.dds", "*.ico", "*.eot", "*.pdf", "*.swf", "*.jar", "*.zip"],

"folder_exclude_patterns": [".svn", ".git", ".hg", "CVS"],

The binary_file_patterns removes the files from Goto Anything, but not from Find in Files, as it should based on the comment. Anyone else experiencing this? Know how to fix it? I'm on Ubuntu, build 2221; same thing happens on my Mac (don't know build number).
There's also "file_exclude_patterns": ["*.log"] if you want to ignore certain files rather than folders.
Does this work in ST3? the comments seem different in ST#, it looks like folder_exclude_patterns will exclude from sidebar, but i only want it excluded from goto and find in files
@Michael its the same in ST3. Everyone else: is there a way to exclude the folder without hiding it? I often want to look at the source of some library but not get it in my typical search results.
@Parris I have the same problem. Would like exclude from search bot nut from sidebar.
J
Jay

For Sublime Text 3: To exclude from search and GoTo results, without removing from the sidebar, change the "binary_file_patterns" setting. Matches files AND folders.

For example, to exclude files in "dist" and "node_modules" from GoTo indexing, add this to your User Settings file:

"binary_file_patterns": ["dist/*", "node_modules/*", "*.jpg", "*.jpeg", "*.png", "*.gif", "*.ttf", "*.tga", "*.dds", "*.ico", "*.eot", "*.pdf", "*.swf", "*.jar", "*.zip"]

I can't figure out how to implement this on a per-project basis :(. Most settings can be moved to a project.sublime-project file. "Project > Save Project As", save it the root of your project, and add "settings": {...} to the json in the generated file. (from source, works as of ST3 build 3095). But does not work with "binary_file_patterns".


This does exactly what it should, it behaves like an ignore file if you want to think in that way. Thanks a lot!
But it is not possible to change this per project?
Note: works in Sublime 2.0.2 too. +1 for your good description and showing that folder/** works too. :)
This doesn't seem to work for me. I'm using ST3 build 3095. I cannot edit the Preferences -> Settings - Default, so I added the filter to -> Settings - User. I tried adding "unused/" as well as "*/unused/*" but neither work for me. However, I was able to add an exclude filter like this: -*/unused/*,-*/_sgbak/* to my search, which is annoying because it doesn't remember, so you have to add it each time.
@user2602152 I couldn't get it to work per-project, see updated answer for what I tried
D
David Pärsson

You can exclude certain file patterns and folders from your project by modifying your project settings like so:

{
    "folders":
    [
        {
            "path": "src",
            "folder_exclude_patterns": ["backup"]
        },
        {
            "path": "docs",
            "file_exclude_patterns": ["*.css"]
        }
    ]
}

This is described in the projects documentation.


Works for Sublime Text 3. Does remove the excluded folder from the sidebar, but that's not a problem for my purposes.
What is best way for to hide from search but not from sidebar, per project?
@AdrianoResende As far as I know there is no correct way to do this on a per project basis. Only from the global settings.
D
Daniel Flippance

You can also exclude folders in the Find All pane by using the -*/foldername/* syntax in the Where field - eg:

-*/node_modules/*

http://www.sublimetext.com/forum/viewtopic.php?f=2&t=3847&start=10


Yes! Perfect. I don't want to exclude node_modules from the project altogether, just wanted to filter it out of the search. Rockin!
I don't think this goes in the where field.
Yep, it does go there.
Note that in Windows you'll also have to use the forward slashes (/) for the directory to exlude, even if you use backward slashes for the directory to search in. Example in the Where field: C:\Projects\SearchThis,-*/but/not/this/*
how to exclude multiple folders ?
e
eephillip

In sublime text 3 (BLD 3059 Windows) I needed to restrict the "find in folder" function to certain files / folders and maybe a single file,

The following works for me Contents of the where: box

/C/path/2/project/folder,*.c,*.h,-*/path/not/to/look/in,/C/path/2/specific/file/file.h

Taking it further without absolute paths, you can combine the above with the following symbolic locations

<open folders>, <open files>, <current file>

<open folders>,*.c,*.h,-*/never_this_in_folder/*,<open files>

This should be the accepted answer, it fulfils the question and doesn't hide folders from the UI that you want to look at.
Yes. The other suggestions with folder_exclude_patterns and file_exclude_patterns only hides it from the sidebar, and not from the search or GoTo. Sadly I haven't found any permanent setting for excluding from search and GoTo. So this answer is currently the best workaround.
Actually just verified that it does hide it from the Goto. But it still doesn't hide it in the project wide search.
I just found out that it excludes from find in files if you include these options in your global settings file ("Sublime Text -> Preferences -> Setting - User"). It does not work with the project settings file, so one has to make due with having these settings applied for every project / app.
H
Harsh Vakharia

For SublimeText 2, this is working great for me.

When you choose Find in Files, specify exclude folders in Where input;

-bower_components/**/*, -dist/**/*, -node_modules/**/*, -tmp/**/*

So, a hyphen followed by exclude pattern for folders you don't want to search in.

-folder1/**/*, -folder2/**/*

This will limit your searching scope.

https://i.stack.imgur.com/bHGJq.png


K
Kevin Leary

I think many of these answers span a few different versions of Sublime Text, here's how I do this with Sublime Text 3 on a Mac.

Open the Sublime Text > Preferences > Settings - User menu Edit the file_exclude_patterns and folder_exclude_patterns values to ignore files and/or folders from the Find tool

Example

"file_exclude_patterns":
[
    ".svn",
    ".git",
    ".hg",
    ".md",
    ".txt",
    ".DS_Store"
],
"folder_exclude_patterns":
[
    "node_modules",
    "bower_components",
    ".svn",
    ".git",
    ".hg",
    "CVS",
    "deprecated",
    "cache"
],

Screenshot

https://i.stack.imgur.com/mMeUb.png


I was looking for a global solution (not a per-project one) and trying to differentiate file from folders, I got everything here ! (Using ST3 on Linux Mint)
Thanks for this - I got it working with ST2 on Ubuntu. Quick discrepancy: I had to add * to the "file_exclude_patterns"; e.g., ".txt" -> "*.txt". I followed this: lugolabs.com/articles/…
This works, but also excludes you from being able to open the file/folder in sublime as well. Say you want to always exclude node_modules when doing a folder wide find. However, if you are debugging something and there is an issue with a dependency, it would be nice to have the option to open a file in node_modules
a
afsheenirani

You can also exclude folders from your search via the Where field:

Where: <open folders>,-*/node_modules/*.*,-*/build/*.*

So in my example above:

I am searching through all Open folders. I am excluding the folder called "node_modules" which is a top-level folder right under the root directory for my project. I am excluding the folder called "build" which is a top-level folder right under the root directory for my project.

This works for me in Sublime Text 3 and the folders continue to show in the SideBar. This is a search only exclusion via input (does not affect any behind the scenes indexing).


C
Community

This solution works perfectly for me : https://superuser.com/a/601270

Find: "something" Where: "<open folders>" // <open folders>" not include hidden folder in sidebar

A
Alter Lagos

Just note that if you want to add a subfolder of your project folder, you must to join the folders with \/. Using the same example of @DavidPärsson:

    {
        "folders":
        [
            {
                "path": "src",
                "folder_exclude_patterns": ["backup\/src\/log"]
            }
        ]
    }

M
MEngelby777

For those few times you need to limit the find (and replace) to the current directory only, do this:

c/Path/2/Project,-c/Path/2/Project/*/*

The important bit is /*/* in the path exclude pattern. Using Sublime Text 3 build 3083 on Windows 7 64-bit.


What's the key for that? Where do you place it? (sublime prefs)
@LeonGaban This tip is the 'Where' entry for the interactive panel Find-in-Files. The default keybinding is CTRL+SHIFT+F.
A
Ahmad Awais

I think the easiest way to make sure such files and folders are excluded on each project is to just add the following code in Sublime User Settings (Add and edit this in your ~/Library/Application Support/Sublime Text 2/Packages/User/Preferences.sublime-settings file.)

{
    // Remove certain files permanently from Sublime via Preferences.sublime-settings.
    "folder_exclude_patterns": [".svn", ".git", ".hg", "CVS", "node_modules"]   
}

Gist : https://gist.github.com/ahmadawais/690a816ca158067708ad4dbe17822841

OR you can check my preferences file here https://github.com/ahmadawais/dotFiles/blob/master/SublimeText/User/Preferences.sublime-settings#L80-L81