Why the dot (.) command is so useful in 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()
{
}
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.
Here are some actions that I do with the dot command:
- Simpler than
:%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.
- When I want to left-align some blocks:
<Ctrl-V>jjj<.....
or insert spaces at the front:<ctrl-v>jjjI<space><esc>....
- after
dd
ordw
, 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.
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.