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

前端 未结 16 1165
醉话见心
醉话见心 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:14

    marks would be the simplest mb where u want to begin and me where u want to end once this is done you can do pretty much anything you want

    :'b,'ed
    

    deletes from marker b to marker e

    commenting out 40 lines you can do in the visual mode

    V40j:s/^/#/
    

    will comment out 40 lines from where u start the sequence

    0 讨论(0)
  • 2021-01-31 10:16

    Press V (uppercase V) and then press 40j to select 40 lines and then press d to delete them. Or as @zigdon replied, you can comment them out.

    0 讨论(0)
  • 2021-01-31 10:25

    Use Shift+V to go in visual mode, then you can select lines and delete / change them.

    0 讨论(0)
  • 2021-01-31 10:29

    If you want to perform an action on a range of lines, and you know the line numbers, you can put the range on the command line. For instance, to delete lines 20 through 200 you can do:

    :20,200d
    

    To move lines 20 through 200 to where line 300 is you can use:

    :20,200m300
    

    And so on.

    0 讨论(0)
  • 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`.

    0 讨论(0)
  • 2021-01-31 10:33

    To insert comments select the beginning characters of the lines using CTRL-v (blockwise-visual, not 'v' character wise-visual or 'V' linewise-visual). Then go to insert-mode using 'I', enter your comment-character(s) on the first line (for example '#') and finally escape to normal mode using 'Esc'. Voila!

    To remove the comments use blockwise-visual to select the comments and just delete them using 'x'.

    0 讨论(0)
提交回复
热议问题