Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Search result syntax highlighting for Sublime Text

#ST2/3 Search result syntax highlighting

##Introduction and disclaimer

This is gist contains an edited Find Results.hidden-tmLanguage which adds syntax highlighting to your ST search results. As far as I know, there is no plugin that does this and for that reason it needs to be done via config.

DISCLAIMER: Use this HACK at your risk and backup the original files you are about to mess with.

##Usage Guide for Sublime Text 2

  1. Open the Find Results.hidden-tmLanguage file, located in your Packages directory. You can reach that view the Preferences > Browse Packages... menu. Default locations are:
  • OSX: ~/Library/Application Support/Sublime Text 2/Packages/Default/
  • WIN: ~\AppData\Roaming\Sublime Text 2\Packages
  1. Replace with the file contents with the contents of the gist and make any changes you wish

  2. Save changes and restart Sublime.

  3. ??

  4. Profit

##Usage Guide for Sublime Text 3

  1. Install the PackageResourceViewer extension either via PackageControl or manually from github

  2. Open the Command Pallete and run PackageResourceViewer: Extract Package. Choose Default from the list populated below

  3. The package is now extracted in your packages directory. You can reach that view the Preferences > Browse Packages... menu. Default locations are:

  • OSX: ~/Library/Application Support/Sublime Text 3/Packages/Default/
  • WIN: ~\AppData\Roaming\Sublime Text 3\Packages
  1. Replace with the file contents with the contents of the gist and make any changes you wish

  2. Save changes and restart Sublime.

##Adding more languages

For example, lets say you want to add SASS to the search results highlighting.

Copy one of the existing definitions, e.g LESS or CSS

<dict>
    <key>begin</key>
    <string>^([^ ].*\.css:)$</string>
    <key>beginCaptures</key>
    <dict>
        <key>1</key>
        <dict>
            <key>name</key>
            <string>entity.name.filename.find-in-files</string>
        </dict>
    </dict>
    <key>end</key>
    <string>^[^ ]</string>
    <key>patterns</key>
    <array>
        <dict>
            <key>include</key>
            <string>#line-numbers</string>
        </dict>
        <dict>
            <key>include</key>
            <string>source.css</string>
        </dict>
    </array>
</dict>

And make the following changes

  1. Change <string>^([^ ].*\.css:)$</string> to <string>^([^ ].*\.less:)$</string>
  2. Change <string>source.css</string> to <string>source.less</string>
  3. Save changes and restart

IMPORTANT: To find out the correct key (scope name) to use for your language, you need to do a bit more digging.

  1. You need need to find the corresponding .tmLanguage file for the language you want, by going through your Packages folder

    • ST3 users can use PackageResourceViewer: Extract Package command to browser through packages and open the required .tmLanguage file
  2. Search for scopeName in the file and copy the value of the xml node

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>Find Results</string>
<key>patterns</key>
<array>
<dict>
<key>begin</key>
<!-- add the filetype extensions, here -->
<!-- these are XML formatted files: -->
<string>^([^ ].*\.(?:xml|tmLanguage|hidden-tmLanguage|tmTheme):)$</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>entity.name.filename.find-in-files</string>
</dict>
</dict>
<key>end</key>
<string>^[^ ]</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<!-- reference to something in the "repository" -->
<string>#line-numbers</string>
</dict>
<dict>
<!-- which syntax should match up to the filetype extensions listed above: -->
<!-- to find out what the "scopeName" is, refer to the corresponding *.tmLanguage file -->
<!-- for XML, this is ~/Library/Application Support/Sublime Text 2/Packages/XML/XSL.tmLanguage -->
<key>include</key>
<string>text.xml</string>
</dict>
</array>
</dict>
<!-- HTML -->
<dict>
<key>begin</key>
<string>^([^ ].*\.html:)$</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>entity.name.filename.find-in-files</string>
</dict>
</dict>
<key>end</key>
<string>^[^ ]</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#line-numbers</string>
</dict>
<dict>
<key>include</key>
<string>text.html.basic</string>
</dict>
</array>
</dict>
<!-- CSS -->
<dict>
<key>begin</key>
<string>^([^ ].*\.css:)$</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>entity.name.filename.find-in-files</string>
</dict>
</dict>
<key>end</key>
<string>^[^ ]</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#line-numbers</string>
</dict>
<dict>
<key>include</key>
<string>source.css</string>
</dict>
</array>
</dict>
<!-- LESS -->
<dict>
<key>begin</key>
<string>^([^ ].*\.less:)$</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>entity.name.filename.find-in-files</string>
</dict>
</dict>
<key>end</key>
<string>^[^ ]</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#line-numbers</string>
</dict>
<dict>
<key>include</key>
<string>source.less</string>
</dict>
</array>
</dict>
<!-- JS -->
<dict>
<key>begin</key>
<string>^([^ ].*\.js:)$</string>
<key>beginCaptures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>entity.name.filename.find-in-files</string>
</dict>
</dict>
<key>end</key>
<string>^[^ ]</string>
<key>patterns</key>
<array>
<dict>
<key>include</key>
<string>#line-numbers</string>
</dict>
<dict>
<key>include</key>
<string>source.js</string>
</dict>
</array>
</dict>
<!-- default. -->
<dict>
<key>match</key>
<string>^([^ ].*):$</string>
<key>captures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>entity.name.filename.find-in-files</string>
</dict>
</dict>
</dict>
<!-- this is the initial settings for this file -- (I think declaration order defines precedence) -->
<!-- keeping it here for unrecognized / configured filetypes -->
<dict>
<key>match</key>
<string>^ +([0-9]+) </string>
<key>captures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>constant.numeric.line-number.find-in-files</string>
</dict>
</dict>
</dict>
<dict>
<key>match</key>
<string>^ +([0-9]+):</string>
<key>captures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>constant.numeric.line-number.match.find-in-files</string>
</dict>
</dict>
</dict>
</array>
<!-- stuff in the "repository" is just the line number matching that were originally in the file -->
<key>repository</key>
<dict>
<key>line-numbers</key>
<dict>
<key>patterns</key>
<array>
<dict>
<key>match</key>
<string>^ +([0-9]+) </string>
<key>captures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>constant.numeric.line-number.find-in-files</string>
</dict>
</dict>
</dict>
<dict>
<key>match</key>
<string>^ +([0-9]+):</string>
<key>captures</key>
<dict>
<key>1</key>
<dict>
<key>name</key>
<string>constant.numeric.line-number.match.find-in-files</string>
</dict>
</dict>
</dict>
</array>
</dict>
</dict>
<key>scopeName</key>
<string>text.find-in-files</string>
</dict>
</plist>
@swrobel

This comment has been minimized.

Copy link

@swrobel swrobel commented May 22, 2014

You are my hero

@tobiasboyd

This comment has been minimized.

Copy link

@tobiasboyd tobiasboyd commented Jun 13, 2014

Question - to me, it looks like the first value should match the file extension of the language, e.g.
<string>^([^ ].*\.rb:)$</string>
and the second should match the scopeName from the tmLanguage, e.g.
`source.ruby``

But I can't quite tell from docs, since .less is both an extension and a scope... I don't seem to get anything trying it either way, though (ST3 build 3062 on OSX 10.8.5).

@squanto

This comment has been minimized.

Copy link

@squanto squanto commented Dec 23, 2016

🔥

@xixixao

This comment has been minimized.

Copy link

@xixixao xixixao commented Feb 5, 2017

This is amazing! Works great in ST3.

@rafmjr

This comment has been minimized.

Copy link

@rafmjr rafmjr commented Dec 4, 2018

It works like a charm! Thanks 👏 🔥

@darkobodnaruk

This comment has been minimized.

Copy link

@darkobodnaruk darkobodnaruk commented Nov 22, 2019

Amazing 🙌

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