Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax specific highlighting with Sublime Text 2

Tags:

sublimetext

I'm wondering if there is a way to have two different tag colours ("colors" for those in the US) for different language tags in the same file.

For example, lets say I have ColdFusion code and HTML code in the same .cfm file. Could I make the ColdFusion tags red and the HTML tags Blue?

For instance, lets call the below file HelloWorld.cfm - could I colour the tags differently?

<cfset myvar = "hello, world" />
<html>
<head>
  <title>This is my title</title>
</head>
<body>

<div><cfoutput>#myvar#</cfoutput></div>

</body>
</html>

Thanks!

like image 585
NotJustClarkKent Avatar asked Feb 18 '12 23:02

NotJustClarkKent


People also ask

Does Sublime Text support syntax highlighting?

Once you select a language, Sublime Text will use the Syntax Highlighting for that language. To enable Syntax Highlighting click on “View” in the top bar, then hover your mouse over “Syntax”, and select your programming language from the list.

Can you highlight in sublime?

🖍 Text Marker (Highlighter)This Sublime Text plugin allows temporarily and persistently marking all occurrences of selected words in a color; multiple marked selections can be added simultaneously, each marking the selections with different colors.

How do I change the syntax color in Sublime Text?

The colors of the syntax highlighting depend on Sublime Text's color theme. You can change the theme by going to Preferences > Color Scheme.

How do I change the default syntax in Sublime Text?

In the current version of Sublime Text 2 (Build: 2139), you can set the syntax for all files of a certain file extension using an option in the menu bar. Open a file with the extension you want to set a default for and navigate through the following menus: View -> Syntax -> Open all with current extension as...


2 Answers

Yes, as long as the tags can be identified as having different scopes by your installed language definitions, you can edit your colour scheme to target those scopes with specific colours and other styles.

In your packages folder, language scopes are defined in the .tmLanguage files for your installed languages, while styles are defined in the .tmTheme files in the "color scheme - default" folder.

If you position your cursor inside a tag, and press shift+ctrl+alt+p (shift-cmd-p in OSX I think) the status bar will display the current scope. You can also copy this to the clipboard via the console with this command:

sublime.set_clipboard(view.syntax_name(view.sel()[0].b))

You can use this information to create your styles, a bit like css selectors, but with XML. For example I use this Coldfusion package and I have the scope selectors shown below in my custom .tmTheme file to distinguish cf tags from HTML tags.

<dict>
    <key>name</key>
    <string>Tag name</string>
    <key>scope</key>
    <string>entity.name.tag</string>
    <key>settings</key>
    <dict>
        <key>background</key>
        <string>#D8D0B6</string>
        <key>fontStyle</key>
        <string>bold</string>
        <key>foreground</key>
        <string>#647A4F</string>
    </dict>
</dict>
<dict>
    <key>name</key>
    <string>CF tag name</string>
    <key>scope</key>
    <string>entity.name.tag.conditional.cfml, entity.name.tag.declaration.cfml, entity.name.tag.other, entity.name.tag.cf, entity.name.tag.inline.other.cfml</string>
    <key>settings</key>
    <dict>
        <key>background</key>
        <string>#D8D0B6</string>
        <key>fontStyle</key>
        <string>bold</string>
        <key>foreground</key>
        <string>#990033</string>
    </dict>
</dict>

More info on scope selectors.

like image 117
Jeremy Halliwell Avatar answered Oct 11 '22 16:10

Jeremy Halliwell


I've updated the ColdFusion.tmLanguage so that you'll only need to target entity.name.tag.cf to color all cf tags. You can also target specific tags for example entity.name.tag.cf.script or entity.name.tag.cf.query for cfscript and cfquery respectively. HTH

like image 20
atomi Avatar answered Oct 11 '22 16:10

atomi