Vim's :*do Family of Commands

by Noah Frederick |

Vim has a class of commands that execute their arguments in the context of each of a collection of entities, including tab pages, windows, buffers, items in the arg list, etc. Here is a summary:

Command Applies to
:tabdo Tab pages
:windo Windows in the current tab
:bufdo Buffers
:argdo Files in the arg list
:cdo Entries in the quickfix list
:cfdo Files in the quickfix list
:ldo Entries in the location list
:lfdo Files in the location list

Each of these commands takes another command as its argument. For example, to replace the string “Emacs” with “Vim” in all buffers:

:bufdo %s/Emacs/Vim/g

Commands can be combined with | to perform multiple actions, so we can also write the changes from the substitution with :update in the same invocation:

:bufdo %s/Emacs/Vim/g | update

Finally, the :*do commands take a range to restrict the items iterated over. To apply a substitution only to the first three entries of the quickfix list:

:1,3cdo s/Emacs/Vim/g

Further Reading

:help :tabdo :help :windo :help :bufdo :help :argdo :help :cdo :help :cfdo :help :ldo :help :lfdo :help arglist :help :bar