How do I perform the same operation across multiple files using the argument list?
Answer
:args **/*.js | argdo %s/old/new/ge | update
Explanation
The argument list (arglist) is Vim's mechanism for loading a set of files and running commands across all of them. Unlike the buffer list, the arglist is explicitly controlled by you, making it ideal for targeted batch operations like project-wide renames, formatting changes, or code migrations.
How it works
:args **/*.jspopulates the argument list with all.jsfiles found recursively in the current directory:argdo {cmd}executes{cmd}in every file in the argument list%s/old/new/geruns a substitution on each file — theeflag suppresses errors when a file has no matches| updatesaves each file only if it was modified
The full pipeline loads files, transforms them, and saves them in one command chain.
Example
Rename a CSS class across all HTML files:
:args **/*.html
:argdo %s/class="btn-primary"/class="btn-main"/ge | update
Add a "use strict"; line to the top of every JavaScript file:
:args **/*.js
:argdo 1s/^/"use strict";\r/ | update
Delete all console.log lines from your project:
:args **/*.ts
:argdo g/console\.log/d | update
Tips
- Use
:argswithout arguments to view the current argument list and see which file is active (marked with[]) - Navigate the arglist manually with
:next,:prev,:first, and:last - Use
:argadd *.cssto add files to the existing arglist without replacing it - Use
:argdelete *.test.jsto remove files from the arglist :argdosupports any Ex command::argdo normal @aruns a macro,:argdo g/TODO/pprints matching lines- Unlike
:bufdo, the arglist does not include every open buffer — only files you explicitly loaded with:args, giving you precise control - Always use
ein substitute flags (/ge) with:argdoto prevent the command from aborting on the first file with no matches - Prefer
:cfdoover:argdowhen you only want to touch files that actually contain matches (populate the quickfix list with:vimgrepfirst)