0

Let's say I run the following command on a node.js file inside Vim:

:terminal node %"

enter image description here

I'd like to continue executing commands on the newly opened window with a single command. But if I try executing :terminal node % | w! /tmp/file | e /tmp/file it doesn't work. Even though running first :terminal node % and then :w! /tmp/file | e /tmp/file manually it works.

Inside vimscript if I try using:

:terminal node %
:w! /tmp/file | e /tmp/file

It also doesn't work. What am I missing? How can execute commands on the new window opened by the terminal command?

1
  • What kind of commands do you want to execute on that window, exactly? If you only want a buffer with the output of your node script, then you don't need :terminal at all and certainly not a temporary file. Just do :vnew|:read !node #. See this gist for a more elaborate solution.
    – romainl
    Commented Dec 8 at 10:15

1 Answer 1

1

The command :terminal includes | in its arguments (so that you can run a pipeline). See :help :terminal:

            No Vim command can follow, any | is included in
            [command].  Use `:execute` if you must have a Vim
            command following in the same line.

The standard trick around this is to instead

:execute 'terminal node %' | write! /tmp/file | edit /tmp/file
2
  • Even though it executes using the command like that it seems that it doesn't wait for the execution of terminal node % to finish to start executing the next commands. I've realized that using execute 'terminal node %' | sleep 1 | w! /tmp/file | e /tmp/file works filne for a simple console.log('Hello World') example. But not all codes will finish after 1 second. Is there any way of starting write! /tmp/file only after terminal node % finished its execution?
    – raylight
    Commented Apr 11, 2022 at 15:15
  • 1
    @raylight you get more programmatic control if you use the term_start or job_start functions
    – D. Ben Knoble
    Commented Apr 11, 2022 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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