vimtricks.wiki Concise Vim tricks, one at a time.

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 **/*.js populates the argument list with all .js files found recursively in the current directory
  • :argdo {cmd} executes {cmd} in every file in the argument list
  • %s/old/new/ge runs a substitution on each file — the e flag suppresses errors when a file has no matches
  • | update saves 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 :args without 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 *.css to add files to the existing arglist without replacing it
  • Use :argdelete *.test.js to remove files from the arglist
  • :argdo supports any Ex command: :argdo normal @a runs a macro, :argdo g/TODO/p prints 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 e in substitute flags (/ge) with :argdo to prevent the command from aborting on the first file with no matches
  • Prefer :cfdo over :argdo when you only want to touch files that actually contain matches (populate the quickfix list with :vimgrep first)

Next

How do I edit multiple lines at once using multiple cursors in Vim?