Brad
Brad

Reputation: 109

How to run command for every line in visual linewise mode in vim

I have the following CSS that I want to comment out with "//" at the beginning of each line (using Sass).

a:focus {                                                                                                                  
    outline: thin dotted;                                                                                           
}

With my cursor on the first line I enter visual linewise mode and select 3 lines Vjj. To comment I type I//ESC. What I expect to happen is all lines have the text "//" prepended but instead only the first line has been modified.

Alternatively if I use visual blockwise mode to select (i.e. Ctrl-vjj) the lines and press I//ESC I receive the expected result of all lines prepended with "//".

My assumption has been that linewise and blockwise modes were merely different ways to select text. If I wanted to select all text of multiples lines the selection commands were interchangeable as long as I was able to select the text to modify. But the behavior above leads me to believe there's a difference I don't yet understand.

Is it possible to use visual linewise mode to accomplish this task or is it just the wrong tool for the job? Also documentation on the differences between the two modes would be greatly appreciated.

Upvotes: 0

Views: 2080

Answers (3)

romainl
romainl

Reputation: 196466

Character-wise, line-wise and block-wise visual modes all allow you to select text across multiple lines.

Character-wise visual mode is mainly useful when you don't care about or don't want to deal with "lines".

Line-wise visual mode is to be used when dealing with whole lines is important.

Block-wise visual mode is a convenient way to repeat a change across multiple similar lines. I like to see it like "a window inside the window" that allows me to act on a subset of my current buffer.

The one you choose is dictated by what you plan to do with that selection but their behavior differs only when doing visual mode commands: because Ex commands are always line-wise, they don't care about the specifics of your visual mode beyond the first line and the last line of the selection.

Upvotes: 4

Dhruva Sagar
Dhruva Sagar

Reputation: 7307

I would prefer to use visual mode and invoke :'<,'>s#^#//#

Upvotes: 1

FDinoff
FDinoff

Reputation: 31419

If you are in linewise visual mode you can use normal to accomplish what you want.

:'<,'>norm I//

normal runs the command I// on every line in normal mode.

Upvotes: 6

Related Questions