为什么点 (.) 命令在 VIM 中如此有用?
我现在经常使用 VIM,但我从不使用点 (.) 命令来重复之前的操作。我一直在阅读它有多么棒,但我从未见过任何对我以及我在 VIM 中编码的方式有意义的现实世界示例。有哪些现实世界的示例可以展示点 (.) 命令的强大之处?
I use VIM pretty regularly now but I never use the dot (.) command to repeat the previous action. I keep reading about how awesome it is but I never see any real world examples that make sense to me and the way I code in VIM. What are some real world examples that show how awesome the dot (.) command is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我经常使用 dw.... 来删除一系列单词,而无需先在心里数数。在该示例中,
5dw
的字符数较少,但我认为使用点命令重复删除单词 比计数要快 3 毫秒。编辑 我刚才正在做一些编辑,并意识到还有另一种情况,我大量使用了点命令。我想在 Vim 中有一种更简单的方法来处理此类事情,但是点命令可以帮助解决以下和类似的情况。以下基本上是“我做了一次某事,现在我想再重复几次。”的示例,我正在编写一个新测试(用 C 语言),需要在其中嵌入一个字符串代表我从其他地方复制的 SQL 脚本的常量。原始的多行(7 行)脚本没有用双引号引起来,所以我这样做了:
I use
dw....
fairly often to delete a series of words without needing to mentally count them first. In that example,5dw
is fewer characters, but I think I am about 3ms faster using the dot command to just repeat thedelete word
over counting them.Edit I was just now doing some editing and realized there is another situation that I use the dot command a fair amount. I would imagine there is a much simpler way of handling this type of thing in Vim, but the dot command helps out with the following and similar situations. The following is basically an example of "I did something once, now I want to repeat it a few more times." I was writing a new test (in C) and needed to embed into it a string constant representing an SQL script that I copied from another place. The original multiline (7 line) script was not enclosed in double quotes, so I did this:
现在,将光标置于第一行:
A{}
。现在点击jj
并且Now, with the cursor in the first line:
A<bs><cr>{<cr><cr>}<cr><esc>
. Now hitj.j.
and所有其他答案都提供了很好的例子,我只想补充一点,点很棒,因为在某种程度上,它是您最后一个键组合的自动宏,距离只有一个键。
虽然宏很棒,但使用起来有点麻烦,而
dot
始终可用,即使功能较弱。All the other answers provides good examples, I just want to add that
dot
is great because in a way it is an automatic macro for your last key combination that is only one key away.While macro are great, they are a bit cumbersome to use, while the
dot
is always available, even if less powerful.与上一张海报一样,我经常在删除时使用它:
dw...
dd...
在多行上重复操作时:
iSomeText:[Esc ]jjj
Like the previous poster, I often use it when deleting:
dw...
dd...
And when repeating an action on multiple lines:
iSomeText:[Esc]j.j.j.
我最常见的例子是更改文本的缩进级别
或只是重新进行特定的文本更改,例如在多个变量前面插入
(char *)
:(或其他)
My most common examples are changing the indent level of text
or just re-doing a particular text change like inserting
(char *)
in front of several variables:(or whatever)
对我来说,点命令一开始是命中或错过的,直到我开始以任何频率记录宏。记录有用宏的技巧是将问题表示为可重复的步骤。有时,点是唯一可以做的事情,或者至少可以让问题变得更容易。
此外,使用点命令将迫使您使用以前可能不需要的某些命令,例如:cw ct; ci" ca),因为它们将文本的删除/更改折叠为一个可通过点重复的“操作”。
此外,除了点之外,还有 ; 我使用的频率要低得多,但当我这样做时,它非常有用,它会重复最后一个文本动作对于 w 和 b 等东西没有那么有用,但对于 f 等东西来说它是一个很好的小技巧。
For me the dot command was hit or miss at first until I started recording macros with any frequency. The trick to recording useful macros is to represent the problem as repeatable steps. Sometimes the dot is the only thing that will do or at least makes the problem much easier.
Also, using the dot command will force you to use certain commands that you may have not needed as much before such as: cw ct; ci" ca) since they collapse the delete/change of the text into one "action" that is repeatable by dot.
Also, in addition to dot there is also ; which I use much less frequently but when i do its very useful which repeats the last text motion. Not that useful for things such as w and b but for things like f; its a nice little trick.
当您需要将其转换
为:时,
您可以使用视觉块选择左侧的“位置”,并使用点作为右侧的“位置”。
When you need to convert this:
into this:
you'd use visual block selection for the left 'Position' and a dot for the right.
以下是我使用点命令执行的一些操作:
更简单:%s/\/replacement/gc
是单词上的*
,然后cereplacement
,然后重复n.
。如果您的单词出现两到三次,这很好。如果您想要替换多个单词,请转到下一个单词,点击*
,然后再次点击n。
jjj<.....
或在前面插入空格:dd
或dw
,点命令将删除另一行/单词点命令发生的一个神奇的事情是,如果它重复使用编号寄存器的命令,它会将使用下一个编号的寄存器(请参阅
:help redo-register
)。说明:如果您对 9 行执行了
dd
并希望按照删除它们的顺序恢复它们,则执行:"1P........"1P
将在光标之前插入最后删除的文本,"2P
将在之前插入。最后删除的文本等等。Here are some actions that I do with the dot command:
:%s/\<word\>/replacement/gc
is*
on the word, thencereplacement<esc>
and then repeatn.
. This is nice if you have two or three occurrences of your word. If you have several words that you want to replace, then go to the next word, hit*
and againn.
<Ctrl-V>jjj<.....
or insert spaces at the front:<ctrl-v>jjjI<space><esc>....
dd
ordw
, the dot command will delete another line/wordA magical thing that happens with the dot command is that if it repeats a command that used a numbered register, it will use the next numbered register (see
:help redo-register
).Explanation: If you did
dd
on 9 lines and want to restore them in the order in which you've deleted them, then do:"1P........
. Note that registers 1 to 9 are Vim's delete-ring."1P
will insert before the cursor the last deleted text,"2P
will then insert prior-to-last deleted text, and so on.