How to mark lines above marked lines
-
in the same way as you did it the first time I guess.
But I assume that there is a reason why you can’t do it that way,
so maybe you wanna let us know the reason and more
importantly, what exactly you mean by marked line.
Is it a bookmark or is it text mark (using styles)?Eko
-
I guess that you havent understood my question. I want to mark the lines that were immediately above of the ones I marked before. For example If i have the next two lines:
edit “harry”;1
set subnet 213.154.12.123 255.255.255.0;2I want to mark both of them although the one above has the 1 value that doesnt match with the last bookmark.
-
@Unai-Abrisketa said:
I guess that you havent understood my question.
Most likely and I’m still confusedI want to mark the lines that were immediately above of the ones I marked before.
That is where my confusion comes from. You state that you have already marked it,
so why not doing the same with the line above?For example If i have the next two lines:
edit “harry”;1
set subnet 213.154.12.123 255.255.255.0;2I want to mark both of them although the one above has the 1 value that doesnt match with the last bookmark.
One question which you haven’t answered is whether you did bookmark it or whether you selected the
text and “marked” it with a different color or you used find dialog to mark or …Eko
-
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 optionSearch > 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 lineandPurge for each search -
Tick, preferably, the
Wrap aroundoption, too -
Select the
Regular expressionsearch mode ( IMPORTANT ) -
Click ONCE, on the
Replace Allbutton
=> In one go :
-
All the lines, containing the
;2string, 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
11bookmarked 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
;2string, 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
-
-
@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.
-
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
-
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
XXXand each line containing this criterion,LINE XRemarks :
-
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 Xare separated by some lines. In theMinimum X....Xcolumn, of the two tables, I mentioned, for each regex, the minimum number of lines, between twoLINE 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 aLINE X, must contain, at least,2characters 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
-
-
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() -
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 these5linesOf 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
5lines before and5lines after, which could be bookmarked, or not, independently…Cheers,
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…
-
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
ValueErroras the search string (I am searching the file containing the script’s own source code):
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
ValueErrorin it to be bookmarked):
Similarly, after that one is asked about the “after” lines, to which I have answered “2”:

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):

As you can see, the line with the hit is bookmarked, as is the 3 lines immediately before and the 2 lines immediately after.
-
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.
-
This post is deleted!