Community
    • Login

    How to add lines in specific locations

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    21 Posts 5 Posters 2.0k 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.
    • ImgemaI
      Imgema
      last edited by Imgema

      Ok here’s what i’m trying to do.

      Lets say i have something like this:


      Game: Game name 1
      <image>C:/Games/game 1.png</image>
      <box>C:/Games/game 1.png</box>

      Game: Game name 2
      <image>C:/Games/game 2.png</image>
      <box>C:/Games/game 2.png</box>


      How to add one line under each <box> line so it ends up like this:


      Game: Game name 1
      <image>C:/Games/game 1.png</image>
      <box>C:/Games/game 1.png</box>
      <thumb>C:/Games/game 1.png</thumb>

      Game: Game name 2
      <image>C:/Games/game 2.png</image>
      <box>C:/Games/game 2.png</box>
      <thumb>C:/Games/game 2.png</thumb>


      The new <thumb> line needs to be under the <box> line for each entry. I have thousands of entries so i can’t just add them manually, it needs to be a bulk edit.

      Thanks!

      Alan KilbornA Paul WormerP 2 Replies Last reply Reply Quote 0
      • Alan KilbornA
        Alan Kilborn @Imgema
        last edited by

        @imgema

        I suppose you could try:

        Find: (?-s)<box>(.+)</box>(\R)
        Replace: $0<thumb>$1<thumb>$2
        Search mode: Regular expression

        Neil SchipperN ImgemaI 2 Replies Last reply Reply Quote 3
        • Paul WormerP
          Paul Wormer @Imgema
          last edited by

          @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>")
          
          
          Alan KilbornA 1 Reply Last reply Reply Quote 1
          • Alan KilbornA
            Alan Kilborn @Paul Wormer
            last edited by

            @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.

            Paul WormerP 1 Reply Last reply Reply Quote 3
            • Neil SchipperN
              Neil Schipper @Alan Kilborn
              last edited by

              @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 KilbornA 1 Reply Last reply Reply Quote 2
              • Paul WormerP
                Paul Wormer @Alan Kilborn
                last edited by

                @alan-kilborn
                OK, I didn’t know that. Doesn’t “Replace All” give problems when you have “wrap around” on?

                Alan KilbornA 1 Reply Last reply Reply Quote 0
                • Alan KilbornA
                  Alan Kilborn @Neil Schipper
                  last edited by Alan Kilborn

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

                  1 Reply Last reply Reply Quote 1
                  • Alan KilbornA
                    Alan Kilborn @Paul Wormer
                    last edited by

                    @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.

                    Paul WormerP 1 Reply Last reply Reply Quote 1
                    • Paul WormerP
                      Paul Wormer @Alan Kilborn
                      last edited by

                      @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.

                      Alan KilbornA 1 Reply Last reply Reply Quote 2
                      • Alan KilbornA
                        Alan Kilborn @Paul Wormer
                        last edited by

                        @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.

                        Paul WormerP 2 Replies Last reply Reply Quote 2
                        • Paul WormerP
                          Paul Wormer @Alan Kilborn
                          last edited by

                          @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.

                          Alan KilbornA 1 Reply Last reply Reply Quote 0
                          • Alan KilbornA
                            Alan Kilborn @Paul Wormer
                            last edited by

                            @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.

                            1 Reply Last reply Reply Quote 2
                            • Paul WormerP
                              Paul Wormer @Alan Kilborn
                              last edited by

                              @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.

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

                                @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.

                                PeterJonesP 1 Reply Last reply Reply Quote 2
                                • PeterJonesP
                                  PeterJones @Alan Kilborn
                                  last edited by PeterJones

                                  @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 of five and it was remembering that cursor location as the “start/end” point, then it would search from five to seven not finding a match, then wrap around to one, skip lines one and two, and match from three to four – stopping because it got to the “start/end” at line five. =>

                                  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
                                  
                                  1 Reply Last reply Reply Quote 4
                                  • PeterJonesP
                                    PeterJones @Paul Wormer
                                    last edited by

                                    @paul-wormer ,

                                    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.

                                    Paul WormerP 1 Reply Last reply Reply Quote 3
                                    • Paul WormerP
                                      Paul Wormer @PeterJones
                                      last edited by

                                      @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).

                                      Alan KilbornA 3 Replies Last reply Reply Quote 2
                                      • Alan KilbornA
                                        Alan Kilborn @Paul Wormer
                                        last edited by

                                        @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.

                                        1 Reply Last reply Reply Quote 2
                                        • Alan KilbornA
                                          Alan Kilborn @Paul Wormer
                                          last edited by Alan Kilborn

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

                                          93752776-4ae3-413f-b521-97b08c68f7af-image.png

                                          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.

                                          1 Reply Last reply Reply Quote 1
                                          • Alan KilbornA
                                            Alan Kilborn @Paul Wormer
                                            last edited by

                                            @paul-wormer

                                            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:

                                            35f926f4-afda-4aa7-8cd9-27d93fa0badb-image.png

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