When I use the args command it opens the files:

:args `find . -name \*.html`

I would like to do an operation on the arglist without opening the files at all in a buffer, or either opening them one at a time, doing the operation and then closing them. For example:

:args `find . -name \*.html`
:argdo %s/replace/with this/g

to go through the files and do the replacement one at a time. I do not want to open the files in buffers. Also, due to memory constraints, I do not want to have all the files open at one time in any situation.

How can I accomplish this?

I would imagine, as D. Ben Knoble suggests, that if you don't anticipate a lot of interaction with the files, a different tool would be the way to go. find . -name \*.html -exec sed -i.bak 's/replace/withthis/g' {} + (or perl -i.bak -pe if your pattern is more complex than sed can accommodate).

If you are determined to do this in vim, I would probably use something like xargs -n to restrict the number of files loaded at once, e.g. find . -name \*.html | xargs -n4 vim +'argdo s/replace/withthis/g' will load only 4 at a time (it may warn you about STDIN not being a terminal but it still works for me.

Warning, this may become very messy if you decide to stop early: i.e. if you decide to stop at the second of 20 batches, you may have to :qall or ^C a whole 19 times. Telling xargs to stop in the middle does not appear to be a simple matter. (If somebody knows differently I'll update the answer.)

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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