search and highlight 2 or more words with different colors



  • it would be very useful to search and highlight 2 or more words with different colors … especially useful in log analysis



  • @Alberto-Bongiorni

    Maybe you wanna look into User Defined Language (UDL) setup for your log-analysis needs, as it can “color” such things very well. If you choose to look into it and need help, feel free to post back here.



  • @Alan-Kilborn
    Thanks Alan,
    but I would like to have a dynamic search tool such as a non-language specific find.
    With your solution I should create a specific language for each type of analysis.
    If instead there was a dialog that allows you to specify 3-5 terms and highlight each with a color, that would be great!



  • @Alberto-Bongiorni said in search and highlight 2 or more words with different colors:

    If instead there was a dialog that allows you to specify 3-5 terms and highlight each with a color, that would be great!

    Probably the closest you can get to this is putting your caret on a term and then invoking Search menu > Mark All > and selecting a color. You could assign a keycombo to that so that it is faster than mousing around in the menus (for the coloring part).

    You could “macroize” the whole thing, but that would require having your terms “fixed”, which it doesn’t sound like something you want to have.



  • @Alberto-Bongiorni

    Another thought is that something could be scripted for this if you’re willing to use a scripting plugin, e.g. Pythonscript.
    Let us know what you think about that.



  • @Alan-Kilborn said in search and highlight 2 or more words with different colors:

    Another thought is that something could be scripted for this if you’re willing to use a scripting plugin, e.g. Pythonscript.
    Let us know what you think about that.

    Sounds like a great idea to me. If anyone can realize it I would appreciate it, otherwise where can I learn how to write a python plugin for notepad ++?



  • @Alberto-Bongiorni

    Here’s a little Pythonscript demo of the functionality:

    # -*- coding: utf-8 -*-
    
    from Npp import editor, notepad
    
    for indic in [25, 24, 23, 22, 21]:
        user_input = notepad.prompt('\r\nEnter comma separated terms to show in Style #{}:'.format(25 - indic + 1), '', '')
        if user_input == None: break
        if len(user_input) > 0:
            input_list = user_input.split(',')
            for inp in input_list:
                matches = []
                editor.search(inp, lambda m: matches.append(m.span(0)))
                for m in matches:
                    editor.setIndicatorCurrent(indic)
                    editor.indicatorFillRange(m[0], m[1] - m[0])
    

    Some nice instructions for how to acquire the Pythonscript plugin and set up a script are found HERE.



  • @Alan-Kilborn said in search and highlight 2 or more words with different colors:

    -- coding: utf-8 --

    from Npp import editor, notepad

    for indic in [25, 24, 23, 22, 21]:
    user_input = notepad.prompt(’\r\nEnter comma separated terms to show in Style #{}:’.format(25 - indic + 1), ‘’, ‘’)
    if user_input == None: break
    if len(user_input) > 0:
    input_list = user_input.split(’,’)
    for inp in input_list:
    matches = []
    editor.search(inp, lambda m: matches.append(m.span(0)))
    for m in matches:
    editor.setIndicatorCurrent(indic)
    editor.indicatorFillRange(m[0], m[1] - m[0])

    thanks, it’s almost perfect, can I change the background color depending on the position of the search term? For example 1st blue term, 2nd yellow term etc … ?



  • Hello, @alberto-bongiorni, @alan-kilborn and All,

    Thanks again, Alan for this new script ! After some testing ,I preferred to use the regex mode. So I simply changed the line :

                editor.search(inp, lambda m: matches.append(m.span(0)))
    

    by the line

                editor.research(inp, lambda m: matches.append(m.span(0)))
    

    This modification provides two advantages :

    • Firstly, we can simulate the Match whole word only and Match case options of the Find/Replace/Find in Files/Mark dialogs. For instance :

      • Program will mark this string, with this exact case ( Default case )

      • (?i)Program will mark this string, wahtever its case

      • \bProgram\b will mark this word, with this exact case

      • (?i)\bProgram\b will mark this word, whatever its case

    • Secondly, it allows us to mark areas of differents lengths ! Test, against the well-known license.txt file, the two regexes below :

      • b.*q

      • (?s)b.*q

    Nice, as this kind of marking is impossible with the Search > Mark All >.... native N++ options ;-))


    One more tip :

    If you use a same expression for two or more styles, you can mark these expressions with the blend of all the styles involved ! For instance, the picture, below, shows the default style colors and the blend of two styles :

    f56816eb-3c69-489b-8fef-012f57c9c4b1-image.png

    Best Regards,

    guy038

    P.S. :

    In the picture below, I added the combinations with 3 styles !

    d6001d87-6c32-46ac-8d39-970afc0b3c34-image.png



  • @Alberto-Bongiorni said in search and highlight 2 or more words with different colors:

    can I change the background color depending on the position of the search term? For example 1st blue term, 2nd yellow term

    Not sure what you mean by that, in context of how the script works.
    Can you re-ask that question with a concrete example?



  • @Alberto-Bongiorni said in search and highlight 2 or more words with different colors:

    it would be very useful to search and highlight 2 or more words with different colors … especially useful in log analysis

    In what way does this improve over right click -> Style token?



  • @gstavi said in search and highlight 2 or more words with different colors:

    In what way does this improve over right click -> Style token?

    It really doesn’t. :-)
    Except that you don’t have to find an occurrence (by hand or search) of your “token” first, in order to rclick it.
    The script is really just a demo that someone could build something related upon.
    I can’t see someone typing all those “tokens” very often…

    It really makes way more sense to use a UDL for such a purpose (OP’s original “log analysis”). And that was my first suggestion to OP.

    But everyone has their own workflow.
    And their own likes, dislikes, and "I want"s.


Log in to reply