如何在 Vim 中通过替换来重复命令?

发布于 2024-07-09 21:31:20 字数 234 浏览 12 评论 0原文

在 Unix 中,^ 允许您用一些文本替换新文本来重复命令。 例如:

csh% grep "stuff" file1 >> Results
grep "stuff" file1
csh% ^file1^file2^
grep "stuff" file2
csh%

有等效的 Vim 吗? 很多时候我发现自己一遍又一遍地在命令行上编辑小东西。

In Unix the ^ allows you to repeat a command with some text substituted for new text. For example:

csh% grep "stuff" file1 >> Results
grep "stuff" file1
csh% ^file1^file2^
grep "stuff" file2
csh%

Is there a Vim equivalent? There are a lot of times I find myself editing minor things on the command line over and over again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

最冷一天 2024-07-16 21:31:20

特别针对替换:使用 & 在正常模式下的当前行上重复上次替换

要对所有行重复,请输入 :%&

Specifically for subsitutions: use & to repeat your last substitution on the current line from normal mode.

To repeat for all lines, type :%&

生来就爱笑 2024-07-16 21:31:20

q: 进入命令行窗口 (:help cmdwin)。

您可以在此窗口中编辑和重复使用之前输入的前样式命令。

q: to enter the command-line window (:help cmdwin).

You can edit and reuse previously entered ex-style commands in this window.

<逆流佳人身旁 2024-07-16 21:31:20

一旦您点击 :,您就可以输入几个字符和向上箭头,它会与您输入的内容进行字符匹配。 例如,输入 :set ,它会爬回您的“集合”。 这也适用于搜索 - 只需键入 / 和向上箭头。 /abc 向上箭头会按逆时间顺序为您提供匹配的搜索字符串。

Once you hit :, you can type a couple characters and up-arrow, and it will character-match what you typed. e.g. type :set and it will climb back through your "sets". This also works for search - just type / and up-arrow. And /abc up-arrow will feed you matching search strings counterchronologically.

蓝海 2024-07-16 21:31:20

要在具有所有相同标志的所有行上重复前面的替换,您可以使用映射 g&

To repeat the previous substition on all lines with all of the same flags you can use the mapping g&.

烟凡古楼 2024-07-16 21:31:20

有两种方法。

  1. 您只需按 . 键即可精确重播最后一个命令(移动除外)。 例如,我输入 cw 然后输入 hello 将单词更改为“hello”。 将光标移动到另一个单词后,我点击 . 再次执行此操作。
  2. 对于更高级的命令,如替换,执行替换后,只需按 : 键,然后按 向上箭头键,它就会用相同的内容填充您的命令行命令。

There are 2 ways.

  1. You simply hit the . key to perform an exact replay of the very last command (other than movement). For example, I type cw then hello to change a word to "hello". After moving my cursor to a different word, I hit . to do it again.
  2. For more advanced commands like a replace, after you have performed the substition, simply hit the : key then the up arrow key, and it fills your command line with the same command.
不必了 2024-07-16 21:31:20

如果您在正常模式 :s/A/B/g(当前行)或可视模式 :'<,> 中进行了替换, 's/A/B/g(当前选择中包含的行)并且您想要重复上次替换,您可以:

  • 移动到另一行(正常模式)并只需按 & ;,或者如果您愿意,:-&-(看起来像 :&),影响当前行而不突出显示,或

  • 突出显示一个范围(可视模式)并按: -&- (看起来像 :'<,'>>&)影响行的范围选择。

由于我对 Vim 的了解有限,这解决了几个问题。 首先,最后一个视觉替换 :'<,'>s/A/B/g 可用作最后一个命令 (:-< UP>) 从正常模式和视觉模式,但总是在正常模式下产生错误。 (它仍然指的是视觉模式中的最后一个选择 - 而不是像我假设的那样指光标处的空选择 - 并且我的示例替换在一次传递中耗尽了每个匹配项。)同时,最后一个正常模式替换以 :s 开头,而不是 :'<,'>s,因此您需要修改它才能在可视模式下使用。 最后, & 可直接从正常模式使用,因此它接受重复和其他选择替代方案,例如接下来两行的 2& ,正如用户 ruohola 所说, g& 表示整个文件。

在这两个版本中,按 : 然后按 & 的效果就像按 : 然后重新输入 s/A/B/< /code>,因此您上次所处的模式无关紧要,只有当前光标行或选择决定受影响的行。 (请注意,像 g 这样的尾随标志也被清除,但在此语法中也出现在下一个,如 :&g/: '<,' >&g 在我看来,这是一个好坏参半的事情,因为您可以/必须在此处重新指定标志,而独立的 & 似乎根本不接受标志。 。我一定遗漏了一些东西。)

我欢迎建议和指正。 其中大部分来自刚刚的实验,所以我确信还有更多内容,但希望它能有所帮助。

If you have made a substitution in either normal mode :s/A/B/g (the current line) or visual mode :'<,>'s/A/B/g (lines included in the current selection) and you want to repeat that last substitution, you can:

  • Move to another line (normal mode) and simply press &, or if you like, :-&-<CR> (looks like :&), to affect the current line without highlighting, or

  • Highlight a range (visual mode) and press :-&-<CR> (looks like :'<,'>&) to affect the range of lines in the selection.

With my limited knowledge of Vim, this solves several problems. For one, the last visual substitution :'<,'>s/A/B/g is available as the last command (:-<UP>) from both normal and visual mode, but always produces an error from normal mode. (It still refers to the last selection from visual mode - not to the empty selection at the cursor like I assumed - and my example substitution exhausts every match in one pass.) Meanwhile, the last normal mode substitution starts with :s, not :'<,'>s, so you would need to modify it to use in visual mode. Finally, & is available directly from normal mode and so it accepts repetitions and other alternatives to selections, like 2& for the next two lines, and as user ruohola said, g& for the entire file.

In both versions, pressing : then & works as if you had pressed : and then retyped s/A/B/, so the mode you were in last time is irrelevant and only the current cursor line or selection determines the line(s) to be affected. (Note that the trailing flags like g are cleared too, but come next in this syntax too, as in :&g/: '<,'>&g. This is a mixed blessing in my opinion, as you can/must re-specify flags here, and standalone & doesn't seem to take flags at all. I must be missing something.)

I welcome suggestions and corrections. Most of this comes from experimentation just now so I'm sure there's a lot more to it, but hopefully it helps anyway.

○愚か者の日 2024-07-16 21:31:20

您可以输入 @: 重复上一个命令。

You can type @: to repeat the last command.

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