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

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

  • :argdo executes the rest of the command for each file in the arglist
  • %s/\s\+$//e removes trailing whitespace on every line in the current file
  • \s\+$ matches one or more whitespace characters at line end
  • The e flag suppresses errors in files where no match is found
  • | update writes 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 ... | update instead of :wall to avoid unnecessary writes
  • Add c to the substitute flags when you want per-match confirmation in each file

Next

How do I change the key that opens Vim's command-line window?