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

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

    You should be aware of the normal mode command [count]CTRL-D. It optionally changes the 'scroll' option from 10 to [count], and then scrolls down that many lines. Pressing CTRL-D again will scroll down that same lines again.

    So try entering

    V     "visual line selection mode
    30    "optionally set scroll value to 30
    CTRL-D  "jump down a screen, repeated as necessary
    y      " yank your selection
    

    CTRL-U works the same way but scrolls up.

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

    v enters visual block mode, where you can select as if with shift in most common editors, later you can do anything you can normally do with normal commands (substitution :'<,'>s/^/#/ to prepend with a comment, for instance) where '<,'> means the selected visual block instead of all the text.

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

    My usual method for commenting out 40 lines would be to put the cursor on the first line and enter the command:

    :.,+40s/^/# /

    (For here thru 40 lines forward, substitute start-of-line with hash, space) Seems a bit longer than some other methods suggested, but I like to do things with the keyboard instead of the mouse.

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

    Or you may want to give this script a try...

    http://www.vim.org/scripts/script.php?script_id=23

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

    Well, first of all, you can set vim to work with the mouse, which would allow you to select text just like you would in Eclipse.

    You can also use the Visual selection - v, by default. Once selected, you can yank, cut, etc.

    As far as commenting out the block, I usually select it with VISUAL, then do

    :'<,'>s/^/# /
    

    Replacing the beginning of each line with a #. (The '< and '> markers are the beginning and and of the visual selection.

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

    First answer is currently not quite right? To comment out selection press ':' and type command :'<,'>s/^/# /g

    ('<, '> - will be there automatically)

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