Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

vi

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 delete multiscreen blocks of text or write e.g. 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?

like image 488
Edward Tanguay Avatar asked Sep 11 '25 03:09

Edward Tanguay


1 Answers

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.

like image 154
zigdon Avatar answered Sep 12 '25 17:09

zigdon