Community
    • Login

    How do you color certain text after a keyword in notepad++?

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    udl
    31 Posts 5 Posters 3.7k 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.
    • PeterJonesP
      PeterJones @Leahnn Rey
      last edited by PeterJones

      @Leahnn-Rey ,

      I have absolutely no idea what an .ini file is.

      The .ini file is the configuration file that you opened when you clicked Plugins > Enhance Any Lexer > Enhance current language – ie, the file that has the color codes in it.

      i want my text to be colored blue after the keyword ‘function’.

      Then you need to come up with a regular expression (regex) that matches the word after the keyword function. In the .ini file, the color goes on the left of the equal sign, and the regex goes on the right. So in the original example that Enhance current language created for you, 0x66ad1 = \w+:

      • the color is 0x66ad1, which means 6 units of blue, 6a=106 units of green, and d1=209 units of red;
      • the regex is \w+ which means “one or more word characters” (where a word character is defined as letters, numbers, and underscore).

      So you would need to create a regex that does what you want. Those regex follow the same rules for Notepad++'s regular-expression search, as defined here in the user manual.

      To give you a freebie, you want it to require the prefix function , then match one or more word characters: that would look like function \w+ . But that changes the color of the function keyword as well as the function’s name, which is probably not what you want.

      Instead, you will want to either “reset” the regex between with a \K (everything before the \K is “thrown out” after it matches, so using function \K\w+ will match function-space, throw it out, and then match-and-color one or more word charaters) or use a positive lookbehind with (?<=...) (so in your case, lookbehind for function-space, then normal match on one or more words would be (?<=function )\w+ ).

      Hope this helps.

      Leahnn ReyL 2 Replies Last reply Reply Quote 4
      • Leahnn ReyL
        Leahnn Rey @PeterJones
        last edited by

        @PeterJones Thank you so much Peter. You have no idea how much this is gonna help me.

        1 Reply Last reply Reply Quote 1
        • Leahnn ReyL
          Leahnn Rey @PeterJones
          last edited by

          @PeterJones If i wanted to, can i add multiple ones? such as void or function?

          PeterJonesP 1 Reply Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn
            last edited by Alan Kilborn

            You have no idea how much this is gonna help me.

            I think the discussion in this thread has also helped others understand the EnhanceAnyLexer plugin a little better, too. I know it has helped me.

            EkopalypseE 1 Reply Last reply Reply Quote 1
            • PeterJonesP
              PeterJones @Leahnn Rey
              last edited by

              @Leahnn-Rey said in How do you color certain text after a keyword in notepad++?:

              If i wanted to, can i add multiple ones? such as void or function?

              Yes.

              There are two ways: you could either do a separate color = regex pair, if you want them each different colors (or even the same color but simple maintenance), or you could build a more complicated regex

              0x66ad1 = function \K\w+
              0x22CD7 = void \K\w+
              

              vs

              0x66ad1 = (?<=function |void )\w+
              
              Leahnn ReyL 1 Reply Last reply Reply Quote 0
              • Leahnn ReyL
                Leahnn Rey @PeterJones
                last edited by

                @PeterJones If i do that, it just says “Invalid lookbehind assertion encountered in the regular expression.”

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

                  @Leahnn-Rey ,

                  0x66ad1 = (function|void) \K\w+
                  

                  Sorry, I had forgotten that alternation is not a “fixed length lookbehind”: I remembered you couldn’t have + or * or ? modifiers in a lookbehind, but you also cannot have the alternation if the lengths of the alternates are different. Switch to alternation with the \K, as I show in this reply, gets rid of that problem (because \K doesn’t have the fixed-width requirement)

                  Leahnn ReyL Neil SchipperN 2 Replies Last reply Reply Quote 0
                  • Leahnn ReyL
                    Leahnn Rey @PeterJones
                    last edited by

                    @PeterJones Didn’t work. It works when i do:

                    0xb0c94e = void \K\w+
                    0xb0c94e = function \K\w+
                    

                    So i will keep it that way. I’m having an issue with the positive lookahead. Since i like monokai (Which is the theme i’m using) i want my text to be colore green when there is a () at the end. But, when i do:

                    0x600e6a3 = (?=\(\))\w+
                    

                    It doesn’t color the text before the parenthesis. Am i doing something wrong here?
                    NOTE: i read the documentation

                    Neil SchipperN 1 Reply Last reply Reply Quote 0
                    • Neil SchipperN
                      Neil Schipper @Leahnn Rey
                      last edited by

                      @Leahnn-Rey

                      This should work: \w+(?=\(\))

                      Think of the look-ahead as a kind of modifier of the active (ie, already specified, and so, to the left of the look-ahead) match specification.

                      Leahnn ReyL 2 Replies Last reply Reply Quote 2
                      • EkopalypseE
                        Ekopalypse @Alan Kilborn
                        last edited by

                        @Alan-Kilborn said in How do you color certain text after a keyword in notepad++?:

                        You have no idea how much this is gonna help me.

                        I think the discussion in this thread has also helped others understand the EnhanceAnyLexer plugin a little better, too. I know it has helped me.

                        Then I thoroughly missed my target with the in-place documentation within the confuiguration file, because I had hoped that it would be as easy to understand as it could be.
                        :-(

                        Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
                        • Alan KilbornA
                          Alan Kilborn @Ekopalypse
                          last edited by

                          @Ekopalypse said in How do you color certain text after a keyword in notepad++?:

                          Then I thoroughly missed my target with the in-place documentation within the confuiguration file, because I had hoped that it would be as easy to understand as it could be.

                          Well, I don’t know about that.
                          Maybe I meant it more like: “Some real world examples (e.g. from this thread) have encouraged me to get more into the EnhanceAnyLexer plugin”.

                          1 Reply Last reply Reply Quote 2
                          • Leahnn ReyL
                            Leahnn Rey @Neil Schipper
                            last edited by PeterJones

                            @Neil-Schipper said in How do you color certain text after a keyword in notepad++?:

                            \w+(?=\(\))

                            Works like a charm. Thanks!

                            1 Reply Last reply Reply Quote 0
                            • Leahnn ReyL
                              Leahnn Rey @Neil Schipper
                              last edited by

                              @Neil-Schipper The thing is, when i enter in function parameters, the coloring disappears.

                              Alan KilbornA 1 Reply Last reply Reply Quote 0
                              • PeterJonesP
                                PeterJones @Ekopalypse
                                last edited by

                                @Ekopalypse said in How do you color certain text after a keyword in notepad++?:

                                I had hoped that it would be as easy to understand as it could be.

                                Trust me: getting documentation so that everyone can understand is hard. ;-)

                                And, as Alan said, sometimes it just takes seeing a few examples of people doing interesting-to-me things with a specific plugin before I see the usefulness of a given plugin.

                                1 Reply Last reply Reply Quote 2
                                • Alan KilbornA
                                  Alan Kilborn @Leahnn Rey
                                  last edited by

                                  @Leahnn-Rey said in How do you color certain text after a keyword in notepad++?:

                                  The thing is, when i enter in function parameters, the coloring disappears.

                                  That’s because with the expression you’ve used you’ve told it to color only things like: func1() or my_func(), not func2(3,5) or func3("test").

                                  Time to learn about regular expressions!

                                  Leahnn ReyL 1 Reply Last reply Reply Quote 0
                                  • Leahnn ReyL
                                    Leahnn Rey @Alan Kilborn
                                    last edited by

                                    @Alan-Kilborn I do know about that. What i’m asking here is how i can color it even if it has parameters. I do know regex, but i’m just a beginner yet. So i don’t know advanced stuff.

                                    Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
                                    • Alan KilbornA
                                      Alan Kilborn @Leahnn Rey
                                      last edited by

                                      @Leahnn-Rey

                                      It may take some tweaking to get it exactly right in all cases, but maybe this regular expression moves you closer?:

                                      \w+(?=\(.*?\))

                                      I’m not sure where the limit of us writing regular expressions for you stops and you roll up your sleeves and craft your own is…

                                      I think the original question of “How do you color certain text after a keyword in notepad++?” has been answered, as far as the tools having been provided.

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

                                        @Leahnn-Rey said in How do you color certain text after a keyword in notepad++?:

                                        I do know regex, but i’m just a beginner yet.

                                        Which is why we’ve given you a few examples, and linked you to the regex documentation. I know you’ve said you’ve read it… but reading the documentation for regex once is never enough to understand the practicalities of it; I’ve been using regex for years (decades?) and still have to look up things on that page all the time.

                                        What you really need to do is start with easy expressions, give yourself a goal, and try to find the answer yourself to that goal in the documentation:

                                        For example, the “easy” part of your original goal was “how do I match a word”; you would have then dug into the documents, and found that \w is a “word character” and + means “make the quantity of the previous thing ‘one or more’”, and put those two together to find that “find one or more word characters in a row” is written as \w+ . Or, since we gave you that one as part of the freebie, you could have just looked up the meanings, and found other things nearby, like the concept of * meaning “0 or more of the previous thing” compared to + as 1-or-more, or that . means “any character” similar to \w meaning “only word characters”. (And yes, .* to match “0 or more of anything” is a pretty common idiom that you need to learn right away, if you’re going to call yourself even a beginner with regular expressions.)

                                        Your next goal might have been “after a prefix like function or void” … but that was a complicated one, and one I wouldn’t expect a newbie to get on their own right away. Which is why I just gave it to you as part of the freebie. So this goal was bypassed for now (though at some point, you’ll want to understand the \K and lookaheads and lookbehinds more thoroughly).

                                        You got the literal parentheses yourself, which is great! And Neil helped you put the lookahead in the right place (once again, getting lookaheads right is not an easy task, and one we’re more willing to do freebies on).

                                        Moving on to your added requirement of “optional parameters between the parentheses”, your first thought for doing yourself could have been “how do I translate that into computerese? hmm, maybe ‘optional means “zero or more”; so zero or more of any character between literal parentheses’ …” With that mental translation, it would have rung some bells: you already knew the parentheses; “any character” was the . that you should have found earlier, and “zero or more” was the * that was a line away from the +'s documentation, which you had already learned. Putting that together, you get "parameters between the parentheses as \(.*\) . You might have seen that it matched too far, and if you didn’t find the ? on your own (right next to + and *), you could have asked a directed question “I got \(.*\) to match the parentheses with optional parameters, but how do I make it stop at the first close-parenthesis?”, which is more likely to get answered.

                                        Please note: I went into this detailed explanation of the thought processes involved, not because I am mad or think you are foolish or any such nonsense, but because I want to help you learn… these are thought processes you need to learn, if you ever want to get good with even simple regex. Since you hadn’t learned those processes yet, I thought I would share them.

                                        But also understand that this forum is not a write-my-regex-for-me free-for-all, and regular contributors here quickly tire of answering a long series of “my requirements have changed, please add this feature to the regex you already gave me” from a user. The more effort you show, the more and better replies you will receive.

                                        Good luck.

                                        1 Reply Last reply Reply Quote 0
                                        • Neil SchipperN
                                          Neil Schipper @PeterJones
                                          last edited by

                                          @PeterJones said in How do you color certain text after a keyword in notepad++?:

                                          @Leahnn-Rey ,
                                          0x66ad1 = (function|void) \K\w+

                                          Sorry, I had forgotten that alternation is not a “fixed length lookbehind”: I remembered you couldn’t have + or * or ? modifiers in a lookbehind, but you also cannot have the alternation if the lengths of the alternates are different. Switch to alternation with the \K, as I show in this reply, gets rid of that problem (because \K doesn’t have the fixed-width requirement)

                                          This is a good context to deploy the poor man’s variable length look-behind:

                                          ((?<=function )|(?<=void ))\w+

                                          which you described here. Since it doesn’t rely on \K, it permits the Find Next function, which is helpful to regex learners and hackers.

                                          PeterJonesP 1 Reply Last reply Reply Quote 3
                                          • PeterJonesP
                                            PeterJones @Neil Schipper
                                            last edited by

                                            @Neil-Schipper said in How do you color certain text after a keyword in notepad++?:

                                            you described here

                                            Oh, come on, that was more than a day ago. I can’t seriously be expected to remember something from that far back! ;-)

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