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.

Miles
  • 780
  • 7
  • 19
  • You don't seem to even need `:execute` in the first place. – romainl Jul 29 '17 at 16:19
  • Thanks @romainl, do you know how I could do this without `:exe`? I am lost... – Miles Jul 29 '17 at 17:05
  • 1
    Just `normal! ...` – romainl Jul 29 '17 at 18:30
  • Thanks. Unfortunately, that won't work for me because it's hard to type some control keys through an ssh connection (so `:exe` is useful for me to type "\"). I tested the normal mode one just now on my local machine and it does work, though, so thanks: `:'<,'>norm! mxf{lvt}"ay/\(foo{\)\@!^R"a^Myy'xP` - the `^R` and `^M` are `` and ``, respectively. – Miles Jul 29 '17 at 19:41

1 Answers1

3

: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.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324