tags:

views:

3008

answers:

16

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?

+8  A: 

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.

Greg Hewgill
+2  A: 

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

D4V360
+7  A: 

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

zigdon
Perfect, exactly what I wanted, thanks!
Edward Tanguay
@zigdon, how do you reverse commenting lines?
Gökhan Sever
Well, you can 'undo' ;) But if not, you could do something like `:s/^# //` on the selected lines.
zigdon
A: 

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.

Vinko Vrsalovic
+2  A: 

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.

Swaroop C H
+2  A: 

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.

luntain
+7  A: 

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

cheers,

Rob

Rob Wells
+3  A: 

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

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

Jagmal
NERDCommenter is better: it has more features, last version released at 30.03.2009 (21.02.2008 for enhancedcommentify and it still for vim6), and is available in Gentoo repository (but enhancedcommentify is available too).
ZyX
+6  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'.

Jonas Engman
+1 I use this all the time. Much simpler than the accepted solution.
Wim Coenen
A: 

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.

amrox
+1  A: 

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.

JP Lodine
A: 

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

shyam
A: 

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.

Dergachev
A: 

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.

Nathan Fellman
+1  A: 

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.

Jeremy Cantrell
What made you change from the first to the second?
Luc Hermitte
NERD seems to be more actively developed, but I actually like the behavior of EnhancedCommentify better.
Jeremy Cantrell
WIth a name like NERD... you got to figure someone is caring for it.
ojblass
A: 

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

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

Evgeny