Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

vi

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?

like image 389
rhinoinrepose Avatar asked Sep 06 '11 19:09

rhinoinrepose


People also ask

What does DOT do in Vim?

The dot command The dot command is a simple and powerful tool in Vim. It allows repeating the last change by pressing the . (period) key in Normal mode.

What is exclamation mark in Vim?

The ! qualifier tells Vim to force the operation. For example, if the file was read-only you would use :w! to write it anyway. If the file was modified and you wanted to quit without saving, you would use :q!. :wq! just means force write and quit in one command.

How do you repeat a command in Vim?

vim Normal mode commands (Editing) Repeat the Last Change Your cursor will be at position 1 of line 1, and all you need to do to fix the next two lines is press j. twice - that is, j to move down a line and . to repeat the last change, which was the addition of the I .


1 Answers

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.

like image 182
Benoit Avatar answered Oct 15 '22 07:10

Benoit