简体   繁体   English

如何在vim中以可视逐行模式为每行运行命令

[英]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). 我有以下CSS,我想在每行的开头(使用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 . 将光标放在第一行上,进入可视的逐行模式,然后选择3行V j j To comment I type I / / ESC . 发表评论我型 / / 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 "//". 另外,如果我使用可视的逐块模式来选择(即Ctrl-v j j )行,然后按I / / ESC,则将收到所有以“ //”开头的行的预期结果。

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. 如果您处于逐行可视模式,则可以使用normal来完成所需的操作。

:'<,'>norm I//

normal runs the command I// on every line in normal mode. normal在正常模式下在每行上运行命令I//

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. 您选择的那个取决于您计划对该选择进行的操作,但是它们的行为在执行可视模式命令时有所不同:由于Ex命令始终是逐行的,因此除了第一个命令外,它们都不关心可视模式的细节。行和所选内容的最后一行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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