为什么点 (.) 命令在 VIM 中如此有用?

发布于 2024-12-03 03:43:05 字数 118 浏览 0 评论 0原文

我现在经常使用 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 技术交流群。

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

发布评论

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

评论(8

小情绪 2024-12-10 03:43:06

我经常使用 dw.... 来删除一系列单词,而无需先在心里数数。在该示例中,5dw 的字符数较少,但我认为使用点命令重复删除单词 比计数要快 3 毫秒。

编辑 我刚才正在做一些编辑,并意识到还有另一种情况,我大量使用了点命令。我想在 Vim 中有一种更简单的方法来处理此类事情,但是点命令可以帮助解决以下和类似的情况。以下基本上是“我做了一次某事,现在我想再重复几次。”的示例,我正在编写一个新测试(用 C 语言),需要在其中嵌入一个字符串代表我从其他地方复制的 SQL 脚本的常量。原始的多行(7 行)脚本没有用双引号引起来,所以我这样做了:

  • 将脚本直接粘贴到代码中(文件中的 7 行新行)。
  • 将光标定位在第一个新行上。
  • 空间空间空间空间esc 在当前行插入空格和左引号
  • j. 六次以添加左引号。
  • 重新定位到粘贴文本的第一行
  • A\n" esc 在当前行上放置换行符和结束引号。
  • 再次 j. 六次,将结束引号放在其余行上。

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 the delete 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:

  • Paste the script directly into the code (7 new lines in the file).
  • Position the cursor on the first new line.
  • Ispacespacespacespace"esc to insert spaces and an opening quote on the current line.
  • j. six times to add opening quote for each additional line.
  • Re-position to the first line of the pasted text.
  • A\n"esc to put a line feed character and closing quote on the current line.
  • j. six times again to put the closing quote on the remaining lines.
好菇凉咱不稀罕他 2024-12-10 03:43:06
do_something();
do_another();
third_option();

现在,将光标置于第一行:A{}。现在点击jj并且

do_something()
{ 

}

do_another()
{ 

}

third_option()
{ 

}
do_something();
do_another();
third_option();

Now, with the cursor in the first line: A<bs><cr>{<cr><cr>}<cr><esc>. Now hit j.j. and

do_something()
{ 

}

do_another()
{ 

}

third_option()
{ 

}
友谊不毕业 2024-12-10 03:43:06

所有其他答案都提供了很好的例子,我只想补充一点,点很棒,因为在某种程度上,它是您最后一个键组合的自动宏,距离只有一个键。

虽然宏很棒,但使用起来有点麻烦,而 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.

笑咖 2024-12-10 03:43:06

与上一张海报一样,我经常在删除时使用它:

  • 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.
伏妖词 2024-12-10 03:43:06

我最常见的例子是更改文本的缩进级别

>%...

或只是重新进行特定的文本更改,例如在多个变量前面插入 (char *) :(

i(char *)<Esc>jjjwwww.jjjbb.

或其他)

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:

i(char *)<Esc>jjjwwww.jjjbb.

(or whatever)

两仪 2024-12-10 03:43:06

对我来说,点命令一开始是命中或错过的,直到我开始以任何频率记录宏。记录有用宏的技巧是将问题表示为可重复的步骤。有时,点是唯一可以做的事情,或者至少可以让问题变得更容易。

此外,使用点命令将迫使您使用以前可能不需要的某些命令,例如: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.

对岸观火 2024-12-10 03:43:06

当您需要将其转换

instance Movable Position where
    (x,y) --> Left  = (x-1,y)
    (x,y) --> Right = (x+1,y)
    (x,y) --> Up    = (x,y-1)
    (x,y) --> Down  = (x,y+1)

为:时,

instance Movable Position where
    Position(x,y) --> Left  = Position(x-1,y)
    Position(x,y) --> Right = Position(x+1,y)
    Position(x,y) --> Up    = Position(x,y-1)
    Position(x,y) --> Down  = Position(x,y+1)

您可以使用视觉块选择左侧的“位置”,并使用点作为右侧的“位置”。

When you need to convert this:

instance Movable Position where
    (x,y) --> Left  = (x-1,y)
    (x,y) --> Right = (x+1,y)
    (x,y) --> Up    = (x,y-1)
    (x,y) --> Down  = (x,y+1)

into this:

instance Movable Position where
    Position(x,y) --> Left  = Position(x-1,y)
    Position(x,y) --> Right = Position(x+1,y)
    Position(x,y) --> Up    = Position(x,y-1)
    Position(x,y) --> Down  = Position(x,y+1)

you'd use visual block selection for the left 'Position' and a dot for the right.

攒一口袋星星 2024-12-10 03:43:05

以下是我使用点命令执行的一些操作:

  • 更简单:%s/\/replacement/gc 是单词上的 *,然后cereplacement,然后重复 n.。如果您的单词出现两到三次,这很好。如果您想要替换多个单词,请转到下一个单词,点击 *,然后再次点击 n。
  • 当我想要左对齐某些块时:jjj<..... 或在前面插入空格:jjjI....
  • dddw,点命令将删除另一行/单词

点命令发生的一个神奇的事情是,如果它重复使用编号寄存器的命令,它会将使用下一个编号的寄存器(请参阅:help redo-register)。

说明:如果您对 9 行执行了 dd 并希望按照删除它们的顺序恢复它们,则执行:"1P........"1P 将在光标之前插入最后删除的文本,"2P 将在之前插入。最后删除的文本等等。

Here are some actions that I do with the dot command:

  • Simpler than :%s/\<word\>/replacement/gc is * on the word, then cereplacement<esc> and then repeat n.. 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 again n.
  • When I want to left-align some blocks: <Ctrl-V>jjj<..... or insert spaces at the front: <ctrl-v>jjjI<space><esc>....
  • after dd or dw, the dot command will delete another line/word

A 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.

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