1

I want to delete lines containing a search term from multiple files. I used bufdo or argdo for similar tasks such performing a replacement with s over multiple files. However, bufdo g/SearchString/d | update seems to delete the lines one by one and write the file after every deletion, same with the equivalent argdo command. As each file has thousands of the offending lines and is >100MB in size, this means the command would run for a few centuries. Is there a more efficient way?

5
  • I misunderstood the question. You are not trying to separate the file write(s) from the global command as I was thinking. I deleted my answer for now. The first thing you need to figure out is what is causing the error. You'll never get any further without doing since bufdo simply won't proceed on error. You should provide details of the error in your question.
    – B Layer
    Commented Oct 20, 2020 at 16:01
  • @BLayer I just found a working command based on your deleted answer: :bufdo exe "g/SearchString/d" | update. If you post an answer to that effect I'll accept it as it basically just moves the quotes around and I wouldn't have found that solution without your previous answer.
    – l4mpi
    Commented Oct 20, 2020 at 16:04
  • You know what? Though there are similarities that's kind of different from my answer. Why don't you write an answer yourself (and pick up some rep). I'll give you a vote. :)
    – B Layer
    Commented Oct 20, 2020 at 16:06
  • 1
    @BLayer done, feel free to edit if there's anything that can be improved!
    – l4mpi
    Commented Oct 20, 2020 at 16:13
  • I added a bit from :exe help. Otherwise looks good!
    – B Layer
    Commented Oct 20, 2020 at 16:47

1 Answer 1

3

With the original command, the bar would be interpreted as an argument to :g as described by :help :bar.

Per :h :exe...

":execute" can be used to append a command to commands that don't accept a '|'.

So from that we arrive at this:

:bufdo exe "g/SearchString/d" | update

This performs the deletion and writes the changed file only once As the :g command is quoted in the above variant, the bar is correctly parsed as an argument to :bufdo.

2
  • Yep. It's not the quotes that protect |, though; it's :execute (which takes a string as its only argument)
    – D. Ben Knoble
    Commented Oct 20, 2020 at 16:36
  • That's kinda just semantics, though. If the quotes weren't there it wouldn't work, after all. ;)
    – B Layer
    Commented Oct 20, 2020 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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