François Ingelrest wrote:
Hi,
As the title says, I want to automatically remove trailing spaces when
I save a file. This is what I put in my vimrc:
au BufWrite * %s/\s\+$//ge
This works correctly only when at least one match is found, an error
is issued if I want to save a file without trailing spaces. However,
using the 'e' flag for the substitute command should discard any such
error (at least it's working when I use this command interactively).
Did I miss something?
AFAIK, it ought to work, but there are workarounds:
Method I: Replace "zero or more whitespace, as many as possible", instead of
"one or more, as many as possible". Thus there will be a (possibly empty)
match on every line:
au BufWrite * 1,$s/\s*$//g
Method II: use the ":g" command to find which lines end in a whitespace
character:
au BufWrite * 1,$g/\s$/s/\s\+$//g
Caution: Either method might do things you don't want if ever you edit a
binary file. So, maybe:
au BufWritePre * if ! &bin | %s/\s*$//g | endif
Best regards,
Tony.
--
To be rich is not the end, but only a change of worries.