Happy Thanksgiving! Get 40% off Practical Vim, 2nd edition (and all other titles from Pragmatic Bookshelf).

Learn more

Happy Thanksgiving! Get 40% off Practical Vim, 2nd edition (and all other titles from Pragmatic Bookshelf).

Using :argdo to change multiple files


Run time:

The :argdo command allows us to execute an Ex command across all buffers in the arglist. To demonstrate, we’ll use the example of running the :substitute command across multiple files, then we’ll see how to revert or save the changes. We’ll also compare the :argdo and :bufdo commands, and consider when it’s appropriate to use each one.


The argdo command works like this:

:argdo {cmd}

Where {cmd} could be any Ex command, such as :substitute, :global, or :normal for example. This is just like running:


The argdo command works best when the 'hidden' setting is enabled. For more information, see Vimcast episode 6 - Working with buffers.

Using argdo with substitute

The video demonstrates how to use argdo with the substitute comamnd. We start off simple:

:argdo %s/\a/*/g

There’s a problem with this command: if any of the buffers in the argument list do not contain a match for the pattern specified by the substitute command (in this case \a), then the substitue command will raise an error: E486 pattern not found. We can suppress this error by modifying the substitute command with the e flag (see :help :s_flags):

:argdo %s/\a/*/ge

That cuts down some of the noise, but the substitute command still reports details of about the effect it has on each file in the argument list. We can suppress that output by prefixing the whole thing with silent:

:silent argdo %s/\a/*/ge

Alternatively, we could use :silent!, which would suppress all output including error messages. That way, we could leave out the e flag:

:silent! argdo %s/\a/*/g

Pasting the last Ex command into the command line

On the command line, you can paste the contents of a register by pressing <C-r>{register}. In this context, the quote: register comes in handy: it contains the last executed Ex command. For example, if we start out by running a simple substitute command:


To prefix this with argdo, simply type :argdo, then a space, then <C-r>: which will produce:

:argdo %s/\a/*/g

After executing this command, the : register will contain argdo %s/\a/*/g. We could prefix this with silent by typing :silent, space, then <C-r>: to produce:

:silent argdo %s/\a/*/g

You get the idea?

:argdo Vs :bufdo

Just as we can use :argdo to execute a command on each of the files in the arglist, we can use :bufdo to execute a command on each of the files in the buffer list. In some circumstances, the buffer list and arglist may refer to the same set of files. In this case, it would make no difference whether you used :bufdo or :argdo.

It’s easy to add files to the buffer list (perhaps unintentionally), so if you use :bufdo there’s always a risk that it might end up touching a file that you didn’t mean to change. Whereas the arglist only changes when you tell it to. For that reason, I prefer to use :argdo instead of :bufdo.

Further reading


Level-up your Vim


Boost your productivity with a Vim training class. Join a public class, or book a private session for your team.

Drew hosted a private Vim session for the shopify team that was one of the best workshops I have ever attended.

John Duff, Director of Engineering at Shopify


Make yourself a faster and more efficient developer with the help of these publications, including Practical Vim (Pragmatic Bookshelf 2012), which has over 50 five-star reviews on Amazon.

After reading it, I've switched to vim as my default editor on a daily basis with no regrets. ★★★★★

Javier Collado

Learn to use Vim efficiently in your Ruby projects

In association with Thoughtbot, one of the most well respected Rails consultancies in the world, I've produced a series of screencasts on how to make navigating your Ruby projects with Vim ultra-efficient. Along the way, you’ll also learn how to make Ruby blocks a first-class text object in Vim. This lets you edit Ruby code at a higher level of abstraction. Available to buy from Thoughtbot..