如何在 Vim 上重复某些操作特定时间?
在 Vim 中,我通常想有时重复某些系列的命令。比如说,我想注释 5 行,我会使用
I//<Esc>j
.j.j.j.j
有没有办法多次重复最后一个“.j”部分?
In Vim, I usually want to repeat some series of commands some times. Say, I want to comment 5 lines, I would use
I//<Esc>j
.j.j.j.j
Is there any way to repeat the last ".j" part several times?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
执行此操作的一种方法是将按键序列分配给宏,然后运行该宏一次,然后运行
@@
run-last-macro 命令。例如:如果您知道要重复宏多少次,则可以使用
4@@
或其他。One way to do this is to assign your key sequence to a macro, then run the macro once followed by the
@@
run-last-macro command. For example:If you know how many times you want to repeat the macro, you can use
4@@
or whatever.您可以直观地选择要重复的行,输入
:normal! .
让 vim 在每一行使用.
。因为您从视觉选择开始,所以它最终看起来像这样:但是,如果您大量添加和删除
//
注释,您可能会发现以下映射很有用:您可以使用
5K
注释 5 行,您可以使用视觉模式先选择您的行,或者您可以直接敲击K
直到注释完您想要的所有内容。You can visually select the lines you want to repeat it on, type
:normal! .
to make vim use.
on each line. Because you started with a visual selection, it ends up looking like this:However, if you're adding and removing
//
comments alot, you might find the following mappings useful:You can use
5K
to comment 5 lines, you can use visual mode to select your lines first, or you can just hammerK
until you've commented everything you want.关于您的具体示例,我更喜欢使用可视块模式(使用
Ctrl-v
访问)进行多行插入。例如,如果我有以下几行:我将转到顶行的顶部第一个字符,按
Ctrl-v
进入可视块模式,导航到最后一行(可能使用3j
向下移动 3 行,也许使用4g
直接转到第 4 行,或者可能只是使用G
转到末尾),然后输入I//
一次在所有行上插入注释:此外,还有一个非常方便的注释器/取消注释器插件,支持多种语言 此处。这比手动插入/删除注释更容易。
Regarding your specific example, I prefer to do multiple-line insertion using visual block mode (accessed with
Ctrl-v
). For example, if I had the following lines:I'd go to the top first character in the top line, hit
Ctrl-v
to enter visual block mode, navigate to last line (maybe using3j
to move down 3 lines, maybe using4g
to go directly to 4th line, or maybe simplyG
to go the end), then typeI// <esc>
to insert the comments on all the lines at once:Also, there's a very handy commenter/un-commenter plugin that supports many languages here. It's easier than manually inserting/removing comments.
试试这个:
做某事
退出到正常模式
键入,例如,
22。
最后的命令将重复 22 次。
Try this:
Do something
Exit to normal mode
Type, for example,
22.
The last commands will repeats 22 times.
您可以通过在宏之前附加计数来重复宏。例如,如果您将宏记录到
a
寄存器,并且想要执行它五次,则可以输入以下内容:You can repeat a macro by appending a count before the macro. For example, if you recorded a macro to the
a
register and you wanted to perform it five times, you would type this:对于你的具体例子。您还可以使用范围
.,.5s#^#//#
(执行此操作以及接下来的 5 行)或可视块(按 v,然后选择所需的文本),然后使用<代码>:%s#^#//#。For your particular example. you could also use a range
.,.5s#^#//#
(to do this and the next 5 lines) or a visual block (hit v, then select the text you want) followed by:%s#^#//#
.另一种方法是设置标记并在该范围内运行替换:
Another way to do it is to set marks and run substitutions over that range: