I have a number of regex substitutions I regularly do, and therefore want to map a key to do this easily. However, if one of the regexes to be substituted aren't found in the file, my key mapping stops due to E486: Pattern not found
and no further replacements are carried out. E.g. if the mapping in my vimrc is
map <F12> :%s/spam/foo/<enter>:%s/ham/bar/<enter>:%s/eggs/baz/<enter>
and there is no ham
in my file, eggs
won't get substituted. Is there a flag I can put on the substitutions to ignore errors? I've read :help regex
and :help map
without finding anything usable.
Alternately, is there another, easier, more obvious way to do this that I'm missing?
You can add a "flag" at the end of pattern:
:%s/spam/foo/e
won't spit an error if it doesn't find spam
and allow you to continue.
See :help flags
.
Add the /e
flag to each substitution command. It doesn't show error messages and continues as if no error had ocurred. This would be the new mapping:
map <F12> :%s/spam/foo/e<enter>:%s/ham/bar/e<enter>:%s/eggs/baz/e<enter>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With