Search code examples
sublimetext3sublime-text-plugin

Sublime Text 3 - Change the background color of the selected line


I use the Material Theme in my Sublime Text installation. In the column where the line number is displayed, I would like to change the background color.

example:

enter image description here

I searched the internet for a way to change this color, but I did not find anything related.

Does anyone know if it is possible to change it and how can I do it?

Thanks.


Solution

  • That color is controlled by the color_scheme that you're using (which is separate from your theme) and in particular the setting for line highlighting. It's possible to change that value, and to do so you thus need to make modifications to the underlying color scheme.

    Open the Sublime console with View > Show Console and enter the following command to determine the style information for the line highlighting:

    view.style_for_scope("line_highlight")
    

    This will give you the style information for the color used as the line highlight, which will also give you the color scheme information you need in the following steps.

    There are two different color scheme formats that Sublime supports, so the output can look one of two ways depending on the scheme that you're using. In the examples below I've reformatted the output that you'll see in the console to make it more readable here; in the console it will appear as one long line and the keys might be in a different order.

    >>> view.style_for_scope("line_highlight")
    {
      'foreground': '#dad6cd', 
      'source_file': 'Packages/User/T8H.tmTheme', 
      'source_column': -1, 'source_line': -1, 
      'bold': False, 'italic': False
    }
    
    >>> view.style_for_scope("line_highlight")
    {
      'foreground': '#dad6cd',
      'source_file': 'T8H.sublime-color-scheme', 
      'source_column': 22, 'source_line': 53, 
      'italic': False, , 'bold': False
    }
    

    The important things to note are the name of the source file, discarding any partial path that you might see there, and the current color.

    Now create a file with the following contents and save it into your User folder (use Preferences > Browse Packages... to find it). The name that you use should be the same filename as above, but with the extension sublime-color-scheme, even if the file above is using tmTheme:

    {
        "globals":
        {
            "line_highlight": "#FF00FF",
        }
    }
    

    Assuming you name the file correctly, as soon as you save the change, you'll see the line highlight color change to magenta. You can then tweak the color as you see fit, using the original color as a guideline if you want it to be darker, lighter, etc.

    See the color scheme documentation for more details on the various ways you can specify the color and how to do this and other more complex customization of your color scheme.