Community
    • Login

    How to set a different background color for the text before a certain line?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    4 Posts 2 Posters 66 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • SuperCPUS
      SuperCPU
      last edited by

      Hello everyone,
      Sorry if this was asked before, I posted the question on GitHub first, and I was redirected here - https://github.com/notepad-plus-plus/notepad-plus-plus/issues/17099
      I am editing a text file where the most important part starts after a line containing only the word STARTCODE
      Can Notepad++ change the background color for the text before that? For example, to make it light gray, so I know those lines are not important. Or is it possible to do it with a plugin?

      PeterJonesP 1 Reply Last reply Reply Quote 0
      • PeterJonesP
        PeterJones @SuperCPU
        last edited by

        @SuperCPU ,

        The Notepad++ UDL system isn’t quite up to the task by itself, because there’s no way to say “highlight from beginning of line to here if this matches”.

        But there are two ways I can think of, using plugins.

        First, I know the AnalysePlugin has the ability to apply multiple regular expressions to the same file; I don’t use it, but there might be a way to have it automatically apply colors based on the regular expression (but I’m not 100% sure, so you might need to experiment) – but a regex like ^.*STARTCODE should mark from the beginning of the line until the STARTCODE

        Second (and this one I’m more confident of): you could use the UDL feature of Notepad++ in combination with the EnhanceAnyLexer plugin. That plugin defines its rules based on the lexer language (whether it’s a builtin language like Python or a UDL for your custom needs), and doesn’t actually care whether the UDL has any keywords (though you might want to define STARTCODE as a keyword). But with that plugin, you can define a foreground color

        [normal text]
        0x777777 = ^.*STARTCODE
        
        [exact name of UDL]
        0x777777 = ^.*STARTCODE
        

        baef46b0-0347-4bde-b633-8f0e426bfeb0-image.png

        (My screenshot shows it in the [normal text] section, since I didn’t want to create a new UDL for this example, but if you’ve got a UDL called exact name of UDL, then the second section in my post would do it.)

        I had seen your Issue earlier, so already had a mental plan of what I’d say if you ever posted here. But while I was typing up this reply, I realized that maybe my first interpretation was wrong. If you had the text,

        before
        before
        xyz STARTCODE
        blah
        blah
        

        were you intending the gray text to just be the xyz STARTCODE, or did you want to also gray out the before lines?

        If the latter, then your EnhanceAnyLexer line would be more like,

        0xcccccc = (?s)\A.*\bSTARTCODE\b
        

        207ac063-abe9-4a95-8101-eb72f9779811-image.png

        (Either way, make sure to not set the excluded_styles = #, #, .... unless you want to exclude your regex from working when inside one of the other UDL styles. It’s more powerful to be able to restrict it, but for new users, it usually makes more sense to just leave that option out.)

        I hope one of these is what you want.

        SuperCPUS 1 Reply Last reply Reply Quote 0
        • SuperCPUS
          SuperCPU @PeterJones
          last edited by

          @PeterJones
          Thank you for your reply and for all the effort to answer my question!

          Sorry I wasn’t clear enough: I wanted to say that I want to change the background color for all the lines before that line containing the word STARTCODE, so your second answer is what I need.
          However, it works but once I close the EnhanceAnyLexerConfig.ini or I simply start editing the “startcode.txt” file, the graying is gone for those lines before STARTCODE

          And another question is: how can I change the background color instead of the ink color?

          PeterJonesP 1 Reply Last reply Reply Quote 0
          • PeterJonesP
            PeterJones @SuperCPU
            last edited by

            However, it works but once I close the EnhanceAnyLexerConfig.ini or I simply start editing the “startcode.txt” file, the graying is gone for those lines before STARTCODE

            Once you save the config, assuming you used [normal text], then the next time you open a text file, it will use that rule. So if you close startcode.txt and reopen it, it should work right.

            And another question is: how can I change the background color instead of the ink color?

            EnhanceAnyLexer only affects foreground colors, not background colors. Sorry.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors