DEV Community

Igor Irianto
Igor Irianto

Posted on • Edited on • Originally published at irian.to

Use Vim's dot command to do repetitive tasks faster

Follow @learnvim for more Vim tips and tricks!

Vim has been my go-to editor for the last 1.5 years, yet I am still learning something new. Recently I sought to learn about the vim's dot (.) command.

Dot command is like a mini macro. Is repeats the last change made. If used correctly, it can save us time doing repetitive tasks.

I am also curious how you guys use dot command - feel free to comment below!

How does it work?

If you see the help section in vim (:h .), you'll see:

Repeat last change, with count replaced with [count]...

I immediately thought, "what does vim mean by 'change'?"

After some reading and experimenting, I concluded change means any act of updating, adding, or subtracting the content of a file. Moving around does not count as a change. Let's see if that is true by some application.

Example 1: Adding ; to the end of each line

Here is an example (source). Let's say we want to add ; at the end of each line, this can be done with the help of .:

Roses are Red
Violets are Blue
Unexpected '{'
On line 32
Enter fullscreen mode Exit fullscreen mode

Assume we are starting on top left where 'R' is. We start with A ; <esc> j.

  1. A jumps to end of line and enters insert mode.
  2. ; adds ";", back to normal mode, then go down.

Cool, that whole (A ; <esc> j) was one change, right? No. If we do . . ., we end up with

Roses are Red;
Violets are Blue;;; <-- what happened?
Unexpected '{'
On line 32
Enter fullscreen mode Exit fullscreen mode

This is because vim does not count j as part of change. Change excludes motions. In this case, vim consider a change to be A ; <esc>. We need to do A ; <esc> j . j . j .. Dot, down, dot, down, dot, down.

Example 2: Deleting specific word, but not all

For example, suppose our poem says this instead:

Roses are Red Blue
Violets are Blue
Unexpected Blue '{'
On line Blue 32
Enter fullscreen mode Exit fullscreen mode

We need to delete Blues except the one on line two. We can very quickly do it using dot command.

/ Blue c i w <backspace> <esc> deletes first Blue. Then n n . n .

This time, our change consist of:

  1. Delete the entire word Blue and entering insert mode (c i w)
  2. Backspace while in insert mode
  3. Exit

I am starting to see a pattern here. / Blue and n are not considered change by vim, but c i w <Backspace> <esc> does.

Let's do another example:

Example 3: Adding ( at the beginning on each line

Another one, suppose you have:

One)
Two)
Three)
Enter fullscreen mode Exit fullscreen mode

We are adding ( at the start of each word. You can do I ( <esc> to apply the change to the first, then j . j .. Change here is I ( <esc>.

Comparing what 'changed'

Let's compare all of the repeatable changes from the past few examples:

  1. A ; <esc>
  2. c i w <Backspace> <Esc>
  3. I ( <esc>

Do you see a pattern? They all start with commands that put you into insert mode (c, A, and I are all command that results in entering Insert mode) and end with <esc>.

Another one I didn't mention was delete commands like dd. I can delete lines repeatedly by dd . . . .. Although dd does not enter insert mode, vim considers it as a change because it deletes an entire line. Remember, anything that adds, removes, or updates text is considered as change by vim.

Application

Above are some application of dot commands. It can save us a few keystrokes - a few keystrokes saved is time gained. Next time we are doing repetitive task, see if you can repeat it with the dot command.

Thanks for reading! I really appreciate you making it this far. Happy hacking!

Resources:

Discuss

I am interested to learn how other devs take advantage of the dot command. What other ways do you think dot commands can be used?

Top comments (8)

Collapse
 
chsanch profile image
Christian Sánchez

Also you can use a [count] with . so you can repeat the change [count] times. See :help . for more info.

Collapse
 
iggredible profile image
Igor Irianto

Oh yeah, I forgot about count. Good call!

Collapse
 
ferricoxide profile image
Thomas H Jones II

I'm more a fan of macros and key-bindings for more-common repetitive-tasks (all hail the power of the .exrc or .vimrc!). That said, . is great for ad hoc stuff.

Collapse
 
iggredible profile image
Igor Irianto

Oh yes, macros. I played with them a bit, but I am not proficient with them yet. Macros are next-level stuff. They are definitely on my list to cover, so stay tuned! But for quick-and-dirty repetitive tasks, . is your man!

Collapse
 
darkes profile image
Victor Darkes

This seems real useful! Interested in what kind of development you do so that Vim is your go to editor?

Collapse
 
ferricoxide profile image
Thomas H Jones II • Edited

There's several vim plugins you can use that can turn vim into a near-IDE. So, if your comfort is vi (e.g., if you're like me and have been using vi - and derivatives - since the late 80s), you can have your proverbial cake and eat it too.

And, yes, some IDEs offer the ability to emulate vi/vim syntax, but, sometimes there's holes in those emulations (particularly if you're someone that uses macros and/or key-bindings).

Collapse
 
iggredible profile image
Igor Irianto

Hey Victor! At work we use VsCode (we also have IntelliJ), but I personally prefer Vim as my personal editor. Even at work, I use IntelliJ and VsCode's vim plugin because I can go near mouse-free.

As for my own personal setup - this is by no means comprehensive - but there are several must-have features that I need to make it my own personal IDE/editor. These are

  • neovim - "better" vim.
  • tmux - to manage multiple windows/ panes.
  • fzf - to quickly search file in project
  • Ag - to quickly search string in project
  • neotree - tree file explorer

You can check out my vim dotfiles here.

Some youtube vids I found helpful (I am not associated with any of these):

youtube.com/watch?v=xZTkrB_tEoY
youtube.com/watch?v=0YoNrTQCrHg&t=...
youtube.com/watch?v=YD9aFIvlQYs

Collapse
 
geekyarthurs profile image
Mahesh C. Regmi

great one!