How to add lines in specific locations
-
I suppose you could try:
Find:
(?-s)<box>(.+)</box>(\R)
Replace:$0<thumb>$1<thumb>$2
Search mode: Regular expression -
@imgema
Alan’s solution is brief and elegant but you have to repeat it for every entry. Below is a script that does it in one shot. You need the plugin pythonscript for it and your file must end with an empty line.from Npp import * i = 0 pos = editor.findText(FINDOPTION.WHOLEWORD, 0, editor.getLength(), "<box>") while pos: i += 1 p = editor.positionFromLine(editor.lineFromPosition(pos[0])+1) editor.insertText(p, '<thumb>C:/Games/game {}.png</thumb>\r\n'.format(i)) pos = editor.findText(FINDOPTION.WHOLEWORD, pos[1], editor.getLength(), "<box>")
-
@paul-wormer said in How to add lines in specific locations:
Alan’s solution … you have to repeat it for every entry.
Not true.
Sure, you could walk thru the replacements one by one by pressing the Replace button, but after you’ve done that a few times verifying it is operating correctly, switch to a single press of Replace All to replace the remaining (“thousands”) of occurrences in the file.
This is just basic regex replacement. A script for this is overkill.
-
@alan-kilborn said in How to add lines in specific locations:
Replace: $0<thumb>$1<thumb>$2
Closing tag needs a slash:
$0<thumb>$1</thumb>$2
-
@alan-kilborn
OK, I didn’t know that. Doesn’t “Replace All” give problems when you have “wrap around” on? -
@neil-schipper said in How to add lines in specific locations:
Closing tag needs a slash: $0<thumb>$1</thumb>$2
Sure but I think OP would have been capable of adding that in, after my oversight. :-)
-
@paul-wormer said in How to add lines in specific locations:
Doesn’t “Replace All” give problems when you have “wrap around” on?
No…but what kind of problem are you thinking about?
Replace All + Wrap around will make one pass thru the entire file, starting at the top. It doesn’t care where the caret/cursor currently is.
-
@alan-kilborn said in How to add lines in specific locations:
No…but what kind of problem are you thinking about?
Obviously that the editor keeps on finding <box> and keeps on inserting <thumb>.
BTW, my compliments for your noticing that a partial string copy was possible and the elegant way you solved it. My solution was much more brute force.
-
@paul-wormer said in How to add lines in specific locations:
Obviously that the editor keeps on finding <box> and keeps on inserting <thumb>
You mean finding the same <box> over and over?
It doesn’t do this because after a replacement it starts the next search after the position of the replacement text. -
@alan-kilborn said in How to add lines in specific locations:
You mean finding the same <box> over and over?
No, I mean when the editor wraps around and starts at the top, it will go down finding boxes and inserting thumbs again. But I guess it remembers its starting position, although remembering is not trivial, because lines are added, both above and below the starting position.
-
@paul-wormer said in How to add lines in specific locations:
I mean when the editor wraps around and starts at the top, it will go down finding boxes
Perhaps you didn’t see my earlier point:
Replace All + Wrap around will make one pass thru the entire file, starting at the top. It doesn’t care where the caret/cursor currently is.
There is no “wrap around” with Replace All. In this case think of the Wrap around checkbox as saying “Entire document top-to-bottom”.
The only time “remembering the starting position” is important is when Replace (not All) or Find Next is pressed, and the text isn’t found – in these cases the software needs to know where to stop looking after wrapping around. Basically what it does, for a downward search is TWO searches: from caret/cursor to end-of-file, and then from top-of-file to caret location. In neither case does the starting position move based upon a replacement changing text.
-
@alan-kilborn
I’m sorry, I didn’t read too well. You said that “Replace all” starts at the top, whereas I assumed that it started at the caret (current position), worked down, and wrapped around. -
@paul-wormer said in How to add lines in specific locations:
I assumed that it (Replace All) started at the caret (current position), worked down, and wrapped around.
Well, it could work that way, but that would be more complicated for the developers for that type of replacement.
-
@alan-kilborn said in How to add lines in specific locations:
@paul-wormer said in How to add lines in specific locations:
I assumed that it (Replace All) started at the caret (current position), worked down, and wrapped around.
Well, it could work that way, but that would be more complicated for the developers for that type of replacement.
It could also make some regex not work as expected. Let’s say I had the cursor partway through a document,
one two three four five six seven
and set up a regex that said “skip the first two lines at the beginning of the file, then replace the rest of the file with
deleted
”:
FIND =\A(?-s:^.*$\R){2}\K(?s:.*)
REPLACE =deleted
If it did it your ( @paul-wormer 's) way, and you had the cursor at the start offive
and it was remembering that cursor location as the “start/end” point, then it would search fromfive
toseven
not finding a match, then wrap around toone
, skip linesone
andtwo
, and match fromthree
tofour
– stopping because it got to the “start/end” at linefive
. =>one two deletedfive six seven
Fortunately, it does not do that. Instead, it starts at the beginning of the file, skips the first two lines, and matches the whole rest of the document like was intended. =>
one two deleted
-
Also, please note that this behavior is well-defined in the online user manual:
- Replace All: With ☑ Wrap Around ticked, it makes one pass through the active document, from the very top to the very bottom, and replaces all occurrences found. With ☐ Wrap Around unticked, it searches from the caret to the end of the file (if ☐ Backward direction is unticked) or from the beginning of the file to the caret (if ☑ Backward direction is ticked) and replaces all occurrences found in that region.
– https://npp-user-manual.org/docs/searching/#find-replace-tabs
This description was specifically clarified last year, to avoid any confusion as to what Replace All means in conjunction with the Wrap Around and Backward direction options.
-
@peterjones
Thank you, I understand it completely now.Probably others have remarked this before, but the name of the box “Wrap around” is misleading for a “Replace all”, because - as you point out - there is no wrapping. The term “Wrap around” is the source of my confusion. Easier to understand would be three replacement boxes labeled “Overall”, “Downward”, and “Upward” or something similar. (I do not mean this as suggestion for an adaptation of the UI. The Find panel is already crowded enough, IMHO).
-
@paul-wormer said in How to add lines in specific locations:
Probably others have remarked this before, but the name of the box “Wrap around” is misleading for a “Replace all
Yes, they have. But, this UI is trying to do a maximum of things with a minimum of UI components, because, as you say:
The Find panel is already crowded enough,
So, really, it is just a matter of learning how the software works.
-
@paul-wormer said in How to add lines in specific locations:
Easier to understand would be three replacement boxes labeled “Overall”, “Downward”, and “Upward” or something similar.
Maybe that would be even more confusing, as there’s already a Backward direction checkbox (which resembles your “Upward”).
Also, having an “Overall” choice makes Wrap around redundant, but Wrap around is needed for Find Next and Replace !
Historical note: I forget which version it was changed in, but the Backward direction checkbox used to be two radio buttons in the UI:
As you say, you’re not suggesting changes to the UI, and this is a good thing, because developers don’t listen to those requests for the Find family of windows.
-
And… if it helps you, you could always customize the text of the UI yourself (there’s a way to do it without rebuilding the software), so you’d have something like this for the Wrap around box:
-
Your solution worked. I added the missing “/” in the second <thumb> part where it was needed and by using “replace all” it applied the lines in all entries.
Thanks all for the replies :)