In Vim, what is the best way to select, delete, or comment out large portions of multi-screen text?

前端 未结 16 1170
醉话见心
醉话见心 2021-01-31 09:43

Selecting a large amount of text that extends over many screens in an IDE like Eclipse is fairly easy since you can use the mouse, but what is the best way to e.g. select and de

16条回答
  •  迷失自我
    2021-01-31 10:31

    Use markers.

    Go to the top of the text block you want to delete and enter

    ma
    

    anywhere on that line. No need for the colon.

    Then go to the end of the block and enter the following:

    :'a,.d
    

    Entering ma has set marker a for the character under the cursor.

    The command you have entered after moving to the bottom of the text block says "from the line containing the character described by marker a ('a) to the current line (.) delete."

    This sort of thing can be used for other things as well.

    :'a,.ya b     - yank from 'a to current line and put in buffer 'b'
    :'a,.ya B     - yank from 'a to current line and append to buffer 'b'
    :'a,.s/^/#/   - from 'a to current line, substitute '#' for line begin
    (i.e. comment out in Perl)
    :'s,.s#^#//#  - from 'a to current line, substitute '//' for line begin
    (i.e. comment out in C++)
    

    N.B. 'a (apostrophe-a) refers to the line containing the character marked by a. ``a(backtick-a) refers to the character marked bya`.

提交回复
热议问题