How do I trim trailing whitespace across all files in the arglist?
Answer
:argdo %s/\s\+$//e | update
Explanation
When you need to clean up many files at once, :argdo lets you run the same command on every buffer in your argument list. Pairing it with a substitution and update gives you a safe batch workflow for whitespace cleanup without writing unchanged files. This is especially useful before commits, formatting passes, or lint runs in large repos.
How it works
:argdoexecutes the rest of the command for each file in the arglist%s/\s\+$//eremoves trailing whitespace on every line in the current file\s\+$matches one or more whitespace characters at line end- The
eflag suppresses errors in files where no match is found | updatewrites only if the current buffer changed
Example
Suppose your arglist contains several files and one has trailing spaces:
alpha = 1
beta = 2
After running:
:argdo %s/\s\+$//e | update
That file becomes:
alpha = 1
beta = 2
Tips
- Build the arglist first, for example with
:args src/**/*.lua - Use
:argdo ... | updateinstead of:wallto avoid unnecessary writes - Add
cto the substitute flags when you want per-match confirmation in each file