4

This works fine:

augroup au_test | au!
  autocmd BufNew * if 1 | echom 123 | endif
augroup end

But this doesn't work:

com Test echom 123
augroup au_test | au!
  autocmd BufNew * if 1 | Test | endif
augroup end

It results in following error when i create new buffer:

E488: Trailing characters:  Test | endif

Why ?

This behavior was found by Samuel Jackson in this question

dedowsdi
  • 6,248
  • 1
  • 18
  • 31

1 Answers1

5

Vimscript (well, Ex) is weird in that each command will determine whether they'll take | as an argument or whether they'll allow it as a separator.

In the case of user-defined commands, they default to taking | as an argument, but you can override that by passing a -bar argument to the :command definition.

So, defining Test this way would have prevented the issue:

com -bar Test echom 123

See :help :bar for more details and a list of commands that take | as an argument:

These commands see the '|' as their argument, and can therefore not be followed by another Vim command:

  • :argdo
  • :autocmd
  • ...
  • a user defined command without the -bar argument

To be able to use another command anyway, use the :execute command.

See also :help :command, in particular the part that covers -bar.

filbranden
  • 28,785
  • 3
  • 26
  • 71