1

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.

4

1 回答 1

3

:silent单独是不够的。你需要使用:silent!. 来自:help :silent(强调我的):

添加 [!] 时,也会跳过错误消息,并且在检测到错误时不会中止命令和映射

于 2017-07-31T10:20:35.010 回答