Arglist allows you to perform the same operation on multiple files, without having to have them preloaded in buffers first.
Arglist provides a few commands, as follows:
- :arg defines the arglist.
- :argdo allows you to execute a command on all the files in the arglist.
- :args displays the list of files in the arglist.
For example, if we wanted to replace all instances of animal in every Python file (recursively), we would do the following:
:arg **/*.py
:argdo %s/<animal>/creature/ge | update
This command work as follows:
- :arg <pattern> adds a set of files matching a pattern to the arglist (each argument in arglist also has a corresponding buffer).
- **/*.py is a wildcard for every .py file, recursively starting with the current directory.
- :argdo executes a command on every item in the argument list.
-
%s/<animal>/creature/ge replaces every occurrence of animal with creature, in every file, without raising errors if the matches are not found.
:update is equivalent to :write, but it only saves the file if the buffer has been modified.
You need to use :update in arglist commands, because Vim doesn't like when you switch buffers without saving their contents. An alternative would be to use :set hidden to silence these warnings, and save all files at the end by running :wa.
Give it a shot, and you'll see that every occurrence of a word has been replaced (you can check by running git status or git diff if you have a repository checked into Git). You can also view the contents of the arglist by running the following without any arguments:
:args
Arglist is actually left over from the Vi days—it was used in a similar way to how we use buffers today. Buffers expand upon an argument list: every arglist entry is in the buffer list, but not every buffer is in the arglist.
Technically, you can also use :bufdo to perform an operation on every open buffer (since arglist entries are reflected in the buffer list). However, I would advise against it, since there is a risk of running a command on buffers you unintentionally had open before populating the argument list.