Why the dot (.) command is so useful in VIM?

VimVi

Vim Problem Overview


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 Solutions


Solution 1 - Vim

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.

Solution 2 - Vim

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</kbd>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.

Solution 3 - Vim

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()
{ 

}

Solution 4 - Vim

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.

Solution 5 - Vim

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.

Solution 6 - Vim

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)

Solution 7 - Vim

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.

Solution 8 - Vim

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrhinoinreposeView Question on Stackoverflow
Solution 1 - VimBenoitView Answer on Stackoverflow
Solution 2 - VimMark WilkinsView Answer on Stackoverflow
Solution 3 - VimsidyllView Answer on Stackoverflow
Solution 4 - VimXavier T.View Answer on Stackoverflow
Solution 5 - VimpaulmelnikowView Answer on Stackoverflow
Solution 6 - Vimevil ottoView Answer on Stackoverflow
Solution 7 - VimNeg_EVView Answer on Stackoverflow
Solution 8 - VimAl.G.View Answer on Stackoverflow