Automatic highlighting of lines containing a specific string?



  • I use notepad++ to view PHP error log files. We have a lot of logging in place for analytics and such, and it tends to obscure important stuff.

    An average log entry looks like this - prepended with a time and location stamp:

    [25-Sep-2018 18:22:55 US/Central] error information goes here

    When I add logging specifically for debugging purposes, I prepend my log string with +++, so it would look like this:

    [25-Sep-2018 18:22:55 US/Central]+++ test

    Is there any way to automatically change the formatting (color, specifically) of any line that contains the +++ string? Like maybe set the foreground color to white and the background to red.

    I was fiddling a bit with the user-defined language settings, but can’t figure out how to manage this specific setup, since it’s not a line opener or delimiter.



  • Maybe the marking feature of the search function could be helpful for you. It marks only the search strings found but it is also able to set bookmarks at these lines.

    1. Open the Search dialog and switch to the Mark tab.
    2. Make sure that the following checkboxes have the specified state:
      a) Bookmark line -> checked
      b) Purge for each search -> checked
      c) Match whole word only -> unchecked
    3. Set Search mode to Normal

    After hitting the “Mark all” button you will be able to navigate with F2/Shift+F2 between the lines containing your search string.



  • @Esther-Strom

    UDL might be one approach, but when I have a quick need for something like this, I record a Mark action to a macro and then tie that macro to a keycombination. Then when I need the highlighting, or to refresh it, I just hit the keycombo and the highlighting pops in.

    Here’s how I might do yours:

    Invoke Mark dialog (Search (menu) -> Mark…)
    Find what zone: ^[\d{2}-\w{3}-\d{4}\x20\d{2}:\d{2}:\d{2}\x20US/Central]\+{3}\x20(?-s).+
    Wrap around checkbox: ticked or unticked, as you like
    Search mode selection: Regular expression
    Action: Press Find All button

    Of course, having macro recording running when the button is pressed is what makes this truly useful…the assumption here is you know how to record, name and save macros…

    When executed, this “marking” will cause a red background to appear for your debug string; example:

    Imgur

    In case you are curious or want to modify it, here’s how the Find what expression works:

    • [Assert position at the beginning of a line (at beginning of the string or after a line break character) (carriage return and line feed, form feed)][1 ] ^
    • [Match the character “[” literally][2 ] [
    • [Match a single character that is a “digit” (any symbol with a decimal value in the active code page)][3 ] \d{2}
      • [Exactly 2 times][4 ] {2}
    • [Match the character “-” literally][5 ] -
    • [Match a single character that is a “word character” (letter, digit, or underscore in the active code page)][3 ] \w{3}
      • [Exactly 3 times][4 ] {3}
    • [Match the character “-” literally][5 ] -
    • [Match a single character that is a “digit” (any symbol with a decimal value in the active code page)][3 ] \d{4}
      • [Exactly 4 times][4 ] {4}
    • [Match the character “ ” which occupies position 0x20 (32 decimal) in the character set][6 ] \x20
    • [Match a single character that is a “digit” (any symbol with a decimal value in the active code page)][3 ] \d{2}
      • [Exactly 2 times][4 ] {2}
    • [Match the colon character][5 ] :
    • [Match a single character that is a “digit” (any symbol with a decimal value in the active code page)][3 ] \d{2}
      • [Exactly 2 times][4 ] {2}
    • [Match the colon character][5 ] :
    • [Match a single character that is a “digit” (any symbol with a decimal value in the active code page)][3 ] \d{2}
      • [Exactly 2 times][4 ] {2}
    • [Match the character “ ” which occupies position 0x20 (32 decimal) in the character set][6 ] \x20
    • [Match the character string “US/Central” literally (case sensitive)][5 ] US/Central
    • [Match the character “]” literally][2 ] ]
    • [Match the character “+” literally][2 ] \+{3}
      • [Exactly 3 times][4 ] {3}
    • [Match the character “ ” which occupies position 0x20 (32 decimal) in the character set][6 ] \x20
    • [Use these options for the remainder of the regular expression][7 ] (?-s)
      • [(hyphen inverts the meaning of the letters that follow)][7 ] -
      • [Dot doesn’t match line breaks][7 ] s
    • [Match any single character that is NOT a line break character (line feed, carriage return, form feed)][8 ] .+
      • [Between one and unlimited times, as many times as possible, giving back as needed (greedy)][9 ] +

    Created with RegexBuddy

    [1 ]: http://www.regular-expressions.info/anchors.html
    [2 ]: http://www.regular-expressions.info/characters.html#special
    [3 ]: http://www.regular-expressions.info/shorthand.html
    [4 ]: http://www.regular-expressions.info/repeat.html#limit
    [5 ]: http://www.regular-expressions.info/characters.html
    [6 ]: http://www.regular-expressions.info/nonprint.html
    [7 ]: http://www.regular-expressions.info/modifiers.html
    [8 ]: http://www.regular-expressions.info/dot.html
    [9 ]: http://www.regular-expressions.info/repeat.html

    RegexBuddy settings to emulate N++ regex engine: Application=boost::regex 1.54-1.57 / flavor=Default flavor / replacement flavor=All flavor / ^$ match at line breaks / Numbered capture / Allow zero-length matches



  • @Esther-Strom

    @dinkumoil makes a good point about “bookmarks”, which will give you additional functionality.

    If you add Mark line (tick the checkbox) into the Mark search you can quickly jump between your debug messages which would be very handy when your file is large. See Search (menu) -> Bookmark -> Next Bookmark (and of course also Previous Bookmark). [Technically you don’t need bookmarks to do this, as you can quickly move to redmarked text using Search (menu) -> Jump Down (or Up) -> Find Style.]

    If you want to copy all of the lines touched by the redmarking, bookmarks help here: See Search (menu) -> Bookmark -> Copy Bookmarked Lines



  • @Scott-Sumner said:

    Wrap around checkbox: ticked or unticked, as you like

    Slight revision to the above: I would definitely tick the Wrap around checkbox for this!



  • So my thinking was the above (red)marking action might be recorded into a macro and tied to a keystroke, maybe ctrl+shift+e (whatever). Then when you want the highlighting, you press the keycombo. Then it occurred to me that it might also be desirable to remove the (red)marking with another keystroke, maybe ctrl+shift+alt+e (again, whatever).

    However, when I did some experimenting, I found out that a press of the Clear button on the Mark tab of the Find window doesn’t get recorded into a macro.

    Side note: This has been reported long ago as a Notepad++ issue, see “Case I”. Maybe it has also been discussed on the Community before as well…memory fails…

    Worse yet than not being able to record it, you can’t hand-edit the file in which the macros are stored in order to make it work (this technique works in some other instances).

    In short, you can’t clear (red)marking in any other way than bringing up the Mark window and pressing the Clear button. Or can you??

    Well, if you’ll notice, the Mark window has an option for Purge for each search. Activating (ticking) this option means that when you run a new (red)marking operation, before Notepad++ runs it, it will erase all of the existing (red)marking. So if we just can find a Find what value which will never match anything, we should have a means to clear all existing (red)marking. I settled upon the following, wrapped into a complete solution:

    Find what zone: \A\z
    Purge for each search checkbox: ticked
    Wrap around checkbox: ticked
    In selection checkbox: unticked
    Search mode setting: Regular expression
    Action: Press the Find All button

    This action is recordable into a macro, thus it IS possible to assign a “clear (red)marking” command to a keycombo.



  • Thanks to everyone for the answers. What I’m getting is that there are some hacks to sort of get close to what I want to do, but nothing that will get me automatic highlighting without running a macro every time I open the file, or the open file changes.

    Back to BareTail, I guess.



  • @Esther-Strom said:

    there are some hacks to sort of get close to what I want

    Well, if you’re going to be super-fussy about it… :-) …I’d probably say we aren’t out of good options with Notepad++ yet…maybe stay tuned…but I might say perhaps you aren’t using the right tool for the right job.

    Everybody wants Notepad++ to be great at everything, but it’s really just great at its core duty: text editing. It isn’t great at searching…it isn’t great at log-file monitoring…it isn’t great at hex-editing…the list goes on and on.

    For this specific example, probably something like glogg meets your need better than Notepad++…


Log in to reply