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?
1 Answer
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
.
-
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 LayerCommented Oct 20, 2020 at 16:42
bufdo
simply won't proceed on error. You should provide details of the error in your question.: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.:exe
help. Otherwise looks good!