0

Given the following files with two trailing spaces at the end:

$ echo "test1  " > test1.txt; echo "test2  " > test2.txt; echo "test3  " > test3.txt
$ hexdump -C *.txt
00000000  74 65 73 74 31 20 20 0a  74 65 73 74 32 20 20 0a  |test1  .test2  .|
00000010  74 65 73 74 33 20 20 0a                           |test3  .|
00000018

I'm trying to use argdo to remove trailing spaces, but it seems it applies the changes only to the last file.

So basically I do:

$ vim *.txt

Then in Vim I'm checking:

:args
[test1.txt] test2.txt test3.txt
:argdo! %s/\s\+$//e
"test2.txt" 1 line, 8 characters
"test3.txt" 1 line, 8 characters
:args
test1.txt test2.txt [test3.txt]
:xa " To save all changed buffers and exit.

However only the last file has been modified:

$ hexdump -C *.txt
00000000  74 65 73 74 31 20 20 0a  74 65 73 74 32 20 20 0a  |test1  .test2  .|
00000010  74 65 73 74 33 0a                                 |test3.|

As far as I can read in the help (:help :argdo), it:

Execute {cmd} for each file in the argument list

I believe it works with bufdo, but why it doesn't work with argdo?

Any reason why it doesn't work as expected and how to fix it?

  • works for me. Try using vim -N -u NONE – Christian Brabandt Oct 25 '15 at 16:10
  • @ChristianBrabandt Tried (vim -N -u NONE *.txt), but it seems it's still the same. Tested with Vim v7.3 (Linux) & v7.4 (OS X). – kenorb Oct 25 '15 at 16:49
  • I think I know the problem, will post as an answer – Christian Brabandt Oct 25 '15 at 16:58
3

When using :argdo Vim executes the command and then loads the next buffer. By default, it will therefore unload the current buffer before moving to the next buffer and if the current buffer is modified, it will abort the changes (without the '!' attribute, the argdo will then simply fail).

This means that after your :argdo command, only the third buffer will be marked as modified and this is the only buffer that will be written to disk (simply because the changes for the other buffers have been aborted). You can confirm this, by using :ls and see that only the last argument buffer is marked as modified with "+".

So you should either expliticly write your changes as part of the :argdo command or set the 'hidden' option or possibly set any of the autowrite and autowriteall option to prevent this from happening.

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.