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

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

    Use the visual block command v (or V for whole lines and C-V for rectangular blocks). While in visual block mode, you can use any motion commands including search; I use } frequently to skip to the next blank line. Once the block is marked, you can :w it to a file, delete, yank, or whatever. If you execute a command and the visual block goes away, re-select the same block with gv. See :help visual-change for more.

    I think there are language-specific scripts that come with vim that do things like comment out blocks of code in a way that fits your language of choice.

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

    My block comment technique:

    Ctrl+V to start blockwise visual mode.

    Make your selection.

    With the selection still active, Shift+I. This put you into column insert mode.

    Type you comment characters '#' or '//' or whatever.

    ESC.

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

    For commenting out lines, I would suggest one of these plugins:

    EnhancedCommentify

    NERD Commenter

    I find myself using NERD more these days, but I've used EnhancedCommentify for years.

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

    The visual mode is the solution for your main problem. As to commenting out sections of code, there are many plugins for that on vim.org, I am using tComment.vim at the moment.

    There is also a neat way to comment out a block without a plugin. Lets say you work in python and # is the comment character. Make a visual block selection of the column you want the hash sign to be in, and type I#ESCAPE. To enter a visual block mode press C-q on windows or C-v on linux.

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