Search code examples
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:

  • :'<,'>g/foo{.*}/ - Run the following command on all highlighted lines with foo{...}
  • exe "norm! - Start executing normal mode commands on each line
  • mx - Record the current line
  • f{lvt}y - Copy everything inside the curly braces of foo{...}
  • /\\(foo{\\)\\@!\<C-R>\"\<enter> - Forward search to an instance where the string inside the curly braces of foo{...} is not inside foo
  • yy'xP" - Copy that line, go back to x, and paste it above.

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.