By josh / July 18, 2013

Fix Sublime Text’s quirky PHP, SASS variable selection

Editing PHP and SASS variables is awkward in Sublime Text. Both languages use a $ prefix for variables, but Sublime pretends not to know that.

When you try to select a variable, either by double-clicking it or with your favorite keyboard shortcut, Sublime selects the text portion of the variable but not the prefix. This isn’t too bad if you just want to change the variable’s name, but for other tasks it’s annoying.

The same holds true for other languages, like the SASS mixin @ and jQuery CSS selectors.

Fortunately, there’s an easy fix. Sublime’s word_separators setting governs which characters are considered to be part of a word and which are “word boundaries.”

The default list of word separators includes many characters that are used as prefixes in different languages:

"word_separators": "./\()"'-:,.;<>~!@#$%^&*|+=[]{}`~?"

You can override this setting in your user preferences, but the best approach is creating language-specific settings files. This enables you to tweak Sublime’s selection for PHP files to include the $ when selecting variables, while in JavaScript files you could tell Sublime to include # to make it easier to work with jQuery selectors.

Here’s a short screencast that shows how to create a language-specific settings file for PHP and override word_separators to allow selection of $ prefixes. The same principle works for other languages as well.

I cover more tweaks like this in the Sublime Text Tips newsletter. Sign up today at:

http://sublimetexttips.com/newsletter

About the author

josh

3comments
Constantin Guay - July 25, 2013

Hi, thanks you for this. Do you know how to kepp the “same-word” auto highlight when selecting vars like this ?

Reply
dasim - July 26, 2013

Good tip, but like Constantin, I wonder why “match_selection” preference doesn’t take these selected variables into account. Definitely missing it.

Reply
ASno - June 11, 2014

Thx ! This works fine in ST3 too 😀

Reply
Click here to add a comment

Leave a comment: