Sublime Forum

How to Highlight Matches with a Background Color in Sublime Text

#1

Hello everyone,

I’m trying to highlight matching words or patterns in Sublime Text by changing the background color instead of the default foreground styling.

I’ve seen ways to underline or change text colors using tmTheme or sublime-color-scheme , but I specifically need a way to apply a background color to matched words .

What I’ve Tried:

  1. Using “regions” via a plugin:

python

CopyEdit

view.add_regions("highlight", [region], "region.greenish", "", sublime.DRAW_NO_OUTLINE)
  • This works but doesn’t allow custom background colors beyond predefined region styles.
  1. Modifying sublime-color-scheme :

json

CopyEdit

{
    "scope": "my_highlight",
    "background": "#FFFF00"
}
  • However, I’m not sure how to apply this dynamically to matches.

My Questions:

  • Is there a way to dynamically apply background highlights to specific words using a plugin?
  • Can I define a custom region style with a specific background color?
  • If possible, how do I integrate this into a Sublime Text package/plugin?

Any guidance or examples would be greatly appreciated!

Thanks in advance!

0 Likes

#2

Yes, using the view.add_regions() API as you mentioned above.

Yes, but only by using a customized color scheme rule such as the one that you outlined above.

You would do what you did in your original example, only instead of using the scope name region.greenish to specify the color, you would use my_highlight instead, because that’s the scope that’s in your color scheme:

To be more specific about integration, if what you’re making is for your own use, then this is all you need.

If you are instead working on a plugin that you want to share with others, then you need to do something about getting your custom scopes into the color scheme of the current user so that your plugin will use them.

  1. The Settings API can be used to determine what the current color_scheme is set to, and can also tell you when someone changes the setting.
  2. Via the value of this setting you can create an appropriate sublime-color-scheme file; do that by creating a folder in Packages named after your folder and generate a file in that is an empty color scheme with the same name as the one the user is currently using, with your custom rules; when the setting changes, generate a new one if you have not already

This glosses over the notion that to work with any color scheme, whatever color choices you make need to make sense for every color scheme everywhere in order to look good. So you either need to be very careful in your choices so that they apply everywhere, let the user customize them via a setting, or etc.

If you just use the region.* regions which pull the closest color from the color scheme the user is using so that it will blend with the color scheme, then you don’t need to do any of this, but the choice of colors is no longer your own (if it matters).

So, there are tradeoffs there.

1 Like

#3

im Highlight Matches by This PlugIn
“HighlightMatches”

0 Likes