如何在 Vim 上重复某些操作特定时间?

发布于 2024-09-09 16:57:35 字数 128 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

电影里的梦 2024-09-16 16:57:35

执行此操作的一种方法是将按键序列分配给宏,然后运行该宏一次,然后运行 ​​@@ run-last-macro 命令。例如:

qa.jq@a@@

如果您知道要重复宏多少次,则可以使用 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:

qa.jq@a@@

If you know how many times you want to repeat the macro, you can use 4@@ or whatever.

紫竹語嫣☆ 2024-09-16 16:57:35

您可以直观地选择要重复的行,输入 :normal! . 让 vim 在每一行使用 . 。因为您从视觉选择开始,所以它最终看起来像这样:

:'<,'>normal! .

但是,如果您大量添加和删除 // 注释,您可能会发现以下映射很有用:

" add // comment with K
noremap K :s,^\(//\)\=,//,e <BAR> nohls<CR>j
" remove // comment with CTRL+K
noremap <C-K> :s,^//,,e <BAR> nohls<CR>j

您可以使用 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:

:'<,'>normal! .

However, if you're adding and removing // comments alot, you might find the following mappings useful:

" add // comment with K
noremap K :s,^\(//\)\=,//,e <BAR> nohls<CR>j
" remove // comment with CTRL+K
noremap <C-K> :s,^//,,e <BAR> nohls<CR>j

You can use 5K to comment 5 lines, you can use visual mode to select your lines first, or you can just hammer K until you've commented everything you want.

月牙弯弯 2024-09-16 16:57:35

关于您的具体示例,我更喜欢使用可视块模式(使用 Ctrl-v 访问)进行多行插入。例如,如果我有以下几行:

This should be a comment.
So should this.
This is definitely a comment.
Is this a comment? Yes.

我将转到顶行的顶部第一个字符,按 Ctrl-v 进入可视块模式,导航到最后一行(可能使用 3j 向下移动 3 行,也许使用 4g 直接转到第 4 行,或者可能只是使用 G 转到末尾),然后输入 I// 一次在所有行上插入注释:

// This should be a comment.
// So should this.
// This is definitely a comment.
// Is this a comment? Yes.

此外,还有一个非常方便的注释器/取消注释器插件,支持多种语言 此处。这比手动插入/删除注释更容易。

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:

This should be a comment.
So should this.
This is definitely a comment.
Is this a comment? Yes.

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 using 3j to move down 3 lines, maybe using 4g to go directly to 4th line, or maybe simply G to go the end), then type I// <esc> to insert the comments on all the lines at once:

// This should be a comment.
// So should this.
// This is definitely a comment.
// Is this a comment? Yes.

Also, there's a very handy commenter/un-commenter plugin that supports many languages here. It's easier than manually inserting/removing comments.

淡水深流 2024-09-16 16:57:35

试试这个:

  1. 做某事

  2. 退出到正常模式

  3. 键入,例如,22。

最后的命令将重复 22 次。

Try this:

  1. Do something

  2. Exit to normal mode

  3. Type, for example, 22.

The last commands will repeats 22 times.

仙女山的月亮 2024-09-16 16:57:35

您可以通过在宏之前附加计数来重复宏。例如,如果您将宏记录到 a 寄存器,并且想要执行它五次,则可以输入以下内容:

5@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:

5@a
沉溺在你眼里的海 2024-09-16 16:57:35

对于你的具体例子。您还可以使用范围 .,.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#^#//#.

木緿 2024-09-16 16:57:35

另一种方法是设置标记并在该范围内运行替换:

ma
jjjj
mb
:'a,'bs,^,// ,

Another way to do it is to set marks and run substitutions over that range:

ma
jjjj
mb
:'a,'bs,^,// ,
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文