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

How can I print only arglist files that still contain trailing whitespace before bulk cleanup?

Answer

:argdo if search('\s\+$', 'nw') | echo expand('%') | endif

Explanation

Before running destructive cleanup across many files, it helps to know which files will actually change. This pattern walks the current argument list and prints only filenames that still match trailing whitespace. It is useful when you want a quick audit pass before deciding whether to run a replacement command globally.

How it works

  • :argdo executes the following Ex command once per file in the argument list
  • search('\s\+$', 'nw') checks for trailing whitespace without moving the cursor (n) and without wrapping (w is omitted)
  • if ... | echo expand('%') | endif prints the current file path only when a match is found

Because this approach is read-only, you can use it as a safety step in larger refactors. Pair it with quickfix or your shell history to document exactly which files need cleanup.

Example

Given an arglist with three files:

a.py  (has trailing spaces)
b.py  (clean)
c.py  (has trailing spaces)

Running the command prints:

a.py
c.py

Tips

  • Follow with :argdo %s/\s\+$//e | update when you are ready to apply fixes
  • Build the arglist first (for example, :args **/*.py) so the audit is scoped

Next

How do I allow block selections past end-of-line while still permitting one-char past EOL cursor movement?