• Login
Community
  • Login

How to mark lines above marked lines

Scheduled Pinned Locked Moved General Discussion
lineregexmark
14 Posts 6 Posters 19.2k 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.
  • G
    guy038
    last edited by guy038 Nov 28, 2018, 11:25 PM Nov 28, 2018, 12:48 PM

    Hello @unai-abrisketa, @eko-palypse and all,

    Luckily, I did understood your question ;-)) Seemingly, you have a file with some lines containing the string ;2. Of course, you easily succeeded to bookmark ( blue circles ) all these lines. But, you don’t know how to bookmark the line above each line containing ;2., don’t you ?

    Here is my method :

    • Open the Find dialog ( Ctrl + F ) and click on the Mark tab OR use the menu option Search > Mark...

    • In the Find what: zone, type in the regex (?-s)^.+\R(?=.*;2)|\G.+ OR the regex (?-s)^.+\R(?=.*;2)|^.*;2.*

    • Tick, both, the options Bookmark line and Purge for each search

    • Tick, preferably, the Wrap around option, too

    • Select the Regular expression search mode ( IMPORTANT )

    • Click ONCE, on the Replace All button

    => In one go :

    • All the lines, containing the ;2 string, are bookmarked

    • All lines, above a line containing ;2, are, also, bookmarked !

    Et voilà :-))

    So, from the initial sample text, below :

        This is
        bla bla
        edit “harry”;1
        set subnet 213.154.12.123 255.255.255.0;2
        bla bla
    	bla blah
    	bla
    	small test
    	set subnet 213.154.12.123 255.255.255.0;2
    	set subnet 213.154.12.123 255.255.255.0;2
        to
    	bla bla
    	blah blah
    	verify
    	set subnet 213.154.12.123 255.255.255.0;2
        if the
    	blah
    	bla bla
        set subnet 213.154.12.123 255.255.255.0;2
        regex works
        set subnet 213.154.12.123 255.255.255.0;2
        correctly
    	bla bla bla ...
    

    We’ll get the 11 bookmarked lines, with the • mark :

        This is
        bla bla
    •   edit “harry”;1
    •   set subnet 213.154.12.123 255.255.255.0;2
        bla bla
        bla blah
        bla
    •   small test
    •   set subnet 213.154.12.123 255.255.255.0;2
    •   set subnet 213.154.12.123 255.255.255.0;2
        to
        bla bla
        blah blah
    •   verify
    •   set subnet 213.154.12.123 255.255.255.0;2
        if the
        blah
    •   bla bla
    •   set subnet 213.154.12.123 255.255.255.0;2
    •   regex works
    •   set subnet 213.154.12.123 255.255.255.0;2
        correctly
        bla bla bla ...
    

    Note that if two consecutive lines contain the ;2 string, the are both bookmarked, as expected


    I’m about to post some other regexes, which can bookmark, several consecutive lines of a file, when one of these lines contains a specific string ;-))

    Best regards,

    guy038

    1 Reply Last reply Reply Quote 2
    • S
      Scott Sumner
      last edited by Nov 30, 2018, 3:18 AM

      @guy038 said:

      I’m about to post some other regexes, which can bookmark, several consecutive lines of a file, when one of these lines contains a specific string

      Did you actually do this? If so, I haven’t been able to find that posting.

      1 Reply Last reply Reply Quote 0
      • G
        guy038
        last edited by Nov 30, 2018, 12:37 PM

        Hello, @Scott-sumner and All,

        When I said “I’m about to…”, I was a bit pretentious ! I haven’t had time to look at, yet ! And this week-end, I’ll be with family, too ! So rather, during next week ;-))

        Have a nice week-end, too !

        Cheers,

        guy038

        1 Reply Last reply Reply Quote 2
        • G
          guy038
          last edited by guy038 Dec 8, 2018, 8:45 PM Dec 8, 2018, 7:56 PM

          Hello, @scottsumner and All,

          As promised, here is, below, a list of regexes in order to mark a line that satisfies a criterion as well as one or two lines, either, before and/or after that line ;-)). By criteria, I mean, either, a char, a string or, even, a regular expression !

          As a convention, in the two tables below, I named this criterion XXX and each line containing this criterion, LINE X

          Remarks :

          • I assume that all lines of text are NON empty lines, as I supposed there’s little interest in bookmarking empty or pseudo/empty lines, anyway !. So, if your list contain blank lines, use, first, the option Edit > Line operations > Remove Empty Lines ( Containing Blank characters )

          • I also assume that two LINE X are separated by some lines. In the Minimum X....X column, of the two tables, I mentioned, for each regex, the minimum number of lines, between two LINE X, which are necessary for correct bookmarking, assuming the hypotheses, in each line

          • The indication > 1 ch., in the last column of the second table, means that any line, located right after a LINE X, must contain, at least, 2 characters for correct bookmarking


          In the first table, below, the different regexes does NOT bookmark any Line X, only the lines around it :

          •=========================================================================•==========•==========•==========•==========•==========•=========•
          |             REGULAR expression, matching ONE or TWO lines,              | 2nd line | 1st line |  Line X  | 1st line | 2nd line | Minimum |
          |              BEFORE and/or AFTER a LINE X, containing XXX               |  BEFORE  |  BEFORE  |          |   AFTER  |   AFTER  |  X...X  |
          •=========================================================================•==========•==========•==========•==========•==========•=========•
          |  (?-s)^.*XXX.*\R\K.+                                                    |     NO   |     NO   |     NO   |    YES   |     NO   |     1   |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•
          |  (?-s)^.*XXX.*\R.+\R\K.+                                                |     NO   |     NO   |     NO   |     NO   |    YES   |     2   |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•
          |  (?-s)^.*XXX.*\R\K.+\R|\G.+                                             |     NO   |     NO   |     NO   |    YES   |    YES   |     2   |
          •=========================================================================•==========•==========•==========•==========•==========•=========•
          |  (?-s)^.+(?=\R.*XXX)                                                    |     NO   |    YES   |     NO   |     NO   |     NO   |     1   |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•
          |  (?-s)^.+(?=\R.*XXX)|^.*XXX.*\R.+\R\K.+                                 |     NO   |    YES   |     NO   |     NO   |    YES   |     2   |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•
          |  (?-s)^.+(?=\R.*XXX)|^.*XXX.*\R\K.+                                     |     NO   |    YES   |     NO   |    YES   |     NO   |     1   |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•
          |  (?-s)^.+(?=\R.*XXX)|^.*XXX.*\R\K.+\R|\G.+                              |     NO   |    YES   |     NO   |    YES   |    YES   |     2   |
          •=========================================================================•==========•==========•==========•==========•==========•=========•
          |  (?-s)^.+(?=\R.+\R.*XXX)                                                |    YES   |     NO   |     NO   |     NO   |     NO   |     0   |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.*XXX.*\R.+\R\K.+                             |    YES   |     NO   |     NO   |     NO   |    YES   |     3   |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.*XXX.*\R\K.+                                 |    YES   |     NO   |     NO   |    YES   |     NO   |     2   |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.*XXX.*\R\K.+\R|\G.+                          |    YES   |     NO   |     NO   |    YES   |    YES   |     2   |
          •=========================================================================•==========•==========•==========•==========•==========•=========•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.+\R(?=.*XXX)                                 |    YES   |    YES   |     NO   |     NO   |     NO   |     0   |
          |  (?-s)^.+\R(?=.+\R.*XXX)|\G.+                                           |          |          |          |          |          |         |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.+\R(?=.*XXX)|^.*XXX.*\R.+\R\K.+              |    YES   |    YES   |     NO   |     NO   |    YES   |     3   |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.+\R(?=.*XXX)|^.*XXX.*\R\K.+                  |    YES   |    YES   |     NO   |    YES   |     NO   |     1   |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.+\R(?=.*XXX)|^.*XXX.*\R\K.+\R|\G.+           |    YES   |    YES   |     NO   |    YES   |    YES   |     2   |
          |  (?-s)^.+\R(?=.+\R.*XXX)|^.*XXX.*\R\K.+\R|\G.+                          |          |          |          |          |          |         |
          •=========================================================================•==========•==========•==========•==========•==========•=========•
          

          • In the second table, below, the different regexes do bookmark the Line X, as well as the lines around :
          •=========================================================================•==========•==========•==========•==========•==========•=========•==========•
          |           REGULAR expression, matching LINE X containing XXX            | 2nd line | 1st line |  Line X  | 1st line | 2nd line | Minimum | 1st line |
          |              and ONE or TWO lines, BEFORE and/or AFTER it               |  BEFORE  |  BEFORE  |          |   AFTER  |   AFTER  |  X...X  |   AFTER  |
          •=========================================================================•==========•==========•==========•==========•==========•=========•==========•
          |  (?-s)^.*XXX.*                    ( Trivial case ! )                    |     NO   |     NO   |    YES   |     NO   |     NO   |     0   |          |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•----------•
          |  (?-s)^.*XXX.*\R|\G.+                                                   |     NO   |     NO   |    YES   |    YES   |     NO   |     0   |          |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•----------•
          |  (?-s)^.*XXX.*\R|\G.+\R\K.+                                             |     NO   |     NO   |    YES   |     NO   |    YES   |     2   |          |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•----------•
          |  (?-s)^.*XXX.*\R|\G.+(?=.)|\G.\R\K.+                                    |     NO   |     NO   |    YES   |    YES   |    YES   |     2   |  > 1 ch. |
          •=========================================================================•==========•==========•==========•==========•==========•=========•==========•
          |  (?-s)^.+(?=\R.*XXX)|^.*XXX.*                                           |     NO   |    YES   |    YES   |     NO   |     NO   |     0   |          |
          |  (?-s)^.+\R(?=.*XXX)|\G.+                                               |          |          |          |          |          |         |          |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•----------•
          |  (?-s)^.+(?=\R.*XXX)|^.*XXX.*\R|\G.+\R\K.+                              |     NO   |    YES   |    YES   |     NO   |    YES   |     1   |          |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•----------•
          |  (?-s)^.+(?=\R.*XXX)|^.*XXX.*\R|\G.+                                    |     NO   |    YES   |    YES   |    YES   |     NO   |     0   |          |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•----------•
          |  (?-s)^.+(?=\R.*XXX)|^.*XXX.*\R|\G.+(?=.)|\G.\R\K.+                     |     NO   |    YES   |    YES   |    YES   |    YES   |     0   |  > 1 ch. |
          •=========================================================================•==========•==========•==========•==========•==========•=========•==========•
          |  (?-s)^.+(?=\R.*\R.*XXX)|^.*XXX.*                                       |    YES   |     NO   |    YES   |     NO   |     NO   |     0   |          |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•----------•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.*XXX.*\R|\G.+\R\K.+                          |    YES   |     NO   |    YES   |     NO   |    YES   |     3   |          |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•----------•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.*XXX.*\R|\G.+                                |    YES   |     NO   |    YES   |    YES   |     NO   |     2   |          |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•----------•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.*XXX.*\R|\G.+(?=.)|\G.\R\K.+                 |    YES   |     NO   |    YES   |    YES   |    YES   |     3   |  > 1 ch. |
          •=========================================================================•==========•==========•==========•==========•==========•=========•==========•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.+(?=\R.*XXX)|^.*XXX.*                        |          |          |          |          |          |     0   |          |
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.+\R(?=.*XXX)|\G.+                            |    YES   |    YES   |    YES   |     NO   |     NO   |         |          |
          |  (?-s)^.+\R(?=.+\R.*XXX)|\G.+|^.*XXX.*                                  |          |          |          |          |          |         |          |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•----------•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.+(?=\R.*XXX)|^.*XXX.*\R|\G.+\R\K.+           |    YES   |    YES   |    YES   |     NO   |    YES   |     1   |          |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•----------•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.+(?=\R.*XXX)|^.*XXX.*\R|\G.+                 |    YES   |    YES   |    YES   |    YES   |     NO   |     0   |          |
          •-------------------------------------------------------------------------•----------•----------•----------•----------•----------•---------•----------•
          |  (?-s)^.+(?=\R.+\R.*XXX)|^.+(?=\R.*XXX)|^.*XXX.*\R|\G.+(?=.)|\G.\R\K.+  |    YES   |    YES   |    YES   |    YES   |    YES   |     0   |  > 1 ch. |
          •=========================================================================•==========•==========•==========•==========•==========•=========•==========•
          

          Just test them ! Note that, regarding bookmarking, some other regexes may work nice, too ;-))

          Best Regards,

          guy038

          1 Reply Last reply Reply Quote 5
          • S
            Scott Sumner
            last edited by Scott Sumner Dec 9, 2018, 12:06 AM Dec 9, 2018, 12:04 AM

            @guy038

            Hmmmm…well, the regex approach has exactly one thing going for it: Everything needed is entirely built into Notepad++. Other than that…well, sigh…I understand regex…but my head hurts after that last posting…

            If one is willing to install the Pythonscript plugin and run a script, one could be prompted through the process…and get a nice result. I’d like to show the prompts and the end result, but Imgur is not cooperating at the moment, so I guess I will just have to describe it:

            • Prompt box #1: Enter search string (regular expression is possible but not the default)
            • Prompt box #2: Enter integer number of lines to bookmark before the line where search-string hits
            • Prompt box #3: Enter integer number of lines to bookmark after the line where search-string hits

            That’s it…no limitations or caveats…with the exception that it could be slow on really large files with a lot of hits. I should also add that multi-line searches are possible in regex mode or normal mode (in normal mode use ctrl+Enter to specify line breaks–Enter alone will close the prompt box).

            So here’s the script; I call it BookmarkHitLineWithLinesBeforeAndAfter.py:

            def BHLWLBAA__main():
            
                NPP_BOOKMARK_MARKER_ID_NUMBER = 24  # from N++ source code: MARK_BOOKMARK = 24;
            
                search_str = notepad.prompt('Enter search string\r\n(begin string with (?x) to make it a regex search)', '', '')
                if search_str == None or len(search_str) == 0: return
            
                choice_dict = {}
                for choice in [ 'BEFORE', 'AFTER' ]:
                    answer = notepad.prompt('# of lines {} hit line to bookmark:'.format(choice), '', '0')
                    if answer == None: answer = '0'
                    try:
                        answer = int(answer)
                    except ValueError: return
                    if answer < 0: answer = 0
                    choice_dict[choice] = answer
            
                bookmark_line_dict = {}
            
                def match_found(m):
                    first_line_of_match = editor.lineFromPosition(m.span(0)[0])
                    last_line_of_match = editor.lineFromPosition(m.span(0)[1])
                    for line in range(first_line_of_match - choice_dict['BEFORE'], last_line_of_match + choice_dict['AFTER'] + 1):
                        if 0 <= line < editor.getLineCount(): bookmark_line_dict[line] = 1
            
                try:
                    editor.research(search_str[4:], match_found) if search_str.startswith('(?x)') else editor.search(search_str, match_found)
                except RuntimeError as r:
                    notepad.messageBox('Error during search; problem with regex maybe?', '')
                    return
            
                for line in bookmark_line_dict: editor.markerAdd(line, NPP_BOOKMARK_MARKER_ID_NUMBER)
            
            BHLWLBAA__main()
            
            1 Reply Last reply Reply Quote 3
            • G
              guy038
              last edited by guy038 Dec 9, 2018, 12:11 PM Dec 9, 2018, 12:11 PM

              Hi @scott-sumner, and All,

              Just tried your script ! It works nice and I agree : no possibility of strong headaches, with that script ;-))

              Your script logic is a bit different from mine. With my list of regexes you may decide, inside the range [line -2, line -1, hit line, line +1 line +2] to bookmark, or not, any of these 5 lines

              Of course, due to regex limitations, a greater range does not seem sensible ! Probably, in a Python script, it would be possible to extend that range to, let’s say, a maximum of 5 lines before and 5 lines after, which could be bookmarked, or not, independently…

              Cheers,

              guy038

              S 1 Reply Last reply Dec 9, 2018, 1:31 PM Reply Quote 2
              • S
                Scott Sumner @guy038
                last edited by Dec 9, 2018, 1:31 PM

                @guy038

                What, no upvote for a script you liked? :-)

                Regarding your comments…yea, it becomes a clunky user interface problem at that point–how to tell the running script exactly what you want in that case. Of course if you have some great ideas about a clean interface to that…

                1 Reply Last reply Reply Quote 0
                • S
                  Scott Sumner
                  last edited by Dec 9, 2018, 6:11 PM

                  So Imgur is being friendly again, so I can now show the steps of running the script…

                  Starting the script executing you are presented with this box, to which I have typed in ValueError as the search string (I am searching the file containing the script’s own source code):

                  Imgur

                  After saying “OK” to the search string box, one is prompted thusly (to which I have answered I want 3 lines before a line with ValueError in it to be bookmarked):

                  Imgur

                  Similarly, after that one is asked about the “after” lines, to which I have answered “2”:

                  Imgur

                  At that point the script does the searching and the bookmarking, and the result obtained is as follows (I have boxed the search string to make it easier to see):

                  Imgur

                  As you can see, the line with the hit is bookmarked, as is the 3 lines immediately before and the 2 lines immediately after.

                  1 Reply Last reply Reply Quote 1
                  • Alan KilbornA
                    Alan Kilborn
                    last edited by Dec 20, 2022, 10:49 PM

                    The marker ID used for bookmarks changed in Notepad++ 8.4.6 (and later). It is now 20, instead of 24. So, all references to 24 in this thread and/or its script(s), should be changed to 20.

                    ? 1 Reply Last reply May 10, 2023, 3:39 PM Reply Quote 3
                    • ?
                      A Former User @Alan Kilborn
                      last edited by May 10, 2023, 3:39 PM

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