25

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?

Leon Timmermans
  • 30,029
  • 2
  • 61
  • 110
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • One addition to @zigdon's answer: you could use NERDCommenter plugin to comment text, so if you have visually selected text and this plugin installed then use `,cc` to comment the text. More mappings in `:h NERDComFunctionalitySummary`. – ZyX Jun 09 '10 at 16:59

16 Answers16

24

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.

zigdon
  • 14,573
  • 6
  • 35
  • 54
20

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 bya`.

user229044
  • 232,980
  • 40
  • 330
  • 338
Rob Wells
  • 36,220
  • 13
  • 81
  • 146
17

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

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
Jonas Engman
  • 550
  • 1
  • 7
  • 11
  • 1
    Note that the default vim installed with Ubuntu 14.04 (installed as `vi`) does not include the `visualextra` feature, so block insert won't work there. `sudo apt-get install vim` brings in a more full-featured version, on which `visualextra` is activated. You can check the output of `vim --version` to see the full list of activated features. – Gabriel Grant Jul 15 '15 at 12:50
8

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.

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
4

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
  • 16,902
  • 10
  • 43
  • 50
4

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
  • 4,560
  • 6
  • 37
  • 48
4

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.

Abhishek
  • 6,912
  • 14
  • 59
  • 85
amrox
  • 6,207
  • 3
  • 36
  • 57
3

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
  • 26,392
  • 13
  • 55
  • 78
3

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

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

Jagmal
  • 5,726
  • 9
  • 35
  • 35
  • 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 Jun 09 '10 at 17:17
2

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
  • 122,701
  • 101
  • 260
  • 319
2

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

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
fijter
  • 17,607
  • 2
  • 25
  • 28
1

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.

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
Dergachev
  • 348
  • 3
  • 6
1

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

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

Evgeny
  • 3,064
  • 2
  • 18
  • 19
1

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
  • 489
  • 5
  • 13
0

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
  • 330,807
  • 53
  • 334
  • 373
0

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
  • 9,134
  • 4
  • 29
  • 44