How to set a different background color for the text before a certain line?
-
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? -
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
^.*STARTCODEshould mark from the beginning of the line until theSTARTCODESecond (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
(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 calledexact 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 blahwere you intending the gray text to just be the
xyz STARTCODE, or did you want to also gray out thebeforelines?If the latter, then your EnhanceAnyLexer line would be more like,
0xcccccc = (?s)\A.*\bSTARTCODE\b
(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.
-
@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 STARTCODEAnd another question is: how can I change the background color instead of the ink color?
-
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 closestartcode.txtand 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.
-
LuaScript plugin code added to
startup.luacan add an indicator from the start of document up to the start of the line withSTARTCODE.function Event_StartCodeTag() -- Add indicator from position 0 to start position of STARTCODE. if editor.LexerLanguage == 'null' then local indic = 3 if editor:IndicatorEnd(indic, 0) == 0 then local start_pos = editor:findtext('^STARTCODE$', SCFIND_REGEXP | SCFIND_MATCHCASE) if start_pos then editor.IndicatorCurrent = indic editor:IndicatorClearRange(0, start_pos) editor.IndicStyle[indic] = INDIC_ROUNDBOX editor.IndicFore[indic] = 0xC0C0C0 editor.IndicAlpha[indic] = 100 editor:IndicatorFillRange(0, start_pos) end end end end function Event_Ready() -- Called when the editor is ready. Event_StartCodeTag() npp.AddEventHandler('OnSwitchFile', Event_StartCodeTag) end npp.AddEventHandler('OnReady', Event_Ready)This will try to handle any document using the
nulllexer. That is theNone (Normal Text)item in the Language menu.It is set to use indicator
3with aINDIC_ROUNDBOXstyle with a colour of0xC0C0C0which is light grey. The alpha is100and can be set between0and255. TheOnReadyevent is to ensure the indicators are not being applied until Notepad++ is ready.Viewed with the default theme:

Other scripting plugins should be able to apply indicators with similar code.