Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Ignoring errors in a list of mapped substitutions

Tags:

regex

vim

vi

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?

like image 696
kthy Avatar asked Jan 16 '12 12:01

kthy


2 Answers

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.

like image 59
romainl Avatar answered Nov 15 '22 08:11

romainl


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>
like image 22
Birei Avatar answered Nov 15 '22 07:11

Birei