tags:

views:

133

answers:

3

HI all!

I would like to insert a hash at the beginning of a selected block of text in VIM (ruby comment). I selected the lines in Visual Mode, but how do I perform the same operation to all lines?

Thank you in advance!

A: 

You better use this.

COMMAND MODE with set number to see lines

:10,50s/^/#/g

First number before comma is the start line and second number after comma is the end line. Both are included.

Dez
This isn't quite how you deal with a visual selection.
Jefromi
@Jefromi, yes, but it is faster in the execution of the desired result.
Dez
@Dez: Sure, if you already know the line numbers. What if the fastest way to specify the selection `nVn`, or `V}}}`? Why force yourself to look up line numbers? Just use `nVn:s/.../.../`.
Jefromi
+6  A: 

You have two primary options:

  • Select in block visual mode (ctrl-v), then use I to insert the same thing along the left side of the entire block. Similarly A appends; see blockwise operators.

  • Select the lines in normal visual (v) or visual line (V) mode, then run the same command on all of them, for example s/^/# / or normal I#. Typing : while you have a visual selection automatically uses the visual selection as the line range (denoted by '<,'>).

Jefromi
Block visual mode did not work for me, but normal did. In block visual, I selected the first char of every line, and it deleted all of them, not putting what I wanted in. The second worked though, thank you!
tesmar
@tesmar: I use that all the time; I know it works - you must've done some little thing wrong. `ctrl-v`, get the selection you want (first char is fine, like you did), `I# <Esc>`.
Jefromi
You are right, it worked once I realized I had to press ctrl + v. Thanks for the tip!
tesmar
+2  A: 

While in visual mode do the

:'<,'>s/^/#

actually, '<,'> will be inserted automatically when you hit :.

Michael Krelin - hacker