Skip to content

Instantly share code, notes, and snippets.

@OdatNurd
Created July 5, 2021 05:38
Show Gist options
  • Select an option

  • Save OdatNurd/9d36e9850abd3da7ea9ff286bedb8c33 to your computer and use it in GitHub Desktop.

Select an option

Save OdatNurd/9d36e9850abd3da7ea9ff286bedb8c33 to your computer and use it in GitHub Desktop.
Enable syntax highlighting on large files in Sublime Text 4
import sublime
import sublime_plugin
class LargeFileLoadListener(sublime_plugin.EventListener):
def on_load(self, view):
if len(view) >= 4194304 and 'Plain text' in view.settings().get('syntax'):
first_line = view.substr(view.line(0))
syntax = sublime.find_syntax_for_file(view.file_name(), first_line)
view.assign_syntax(syntax.path)
@OdatNurd

OdatNurd commented Jul 5, 2021

Copy link
Copy Markdown
Author

This plugin is featured in the video on enabling syntax highlighting on large files in Sublime Text 4 on my YouTube Channel.

As a performance enhancement in ST4, files larger than 4mb are loaded but not syntax highlighted, which drastically cuts load times. If you work on large files and don't want to manually apply syntax highlighting, this plugin will enable this automatically.

IMPORTANT NOTE: In a forthcoming version of ST4, there will be a setting that controls this behaviour, which makes this particular plugin obsolete except as a demonstration of using the new find_syntax_for_file() API endpoint. Before using this, check out the default preferences to see if that setting exists yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment