120

For a large project with many dependencies e.g. in the node_modules/ folder, I noticed frequent CPU spikes because of Sublime indexing all the files in the folder.

I know I can hide files and folders using the folder_exclude_patterns setting, but I still want the folder to be visible in the sidebar.

How can I keep e.g. node_modules/ in the sidebar, but exclude it from indexing?

Geir Sagberg
  • 9,632
  • 8
  • 45
  • 60

5 Answers5

201

To exclude files from the index but keep them in the sidebar, use the binary_file_patterns setting in your User Settings, for example:

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

Make sure to copy the values from your Settings - Default preferences (here shown as "*.jpg" etc.), or you will start indexing binary files.

Geir Sagberg
  • 9,632
  • 8
  • 45
  • 60
  • 7
    I wanted to accomplish the same thing as OP, but for what it's worth, Sublime Text 3 cpu usage spikes for me with "binary_file_patterns." Unfortunately, I can only get it to calm down by using "folder_exclude_patterns." I am on a late 2013 Macbook Pro. – Brian FitzGerald Dec 16 '15 at 03:59
  • 3
    I've got the same issue as @BrianFitzGerald, have to use folder_exclude_patterns when on OS X. (ST Build 3103, OS X 10.11) – t.mikael.d Apr 06 '16 at 14:12
  • Works for me in the latest Sublime Text version Build 3126 on OSX El Capitan. – JohnnyQ Sep 27 '16 at 08:36
  • `To exclude files from the index but keep them in the sidebar...` This is pure genius! – Mrchief Dec 14 '16 at 17:49
  • 39
    As of March 2017, the Sublime Text 3 preference is `index_exclude_patterns`, e.g. `"index_exclude_patterns": ["*.log","node_modules/**","bower_components/**"]` – Paul Wenzel Mar 20 '17 at 19:24
  • @PaulWenzel I just discovered that setting, but when I do a Find in Files, those show up. Is it only for indexing `meta` constructs like function names? – Michael Apr 04 '17 at 15:04
  • 2
    @Michael I read that `"folder_exclude_patterns": ["name_of_folder"]` might help remove certain patterns from search results, but I haven't tested it. Source: https://coderwall.com/p/bk90bw/exclude-a-directory-from-searching-in-sublime-text-2 – Paul Wenzel Apr 28 '17 at 16:24
  • 3
    @Michael I can confirm that `index_exclude_patterns` doesn't hide `node_modules`'s files from the "Goto Anything" (⌘P) search: `"index_exclude_patterns": ["*.log", "node_modules/**"],` Tested with Sublime Text 3.1.1, Build 3176. – olistik Aug 12 '18 at 08:24
  • 1
    So far the only working solution has been `binary_file_patterns`. – olistik Aug 12 '18 at 08:27
  • Why two asterisks? Also I want to exclude all of a folder except JSON file in that folder. Could you suggest a way for this? – Nishant Apr 27 '19 at 01:22
  • @Nishant Two asterisks are not actually necessary. `/*` and `*/` are treated as globs. – Michael Nov 25 '20 at 20:22
40

You can change your personal settings, in Preferences -> Settings - User, add:

{
    "folder_exclude_patterns":
    [
        ".svn", ".git", ".hg", "CVS",
        "node_modules",
    ],
}
Community
  • 1
  • 1
liut
  • 524
  • 4
  • 4
  • 21
    This is not the solution if you still want the folders to show on the sidebar. – xiao Jul 12 '16 at 19:10
  • I use this to hide and ignore folders with command-P: `"folder_exclude_patterns": ["build/**", ".gradle", "node_modules/**"],` – BYTE RIDER Jun 24 '19 at 15:07
14

Sublime Text 3 now provides a way to exclude files and folders from indexing while keeping them in the sidebar:

  "index_exclude_patterns": [
    "*.log",
    "node_modules/*"
  ]

On my project I observed the following improvement in the indexing status menu after applying changes:

Before:

index "MyApp" collated in 0.70s from 73934 files
index "MyApp" is using 15167488 bytes for 54234 symbols across 1357673 locations

After:

index "MyApp" collated in 0.00s from 137 files
index "MyApp" is using 61440 bytes for 730 symbols across 4763 locations
Killian Huyghe
  • 1,422
  • 9
  • 13
6

Doesn't work in ST3 (Build 3126).

You can show node modules folders in sidebar and hide files inside this way :

"file_exclude_patterns":
[
    ...,
    "node_modules/**"
]

If you want to hide subfolders from each node module :

"folder_exclude_patterns":
[
    "node_modules/*/**"
]

All files inside node_modules will be removed from search, but each node_module subfolder will be still visible in sidebar.

migli
  • 2,692
  • 27
  • 32
0

I thought binary_file_patterns wasn't working, because I am in the habit of right-clicking my top level folder and choosing "Find in folder". folder_exclude_patterns works with this but binary_file_patterns still searches everything - because the "Where" field overrides the setting.

So you can either use the menu option Find > Find in files OR right click-your top level folder, choose "Find in folder" and then delete the text in the "Where" field so it shows the placeholder text "Open files and folders".

Obviously you still have to add this to Preferences/Settings:

    "binary_file_patterns": [
      "node_modules/",
    ],
Michael
  • 8,362
  • 6
  • 61
  • 88
Little Brain
  • 2,647
  • 1
  • 30
  • 54