1

Is there any way I can concatenate a command after :terminal?

For example :terminal | <some_other_vim_command> will pass | <some_other_vim_command> to the terminal that just opened, but how can I prevent that from happening?

  • 1
    use :exe ":terminal" | another_vim_command – Christian Brabandt Apr 23 at 11:28
  • Thanks, that seems to work! – msh Apr 23 at 11:32
  • 1
    @ChristianBrabandt I've been looking for a duplicate but can't find one... We do have some questions on :command missing -bar but we don't seem to have one on needing :exe for bar. Also, Vim help :| has a list of commands needing :exe but :term is not on that list and probably should be... If you'd like to take care of these that would be awesome, otherwise I'll take a closer look at them tomorrow. Cheers! – filbranden Apr 23 at 12:09
  • 2
    Yeah, looks like this is missing from Vims :h :bar. Will post a doc patch later. – Christian Brabandt Apr 23 at 13:49
2

Not all Ex commands allow | to be used as a command separator, some Ex commands want to take | as an argument.

The common workaround is to use :execute to wrap those commands that do not allow | as a command separator.

See :help :bar or :help :| 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
  • ...
  • :normal
  • ...
  • :windo
  • :write !
  • :[range]!

Most Ex commands (such as :!) that take a shell command as an argument will not allow | as a command separator, since shell commands typically use | as a pipe between shell commands, so they'll pass the | literally to the shell.

Note that :terminal is not on that list (as of Vim 8.2.598) but that's just an omission in the documentation and @ChristianBrabandt offered to push a documentation patch to fix that in Vim.

The documentation in :help :| also mentions:

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

Example (append the output of "ls" and jump to the first line):

:execute 'r !ls' | '[

And :help :execute also mentions that type of usage:

:execute can be used to append a command to commands that don't accept a |.

Example:

:execute '!ls' | echo "theend"

In your particular case, you can use:

:exe "terminal" | <some_other_vim_command>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.