Loading ...
Sorry, an error occurred while loading the content.

How to delete range of lines without moving cursor?

Expand Messages
  • Asis Hallab
    Dear Vimmers, I was just wondering, if it is possible to delete a range of lines without moving the cursor. Example: line one line two line three current line
    Message 1 of 4 , Jan 16, 2013
    • 0 Attachment
      Dear Vimmers,

      I was just wondering, if it is possible to delete a range of lines
      without moving the cursor.

      Example:
      line one
      line two
      line three
      current line

      :1,2d
      Deletes the first two lines and leaves the cursor on line three.

      But I would like to leave it on the current position ('current line').

      Kind regards!

      --
      You received this message from the "vim_use" maillist.
      Do not top-post! Type your reply below the text you are replying to.
      For more information, visit http://www.vim.org/maillist.php
    • Chris Schneider
      Fast answer: do right after ( to go to mark, to the mark [where you last jumped from]). so :1,2d would do it. ... -- You received this
      Message 2 of 4 , Jan 16, 2013
      • 0 Attachment
        Fast answer: do '' right after (' to go to mark, to the ' mark [where you last jumped from]).  

        so   :1,2d<enter>''  would do it.




        On Wed, Jan 16, 2013 at 11:16 AM, Asis Hallab <asis.hallab@...> wrote:
        Dear Vimmers,

        I was just wondering, if it is possible to delete a range of lines
        without moving the cursor.

        Example:
        line one
        line two
        line three
        current line

        :1,2d
        Deletes the first two lines and leaves the cursor on line three.

        But I would like to leave it on the current position ('current line').

        Kind regards!

        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php

        --
        You received this message from the "vim_use" maillist.
        Do not top-post! Type your reply below the text you are replying to.
        For more information, visit http://www.vim.org/maillist.php
      • Andy Wokula
        Am 16.01.2013 19:16, schrieb Asis Hallab: Dear Vimmers, ... getline(), setline(), append(), ?delete()? There should be a function delete() to delete
        Message 3 of 4 , Jan 16, 2013
        • 0 Attachment
          Am 16.01.2013 19:16, schrieb Asis Hallab:> Dear Vimmers,
          >
          > I was just wondering, if it is possible to delete a range of lines
          > without moving the cursor.
          >
          > Example:
          > line one
          > line two
          > line three
          > current line
          >
          > :1,2d
          > Deletes the first two lines and leaves the cursor on line three.
          >
          > But I would like to leave it on the current position ('current line').
          >
          > Kind regards!

          Yep, looks like there is something "wrong" in Vim:

          :yank, :change, :append/:insert, :delete
          getline(), setline(), append(), ?delete()?

          There should be a function delete() to delete lines without moving the
          cursor, but the function of this name already deletes a file. And there
          is no other equivalent function.

          You can use
          :h winsaveview()
          :h winrestview()
          to save/restore the cursor position;
          or
          :let savpos = getpos(".")
          ...
          :call cursor(savpos[1:])
          " or :call setpos(".", savpos)
          or a mark ...

          --
          Andy

          --
          You received this message from the "vim_use" maillist.
          Do not top-post! Type your reply below the text you are replying to.
          For more information, visit http://www.vim.org/maillist.php
        • Asis Hallab
          Interesting! So, I will have to write my own command for this. I put this here in my .vimrc Delete range without moving cursor: com! -range D
          Message 4 of 4 , Jan 16, 2013
          • 0 Attachment
            Interesting!

            So, I will have to write my own command for this.
            I put this here in my .vimrc

            " Delete range without moving cursor:
            com! -range D <line1>,<line2>d | norm <C-o>

            >> Example:
            >> line one
            >> line two
            >> line three
            >> current line

            :1,2D

            now does the trick.

            Many thanks to everyone's feedback.


            >>
            >> :1,2d
            >> Deletes the first two lines and leaves the cursor on line three.
            >>
            >> But I would like to leave it on the current position ('current line').
            >>
            >> Kind regards!
            >
            >
            > Yep, looks like there is something "wrong" in Vim:
            >
            > :yank, :change, :append/:insert, :delete
            > getline(), setline(), append(), ?delete()?
            >
            > There should be a function delete() to delete lines without moving the
            > cursor, but the function of this name already deletes a file. And there
            > is no other equivalent function.
            >
            > You can use
            > :h winsaveview()
            > :h winrestview()
            > to save/restore the cursor position;
            > or
            > :let savpos = getpos(".")
            > ...
            > :call cursor(savpos[1:])
            > " or :call setpos(".", savpos)
            > or a mark ...
            >
            > --
            > Andy
            >
            >
            > --
            > You received this message from the "vim_use" maillist.
            > Do not top-post! Type your reply below the text you are replying to.
            > For more information, visit http://www.vim.org/maillist.php

            --
            You received this message from the "vim_use" maillist.
            Do not top-post! Type your reply below the text you are replying to.
            For more information, visit http://www.vim.org/maillist.php
          Your message has been successfully submitted and would be delivered to recipients shortly.