I just launched a new course on ES6! Use the code WESBOS for $10 off.

Strengthen your core JavaScript skills and master all that ES6 has to offer. Start Now →

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 preferencesbrowse packagesHTML.

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

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

Replace it with this:

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

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

This entry was posted in Sublime Text. Bookmark the permalink.

5 Responses to Fixing toggle comment on script tags in Sublime Text

  1. Steve Fasano says:

    This is exactly what I was looking for! Thanks for posting this fix!

  2. Omid Ahourai says:

    Any idea how to do this in the latest version of ST3? The HTML.tmLanguage is now HTML.tmLanguage.cache, and opens to a binary file (unreadable)

  3. Chris Weiser says:

    You are he who has come to deliver us from evil and coding pain. Thank you so much for this!

  4. Rarian says:

    This! is what I’m looking and searching for with tens of keywords!!! Thank you for escaping me from the chaos.

Leave a Reply

Your email address will not be published. Required fields are marked *