vimviex

vim: Prevent :exe from quitting after error


I have an error-checking one-liner vim command that looks like the following. It's not directly part of the question but works as an example, so feel free to ignore it:

:'<,'>g/foo{.*}/exe "norm! mxf{lvt}y/\\(foo{\\)\\@!\<C-R>\"\<enter>yy'xP"

Here is an explanation:

This is basically an error-checking command to see that every time a term is wrapped by foo{, it is always wrapped by foo. However, exe exits on the first case where / (forward search) doesn't find anything. I don't want that to happen.

How do I make exeand :g continue even with errors inside the exe command? I have tried :silent, but that does not save it.

I'd rather keep this as a one-liner, but functions are a second option I am okay with.


Solution

  • :silent alone is not enough. You need to use :silent!. From :help :silent (emphasis mine):

    When [!] is added, error messages will also be skipped, and commands and mappings will not be aborted when an error is detected.