简体   繁体   中英

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 V j j . 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 (ie Ctrl-v j j ) 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.

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.

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.

我更喜欢使用可视模式并调用:'<,'>s#^#//#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM