0

I would like to execute multiple deletions. The problem when a pattern is not found all deletions are cancelled. I used the e flag to tell vim the not founding a pattern is not an error, but it still does not work.

The command looks like the following:

:g/pat1/de | g/pat2/de | ... | g/patn/de
1
  • When there are too many patterns, you can put them in a file and do :%!grep -vf patternfile
    – balki
    Aug 15, 2023 at 18:36

1 Answer 1

1

Please take a careful look at the syntax of :global: there's a pattern, and a command. We also know from :help :bar that everything after the pattern is the command, including |. Lastly, the :delete command can be abbreviated at any point up to :d, so :de is just the regular delete command.

Thus, your command really means "on each line with pat1, do delete (nested global) on each line with pat2, do ….

At :help :global we see

When the command is used recursively, it only works on one line.

You have a few options:

  • Separate the commands. This can be done with :execute, like :execute 'global/pat1/delete' | execute … (abbr.: :exe 'g/pat1/d' | exe …). Or type each command individually, hitting enter. The former might require a :silent modifier somewhere to avoid "Patter not found."
  • Combine the patterns: :global/pat1\|pat2\|…/delete. If you can use \v, I recommend it (then the separator becomes | instead of \|, but pay attention to other changes in patN).

Lastly, you can chain substitutions (though again I would combine patterns here):

:%substitute/.*pat1.*//e | %substitute/.*pat2.*//e | …

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.