Can someone explain to me why the dot (.) command

2019-01-30 17:52发布

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?

标签: vim vi
8条回答
Deceive 欺骗
2楼-- · 2019-01-30 18:23

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)

查看更多
一夜七次
3楼-- · 2019-01-30 18:24

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.

查看更多
虎瘦雄心在
4楼-- · 2019-01-30 18:29

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.

查看更多
劳资没心,怎么记你
5楼-- · 2019-01-30 18:30
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()
{ 

}
查看更多
Melony?
6楼-- · 2019-01-30 18:33

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.
查看更多
冷血范
7楼-- · 2019-01-30 18:35

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.
查看更多
登录 后发表回答