Search code examples
sublimetext2sublimetext3sublimetextsublime-text-plugin

Can you highlight a character after a certain length in sublime text?


In VIM it is possible to highlight a character only if the line goes over a certain length, for example:

Highlight column on VIM

Can this be done in Sublime Text 2 or 3? I know there's a setting to display rulers but I find them a bit distracting sometimes.


Solution

  • javiervd

    Can this be done in Sublime Text 2 or 3?


    Demo


     

    Save the following script @:
    /Packages/Highlight Long Lines/highlight_long_lines.py

     

    import sublime, sublime_plugin
    
    class highlight_long_lines( sublime_plugin.EventListener ):
        def on_modified_async( self, view ):
    
    
            #▒▒▒▒▒▒▒▒  Settings  ▒▒▒▒▒▒▒▒#
            maxLength           = 80
            scope               = "Invalid"
            firstCharacter_Only = False
    
    
    
            view.erase_regions( "LongLines" )
    
            indentationSize     = view.settings().get( "tab_size" )
            indentation_IsSpace = view.settings().get( "translate_tabs_to_spaces" )
    
            document    = sublime.Region( 0, view.size() )
            lineRegions = view.lines( document )
    
            invalidRegions = []
    
            for region in lineRegions:
    
                text             = view.substr( region )
                text_WithoutTabs = text.expandtabs( indentationSize )
    
                if text_WithoutTabs.isspace():
                    tabOffset = 0
                else:
                    tabCount      = text.count( "   " )
                    tabDifference = len( text_WithoutTabs ) - len( text )
                    tabOffset     = tabDifference
    
                lineLength = ( region.end() - region.begin() ) - tabOffset
                if lineLength > maxLength:
    
                    highlightStart = region.begin() + ( maxLength - tabOffset )
    
                    if firstCharacter_Only == True:
                        highlightEnd = highlightStart + 1
                    else:
                        highlightEnd = region.end()
    
                    invalidRegion = sublime.Region( highlightStart, highlightEnd )
                    invalidRegions.append( invalidRegion )
    
            if len( invalidRegions ) > 0:
                view.add_regions( "LongLines", invalidRegions, scope )
    

     


     

    Variable Settings:

    maxLength affects the length that lines will highlight after.

    scope affects the color of the highlighted regions.
    You can use any scope from your active .tmTheme file ( color scheme ).

    firstCharacter_Only affects the extent of the highlighted regions.
    If set to True, only the character at the 81st position will be highlighted.
    If set to False, all characters after the 80th position will be highlighted.