Fixing toggle comment on script tags  in Sublime Text

Fixing toggle comment on script tags in Sublime Text

Toggling comments in Sublime Text is broken and it's time you fix it. If you work with HTML/JavaScript you have probably run into this while using the ⌘ + / comment toggle.

broken-toggle

Sublime Text thinks it is JavaScript, so it uses a JavaScript comment! The problem is that the script tag is acutally HTML, so we need to use an HTML comment to comment it out. We want something like this:

<!-- <script src="jquery.js"></script> -->

The underlying problem is inside the HTML syntax file and it is actually an easy fix.

We need to edit the HTML.tmLanguage file that is located in preferences → browse packages → HTML.

Open it up and look for a line that looks like the following. It should be around line 286.

<string>(?:^\s+)?(<)((?i:script))\b(?![^>]*/>)</string>

Replace it with this:

<string>(?:^\s+)?(<)((?i:script))\b(?![^>]*/>)(?!.*</script>)</string>

Save. You should now be able to properly toggle comment out script tags in Sublime Text!

Find an issue with this post? Think you could clarify, update or add something?

All my posts are available to edit on Github. Any fix, little or small, is appreciated!

Edit on Github

Syntax Podcast

Hold on — I'm grabbin' the last one.

Listen Now →
Syntax Podcast

@wesbos Instant Grams

Beginner JavaScript

Beginner JavaScript

A fun, exercise heavy approach to learning Modern JavaScript from scratch. This is a course for absolute beginners or anyone looking to brush up on their fundamentals. Start here if you are new to JS or programming in general!

BeginnerJavaScript.com
I post videos on and code on

Wes Bos © 1999 — 2024

Terms × Privacy Policy