Community
    • Login

    Delete every line below a bookmarked line.

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    47 Posts 6 Posters 2.3k 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.
    • Antheds KennelA
      Antheds Kennel @guy038
      last edited by

      @guy038 Hello and happy new year, what I attached last time was a suggestion from ChatGPT. What I do is:

      1. Bookmark all the lines that I want to delete (after I delete the line below).
      2. Record a macro where I click F2->down arrow.
      3. CTRL+L to delete that line.
      4. Stop recording.
      5. Click Macro->Run A Macro Multiply Times->Run Until…(with the macro I just created).
      6. Now I’m supposed to go via Search->Bookmark->Remove Bookmarked Lines, but I can’t get that far because the macro deletes more than what I want to delete.
      1 Reply Last reply Reply Quote 0
      • guy038G
        guy038
        last edited by guy038

        Hi, @antheds-kennel, @datatraveller1, @dr-ramaanand, @peterjones and All,

        @antheds-kennel, you said :

        I want to delete all the lines which are below the bookmarked lines

        OK ! Then, if I consider this INPUT text, in a new tab :

          Line 01
          Line 02
          Line 03
        • Line 04
          Line 05
          Line 06
        • Line 07
          Line 08
          Line 09
          Line 10
          Line 11
        • Line 12
          Line 13
          Line 14
          Line 15
          Line 16
          Line 17
          Line 18
          Line 19
          Line 20
          Line 21
          Line 22
        

        Where only the lines 4, 7 and 12 are bookmarked

        You’re expecting this OUTPUT text, aren’t you ?

          Line 01
          Line 02
          Line 03
        • Line 04
        • Line 07
        • Line 12
        

        If this behavior is the one you looking for :

        • Firstly, bookmark all the unmarked lines which lay before the first bookmarked line of current file ( so the lines 01, 02 and 03 in our example )

        • Secondly, do a mouse right-click, within the bookmark margin, on the left, and run the Remove Non-Bookmarked Lines option

        BR

        guy038

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

          @guy038 said :

          …bookmark all the unmarked lines which lay before the first bookmarked line

          This may be a lot of manual effort if there are many lines upon which to place a bookmark – think about clicking the margin or pressing F2 hundreds of times. :-(

          Making a text selection of all of these lines and then choosing the Toggle Bookmark command does not do it; it sets a bookmark on the (single) line of the caret in the selection.

          Here’s a way to automate placing bookmarks on multiple lines, acting on @guy038 's example text:

          • put a character that does not appear in the document at the end of the last line upon which bookmarking of all individual lines is desired:
            c00a17d5-7895-41d8-9703-5067f6b9fd7d-image.png
          • run this marking operation:
            b8f93f73-44a1-46c5-82f3-c4f3c51f388c-image.png
          • obtain this result:
            520d4b7c-b975-424c-b75e-62932cfb3302-image.png
          • remove the extra character added earlier
          1 Reply Last reply Reply Quote 2
          • guy038G
            guy038
            last edited by

            Hello, @alan-kilborn and All,

            Alan, nice catch ;-))

            Happy New Year !

            guy038

            1 Reply Last reply Reply Quote 1
            • guy038G
              guy038
              last edited by

              Hello, @alan-kilborn and All,

              BTW, I’m thinking : would it be a problem if :

              • When no standard selection, in current file, the commands Toggle Bookmark, Clear All Bookmarks, Cut Bookmarked Lines, Copy Bookmarked Lines , Paste to (Replace) Boomarked lines, Remove Bookmarked lines, Remove Non-Bookmarked Lines and Inverse Bookmark would act on the entire file

              • When a standard selection would occur ( even partially ), these eight commands would act on that selection only


              • Could this new behavior be extended to multiple standard selections ?

              • More important : what about a Ctrl + Z / Ctrl + Y action ?

              Best Regards,

              guy038

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

                @guy038 said :

                When no standard selection, in current file, the commands Toggle Bookmark, Clear All Bookmarks, Cut Bookmarked Lines, Copy Bookmarked Lines , Paste to (Replace) Boomarked lines, Remove Bookmarked lines, Remove Non-Bookmarked Lines and Inverse Bookmark would act on the entire file

                With the exception of Toggle Bookmark, these commands already all work on the entire file.

                When a standard selection would occur ( even partially ), these eight commands would act on that selection only

                You should start with a “feature request” for this behavior.

                Could this new behavior be extended to multiple standard selections ?

                I would think that only a single stream-type selection should be supported. Anything more than that overcomplicates a feature request (and probably kills it).


                what about a Ctrl + Z / Ctrl + Y action ?

                Not applicable.
                Undo/redo applies to textual changes only.

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

                  You should start with a “feature request” for this behavior.

                  And HERE it is.

                  dr ramaanandD 1 Reply Last reply Reply Quote 1
                  • dr ramaanandD
                    dr ramaanand @Alan Kilborn
                    last edited by dr ramaanand

                    @Alan-Kilborn @guy038 and all: The regular expression method explained by @guy038 and @PeterJones would have been best but due to some fixed ideas, the OP is unwilling to use it. Happy New Year!

                    Alan KilbornA 1 Reply Last reply Reply Quote 0
                    • Alan KilbornA
                      Alan Kilborn @dr ramaanand
                      last edited by

                      @dr-ramaanand said in Delete every line below a bookmarked line.:

                      the OP is unwilling to use it

                      Well, if it comes down to being able to obtain an end result that you need, and you refuse to use a technique that gets you there, I guess you may not get to your destination.

                      1 Reply Last reply Reply Quote 1
                      • Antheds KennelA
                        Antheds Kennel @guy038
                        last edited by

                        @guy038 said in Delete every line below a bookmarked line.:

                        Hi, @antheds-kennel, @datatraveller1, @dr-ramaanand, @peterjones and All,

                        Sorry, @antheds-kennel, But I still do not understand your goal ! You said :

                        I only want to delete the lines below the ones that are bookmarked.

                        Do you mean :

                        I only want to delete each SINGLE line which is below the bookmarked lines

                        or

                        I only want to delete all the lines which are below the bookmarked lines


                        In addition, in the description of your macro, you said, twice :

                        Press Ctrl+F2 to jump to the next bookmark.

                        This is not right ! This feature does not enable you to move to the next bookmark ! The Ctrl + F2 feature TOGGLE the Bookmark status of the current line :

                        • If the current line is not bookmarked, a Ctrl + F2 action will bookmark this line

                        • If the current line is bookmarked, a Ctrl + F2 action will un-bookmark this line


                        To move to a bookmark, you can, either :

                        • Hit the F2 key, which moves to the next bookmark

                        • Hit the Shift + F2 combination, which moves to the previous bookmark

                        BR

                        guy038
                        Hello and THANK YOU for your patience, what you are referring to is what ChatGPT suggested, I think I missed being clear that there is only ONE row under each bookmarked row that should be deleted.

                        1 Reply Last reply Reply Quote 0
                        • Antheds KennelA
                          Antheds Kennel
                          last edited by Antheds Kennel

                          Hi again, I have an example here where you probably understand better what I mean. I have different albums that I have marked up with bookmarks and want to delete, but in the example I have not bookmarked any as I think you can randomly make your own to make it work. In the example there are six albums where for example I only want to save 3 (every other), then I think you first delete the line DIRECTLY BELOW and then delete the bookmarks.

                          #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                          D:/MP3/Sweden/ABBA/The Album
                          #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                          D:/MP3/Sweden/ABBA/ABBA
                          #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                          D:/MP3/Sweden/ABBA/Ring ring
                          #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                          D:/MP3/Sweden/ABBA/Voyage
                          #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                          D:/MP3/Sweden/ABBA/Super Trouper
                          #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                          D:/MP3/Sweden/ABBA/Waterloo
                          

                          Kind regards Thore

                          PeterJonesP 1 Reply Last reply Reply Quote 0
                          • PeterJonesP
                            PeterJones @Antheds Kennel
                            last edited by PeterJones

                            @Antheds-Kennel ,

                            The macro idea isn’t bad (I hate admitting that a generative AI got even partial credit).

                            Unfortunately for the AI’s reputation, in my initial experiments, it seems that recording the Search > Bookmark > Next Bookmark action (F2) doesn’t just record that menu entry; instead, it records an underlying SCI_GOTOLINE (2024) command that I wasn’t expecting, as well. :

                                        <Action type="0" message="2024" wParam="2" lParam="0" sParam="" />
                            

                            Because that line was recorded for me, then when the run-multiple-times tells it to run again, then it goes back to that same starting line, and thus goes to the same bookmark as before, and deletes the next line under it as well. As it loops, it will obviously then delete a lot more than you were hoping.

                            The AI didn’t know Notepad++ would record that extra GOTO, and thus “unintentionally” misinformed you. Since the generative AI only spout the most likely text based on what it was trained on from scraping the internet, the answers it gives are guaranteed to be the most likely based on its training set, but as can be seen from any experience on the internet, the most likely text is not likely to be the most accurate or correct text. (It is hard to call it truly unintentional, because the GPT providers all brag about how good their models are at providing good-sounding data, without the emphasis that they should provide that it’s only “most likely”, not “most accurate”. I think they should be held accountable for this misleading bragging, and the misinformation and potential damage that they thus cause.)

                            Really, as was suggested above, the easiest would be to bookmark the lines you want to delete, instead of bookmarking the lines above what you want to delete.

                            But if you insist, I have manually crafted a replacement macro for you to use. Close Notepad++, open again, edit %AppData%\Notepad++\shortcuts.xml, find your macro (by searching for its name), and replace its <Action...> lines with the <Action...> lines from my macro below:

                                    <Macro name="DelUnderBookmarks" Ctrl="yes" Alt="no" Shift="yes" Key="121">
                                        <Action type="2" message="0" wParam="43006" lParam="0" sParam="Next Bookmark" />
                                        <Action type="2" message="0" wParam="43005" lParam="0" sParam="Toggle Bookmark" />
                                        <Action type="0" message="2300" wParam="0" lParam="0" sParam="Line Down" />
                                        <Action type="0" message="2338" wParam="0" lParam="0" sParam="Line Delete" />
                                    </Macro>
                            

                            This will move to the next bookmark, from wherever you are, then disable that line’s bookmark, then moves down and deletes the line (the macro recorder for Ctrl+L actually CUT the line using SCI_LINECUT 2337 rather than DELETED the line using SCI_LINEDELETE 2338 (mapped to Ctrl+Shift+L for me), so it clobbered your clipboard; I switched to 2338 to do the SCI_LINEDELETE instead.) Since this alternate macro toggles off each bookmark as it’s found, it won’t accidentally go to the same location again and re-delete.

                            When I run my shown macro from the start to the end of file, if I had bookmarked 3, 7, and 9, then it deleted what were 4, 8, and 10. If I leave out the Toggle Bookmark from the macro, it will delete all the lines from 4 onward, because the old bookmark locations can be re-visited.

                            If you need to keep the bookmarks on the original lines, even after the subsequent line was deleted, then it would take more effort to avoid that problem – and probably becomes something not solvable with macros alone. At which point, I would recommend installing PythonScript plugin, and then you could write a script which adds in the safety logic which a macro alone cannot provide.

                            dr ramaanandD Antheds KennelA 2 Replies Last reply Reply Quote 1
                            • dr ramaanandD
                              dr ramaanand @PeterJones
                              last edited by

                              @Antheds-Kennel If you tell us which lines you want to delete exactly, we can help you with a regular expression, if not, you are wasting our time

                              Antheds KennelA 1 Reply Last reply Reply Quote 0
                              • Antheds KennelA
                                Antheds Kennel @PeterJones
                                last edited by Antheds Kennel

                                @PeterJones YES, the only thing now is that the last line is deleted. I don’t know if I understood you correctly, but this is what I changed in my Macro:

                                #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                D:/MP3/Sweden/ABBA/The Album
                                #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                D:/MP3/Sweden/ABBA/Ring ring
                                #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                

                                But I want it to look like this:

                                #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                D:/MP3/Sweden/ABBA/The Album
                                #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                D:/MP3/Sweden/ABBA/Ring ring
                                #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                D:/MP3/Sweden/ABBA/Waterloo
                                

                                I changed my macro code from this:

                                <Macro name="DelUnderBookmarks" Ctrl="no" Alt="no" Shift="no" Key="0">
                                            <Action type="0" message="2024" wParam="2" lParam="0" sParam="" />
                                            <Action type="2" message="0" wParam="43006" lParam="0" sParam="" />
                                            <Action type="0" message="2300" wParam="0" lParam="0" sParam="" />
                                            <Action type="0" message="2337" wParam="0" lParam="0" sParam="" />
                                        </Macro>
                                

                                To this one where I copied your suggestion which does everything right up to the last bookmark, where it also removes what I want to save:

                                <Macro name="DelUnderBookmarks" Ctrl="no" Alt="no" Shift="no" Key="0">
                                            <Action type="2" message="0" wParam="43006" lParam="0" sParam="Next Bookmark" />
                                            <Action type="2" message="0" wParam="43005" lParam="0" sParam="Toggle Bookmark" />
                                            <Action type="0" message="2300" wParam="0" lParam="0" sParam="Line Down" />
                                            <Action type="0" message="2338" wParam="0" lParam="0" sParam="Line Delete" />
                                        </Macro>
                                

                                So I think I’ve somehow made a mistake that also deletes the last bookmark row that we want to save, assuming it’s 3,7, and 9 we want to delete.
                                Din “Macro name” rad ser ut så här:

                                <Macro name="DelUnderBookmarks" Ctrl="yes" Alt="no" Shift="yes" Key="121">
                                

                                Och min ser ut så här:

                                <Macro name="DelUnderBookmarks" Ctrl="no" Alt="no" Shift="no" Key="0">
                                

                                Kan det vara skillnaden?
                                So now it’s REALLY CLOSE to get it working, you’re a rock at this, you know, THANK YOU :)

                                1 Reply Last reply Reply Quote 0
                                • Antheds KennelA
                                  Antheds Kennel @dr ramaanand
                                  last edited by

                                  @dr-ramaanand If it were that easy, unfortunately, I have about: 200,000 lines where the # lines that I bookmarked have different names. I have managed to make a code that bookmarks all the ones I want to delete with the differences that I found. So now it is “only” the line under all the bookmarks that needs to be deleted. Then I re-bookmark those lines with # again and delete the bookmarked lines.

                                  1 Reply Last reply Reply Quote 0
                                  • Antheds KennelA
                                    Antheds Kennel
                                    last edited by

                                    @Antheds-Kennel said in Delete every line below a bookmarked line.:Something went wrong at the end of my post, and I’m not allowed to correct it since it’s been 4 hours. So the Swedish part is still there, sorry. But it should be like this:

                                    Your “Macro name” line looks like this:

                                    <Macro name="DelUnderBookmarks" Ctrl="yes" Alt="no" Shift="yes" Key="121">
                                    

                                    And mine looks like this:

                                    <Macro name="DelUnderBookmarks" Ctrl="no" Alt="no" Shift="no" Key="0">
                                    

                                    Could that be the difference?
                                    So now it’s REALLY CLOSE to get it working, you’re a rock at this, you know, THANK YOU :)

                                    PeterJonesP 1 Reply Last reply Reply Quote 0
                                    • PeterJonesP
                                      PeterJones @Antheds Kennel
                                      last edited by

                                      @Antheds-Kennel,

                                      No, the only difference in that <Macro name="..."...> line is that mine had a keyboard shortcut assigned and yours did not. Remember, after editing the macro, you must save, exit Notepad++, and reload; and if you had done any other macro recording or keyboard shortcut setting in the same session where you edited the macro, you will have overwritten it, and you will need to re-edit, making sure to exit Notepad++ and start it again before editing and after saving and editing, otherwise it will be overwritten when Notepad++ exits.

                                      Unfortunately, from your posted data, I cannot tell what you had before you ran the macro, where your bookmarks were, and why what you got was different than what you expected. Without all that data, unambiguous, there is no way I can guess what went wrong.

                                      My macro literally goes “move to next bookmark, toggle the bookmark off, go down one line, and delete the line”. If that is not the algorithm you want, then your previous descriptions have all been insufficient to say what you really want.

                                      Antheds KennelA 1 Reply Last reply Reply Quote 0
                                      • Antheds KennelA
                                        Antheds Kennel @PeterJones
                                        last edited by Antheds Kennel

                                        @PeterJones Hi and thanks for the quick reply. What I’m using is this one which I bookmarked lines 3,7 and 9:

                                        #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                        D:/MP3/Sweden/ABBA/The Album
                                        #EXTINF: id="ABBA" name="ALBUM" group-title="Denmark"
                                        D:/MP3/Sweden/ABBA/ABBA
                                        #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                        D:/MP3/Sweden/ABBA/Ring ring
                                        #EXTINF: id="ABBA" name="ALBUM" group-title="Canada"
                                        D:/MP3/Sweden/ABBA/Voyage
                                        #EXTINF: id="ABBA" name="ALBUM" group-title="USA"
                                        D:/MP3/Sweden/ABBA/Super Trouper
                                        #EXTINF: id="ABBA" name="ALBUM" group-title="Sweden"
                                        D:/MP3/Sweden/ABBA/Waterloo
                                        

                                        And I have closed Notepad++ before I change the files, I have been creating macros for several years and made that very mistake in the beginning where I did not close the program before. So the question is as I posted in my last post why your script deletes the last line that I do not want to delete. Otherwise it works great. With kind regards Thore

                                        PeterJonesP 1 Reply Last reply Reply Quote 0
                                        • PeterJonesP
                                          PeterJones @Antheds Kennel
                                          last edited by PeterJones

                                          @Antheds-Kennel said in Delete every line below a bookmarked line.:

                                          why your script deletes the last line that I do not want to delete.

                                          It’s a macro, not a script, so it has no logic capability. I hadn’t noticed it doing that during my earlier tests, but I can reproduce. Essentially what happens is after the last bookmark (on what was originally line 9) is removed and it deletes the next line as it should have, it hasn’t reached the end yet, so searches for a bookmark again, and doesn’t find one, but still moves forward in the macro (because macros cannot have logic) and thus toggles the non-existent bookmark to “on”, then moves down a line, and deletes it, then it searches for the next bookmark which requires it to wrap around, finds one, toggles it, and deletes the next line, and keeps going through those accidental loops until the down-a-line finally reaches the end of the file.

                                          As I tried to indicate earlier, it was entirely possible that a macro is not sufficient for your problem; we have now found the reason why. There might be a different way to construct the macro that would avoid that logic problem, but I cannot think of one. I really don’t think a macro is going to cut it.

                                          You still haven’t explained why bookmarking the actual lines you want to delete, rather than the lines before the ones you want to delete, is not a valid solution: any search that you can use to automatically mark those lines could be easily modified to mark the line after the text rather than the line on your matching text, thus bookmarking the lines you want to delete (and in that case, you could just delete them from the search/replace, rather than bookmarking as one search then deleting in a separate macro action)… And if you’re manually bookmarking the lines, rather than doing it from the Mark dialog, you’re wasting a lot of time, because if you’re already there to mark it, you could just as easily do the deletion yourself.

                                          So the right solution that I can see for you is: (1) if you are currently manually bookmarking, then just manually delete the next line; or (2) if you are bookmarking using the search-based Mark dialog, make a slight tweak to your search and use the Replace menu instead to immediately delete those. If you believe neither (1) nor (2) are sufficient for your needs, you will have to explain why, because I cannot see any reason why one of those two won’t work for the circumstances you’ve described. If you can come up with a meaningful reason, then I or someone else might be able to come up with a script for the PythonScript plugin that will have all the safety logic built in to follow your inefficient and illogical bookmark-the-wrong-line-then-delete-the-subsequent-lines-separately sequence that you seem to be insisting upon… but I still believe (1) or (2) would be better for you.

                                          Antheds KennelA 1 Reply Last reply Reply Quote 4
                                          • Antheds KennelA
                                            Antheds Kennel @PeterJones
                                            last edited by

                                            @PeterJones Hello again. THANK YOU for all the time you have put into this project, but I guess I’ll have to give up and give up. When someone like you says that this is difficult to solve in the way I thought I would, I have to give up unfortunately. I understand what you mean, but I don’t know how I’m going to do it, since there are 397,580 rows that I have to sort somehow. I thought I was on the right track when I managed to bookmark the row of the two that has groupings in it. The second row is just an address that is unique for each post. I thank you so much for all the help, you are super nice here. This was my first problem I have posted in this forum, and it is definitely not the last. I will use the ideas you have come up with on other occasions when I have saved them. Have a good time, with kind regards from a snow-white and icy Sweden.

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