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

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 eg select and delete multiscreen blocks of text or write eg three large methods out to another file and then delete them for testing purposes in Vim when using it via putty/ssh where you cannot use the mouse?

I can easily yank-to-the-end-of-line or yank-to-the-end-of-code-block but if the text extends over many screens, or has lots of blank lines in it, I feel like my hands are tied in Vim. Any solutions?

And a related question: is there a way to somehow select 40 lines, and then comment them all out (with "#" or "//"), as is common in most IDEs?


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.


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 (.) d elete."

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++)

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


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'.

链接地址: http://www.djcxy.com/p/49456.html

上一篇: 如何在Vim中注释掉一段Python代码

下一篇: 在Vim中,选择,删除或注释大部分多行的最佳方式是什么