VIM to delete a range of lines into a register

橙三吉。 提交于 2019-11-29 13:06:10

The definition of "easiest" depends on what do you have, and what do you want to do

  1. if you have a start line number and end number, e.g.

    :2349,5344d a
    

    is the easiest way.

    You don't have to consider the questions like

    "where is my cursor?"

    "how many lines would be removed?" ...

  2. If you are about to remove a small amount of lines, particularly they are on same screen. (You could use relative-linenumber.) for example: "a5dd but you have to move your cursor to the first line you want to delete. And this could be done by option 1 too: 5:d a<CR> (vim will automatically translate it into .,.+5d a<CR>)

  3. If you just know the 1st line of deletion, and find the last line you want to delete by reading your text, (of course, small amount of lines) you could press V, and press j by reading, when it reaches the deletion ending border, press "ad

  4. If the "range" in your question is the "range" concept in vim, The first option would be better. since it could be 234,540, it could be 1;/foo, /foo/,/bar/... :h range see detail

so back to the first sentence in my answer, There is no absolutely easiest way. It all depends on what do you have, and what do you want to do.

The other way to achieve this would be to highlight the range of lines in visual line mode. (Shift-V)

Then type "ad while in visual line mode. This will put the deleted lines into the a register.

" followed by a register puts the next delete, yank or put into that register.

Below is the documentation for " (quote)

                                                            *quote*
"{a-zA-Z0-9.%#:-"}  Use register {a-zA-Z0-9.%#:-"} for next delete, yank
                    or put (use uppercase character to append with
                    delete and yank) ({.%#:} only work with put).

Another example of deleting multiple lines and putting it in a register. To delete 6 lines and put them in a register you can got to the line and type "a6dd. This puts the 6 deleted lines into register a.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!