3

I'm playing with ex and I've found that removing trailing spaces for multiple files in silent mode doesn't work as expected.

The following examples doesn't work as expected (whatever I use argdo or bufdo):

$ ex -s +'argdo! %s/\s\+$//e' -cxa *.*
$ ex -s +'bufdo! %s/\s\+$//e' -cxa *.*
(nothing is changed)

My files have trailing spaces as I've tested in git repository, so I could easily check the changes via git diff. And in addition exactly the same command (without -s) works as expected:

$ ex +'bufdo! %s/\s\+$//e' -cxa *.*
7 files to edit

What's more unexpected, silent mode works for other example such as:

$ ex -s +'bufdo! %s/foo/bar/e' -cxa *.*

What I'm doing wrong?

Debugging with -V10 doesn't give me much useful information I guess.

  • Sounds to me like its a bug. – jecxjo Apr 20 '15 at 19:22
  • 3
    vi -es +'bufdo! %s/\s\+$//e' -cxa ... works here – guido Apr 21 '15 at 4:59
4

What happens here is, that the bufdo and argdo command iterate over all buffers themselves. However without writing your changes, vim will refuse to load the next buffer, unless the 'hidden' option is set (which it is not by default) or the argument to the buf/arg-doc comment does write the change itself.

So this will work:

ex -s -c'bufdo! %s/\s\+$//e|w' -cx *

or alternatively using :set hidden:

ex --cmd ':set hidden' -s -c'bufdo! %s/\s\+$//e' -cxa *
  • And of course this is a non-issue if you put :set hidden in your startup files. (Are there people that really prefer nohidden?) – D. Ben Knoble Oct 22 '19 at 12:55
  • mostly people who are not bothering to customize vim (and don't have a .vimrc). BTW: it ex in silent mode does not read .vimrc file – Christian Brabandt Oct 22 '19 at 13:02
  • Ah, good to know. Based on :h .vimrc, the plugin files do get loaded by ex? I have set hidden in one of those, so I imagine it isn't a problem for me – D. Ben Knoble Oct 22 '19 at 13:04
  • More interestingly, I get an exit code of 1 when I use ex like this. – D. Ben Knoble Oct 22 '19 at 13:19
  • 1
    @kenorb well, apparently not (or perhaps it does load the new buffer, but discards the changes) in silent mode. – Christian Brabandt Oct 23 '19 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.