• Login
Community
  • Login

How can I delete all rows before or after my current line?

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
8 Posts 4 Posters 24.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.
  • E
    Enno Dünkel
    last edited by Dec 7, 2020, 10:23 PM

    In the middle of a huge file - how can I delete all rows before or after my current line - something I miss in the menu. I searched the ‘Line Operations’- part in ‘Edit’ but perhaps this simple feature is still missing in notepad++?

    P 1 Reply Last reply Dec 7, 2020, 10:42 PM Reply Quote 0
    • T
      Terry R
      last edited by Dec 7, 2020, 10:34 PM

      @Enno-Dünkel said in How can I delete all rows before or after my current line?:

      how can I delete all rows before or after my current line

      A fairly easy solution. Ctrl-F2 on the line you wish to keep. This bookmarks the current line (you will see a blue circle at start of line). Then Use the “remove unmarked lines” which is under “Search” main menu, then “Bookmark” item.

      Terry

      1 Reply Last reply Reply Quote 1
      • P
        PeterJones @Enno Dünkel
        last edited by PeterJones Dec 7, 2020, 10:43 PM Dec 7, 2020, 10:42 PM

        @Enno-Dünkel ,

        There’s a way to select from the cursor to the end or beginning of the file, by default with Shift+Ctrl+END or Shift+Ctrl+HOME
        9f02112d-2ad1-47f6-ba51-bcd13ce42138-image.png

        With “normal” size files, those work find, followed by DELETE or BACKSPACE to delete them.

        However, with super long files (hundreds of thousands,or millions, of lines), that amount of data can be slow to select and then delete.

        Oh, while I was typing, @Terry-R replied with a different interpretation. If you really want to keep just the line your cursor is on and delete everything else, then that solution is better than what I explained. But if you want to keep half the document (either keep before or keep after your cursor) then that solution will delete too much.

        If you have the text

        first
        second
        cursor
        penultimate
        ultimate
        

        do you want to end up with @Terry-R’s

        cursor
        

        or my

        first
        second
        cursor
        

        Inspired by Terry’s suggestion of Bookmarks, if you only want to delete the lines after the current cursor (per “my interpretation”), maybe arrow down one line, then Search > Mark, then FIND=^.*$, Bookmark Line enabled, Wrap Around disabled, Regular Expression mode enabled, then Mark All, and then Search > Bookmark > Remove Bookmarked Lines. I don’t know if it’s faster to bookmark every line below the current and then delete them, or to select-then-delete as I initially described.

        A E 2 Replies Last reply Dec 8, 2020, 1:08 PM Reply Quote 1
        • T
          Terry R
          last edited by Dec 7, 2020, 11:41 PM

          @PeterJones said in How can I delete all rows before or after my current line?:

          then FIND=^.*$

          About the only thing I can see which would be better is to use \R instead. It catches the same number of lines but is shorter and may be quicker from the regex engine point of view. And yes I now think the OP wants the ability to do either before or after. My initial interpretation was both, but in hindsight either seems more logical.

          The only reason I posted was because with testing I found that if the very last line is an empty line, thus ONLY contains the file end marker (which by the way isn’t visible even when “View/Show All Symbols/show all characters” is ticked) then I can’t seem to bookmark it with a regex, I can manually. Also tried using the boundary (\b) but it doesn’t mark that very last empty line. Intriguing, or am I missing something? It would seem that to bookmark a line requires a full-width character (including the CR or LF codes), is that correct?

          Terry

          P 1 Reply Last reply Dec 8, 2020, 12:20 AM Reply Quote 2
          • P
            PeterJones @Terry R
            last edited by Dec 8, 2020, 12:20 AM

            @Terry-R said in How can I delete all rows before or after my current line?:

            thus ONLY contains the file end marker (which by the way isn’t visible even when “View/Show All Symbols/show all characters” is ticked)

            AFAIK, that’s because the end of file marker isn’t a character in the file (it’s not like the DOS ^Z days where the EOF marker character was part of the file bytestream)

            I can’t seem to bookmark it with a regex

            I couldn’t either. I tried with \Z too. That works for finding the line in FIND, but in MARK, it won’t bookmark the line. I think you are right, that there has to be something for the MARK/BOOKMARK to latch on to from the dialog box. Interesting.

            1 Reply Last reply Reply Quote 2
            • A
              Alan Kilborn
              last edited by Dec 8, 2020, 12:53 PM

              @Terry-R @PeterJones

              Yes, Mark’ing has to have some text to mark.

              Thus, if your regular expression matches zero actual characters (I suppose that is another way of saying it is a type of “assertion”), nothing will get marked/bookmarked.

              Definitely an easy experiment to try it with ^ which should hit on every line, but will result in 0 matches for purposes of a Mark.

              I believe this is by design. I looked at the code one time, and I believe it sets up different parameters for the “Boost search flags” when one is Mark’ing versus Find / Replace’ing.

              I wasn’t pursuing exactly that, but I noticed it when stepping through code, playing around with something else, as a search was being set up.

              1 Reply Last reply Reply Quote 2
              • A
                Alan Kilborn @PeterJones
                last edited by Dec 8, 2020, 1:08 PM

                @PeterJones said in How can I delete all rows before or after my current line?:

                with super long files (hundreds of thousands, or millions, of lines), that amount of data can be slow to select and then delete.

                I wonder how fast Pythonscript would delete from large files, without any kind of user-selection being made first ; some “one-liners”:

                • delete all characters from start-of-file to caret:
                  editor.deleteRange(0, editor.getCurrentPos())

                • delete all lines from start-of-file to line before caret’s:
                  editor.deleteRange(0, editor.positionFromLine(editor.lineFromPosition(editor.getCurrentPos())))

                • delete all characters from caret to end-of-file:
                  editor.deleteRange(editor.getCurrentPos(), editor.getLength() - editor.getCurrentPos())

                • delete all lines after caret’s line to end-of-file:
                  p = editor.positionFromLine(editor.lineFromPosition(editor.getCurrentPos()) + 1); editor.deleteRange(p, editor.getLength() - p)

                I didn’t actually try any of these on a super-large file, so they are just provided as “something to maybe experiment with”. :-)

                1 Reply Last reply Reply Quote 2
                • E
                  Enno Dünkel @PeterJones
                  last edited by Dec 9, 2020, 3:56 PM

                  @PeterJones: That 2 shortcuts work best for me - I don’t just want to keep the cursorline.
                  Sorry for my stupid question - thread is solved. And the solution is very simple…
                  Thanks a lot!

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