1

I am trying to write a command (in vim rc) that automatically creates a new split and starts a terminal (i.e by executing :Term) and then automatically executing some bash commands on that terminal (specifically 'conda activate')

This is how far I got:

command CustomTerminal execute "vsplit ." <bar> execute "Term" 

How to I extended this command such that, it pipes and executes some bash commands on this terminal?

1 Answer 1

1

I see the following issues with your command definition:

  • you don't need :execute here; also <Bar> is only needed in mappings
  • the vsplit . creates a directory listing; is that intended?
  • have you defined a custom :Term command? The build-in command to open a terminal is :terminal
  • :term performs a (horizontal) split on its own; its :help :terminal mentions

If you want to split the window vertically, use:

    :vertical terminal

The :terminal command accepts optional shell commands already. If that's what you need, you can easily extend your custom command to take and pass this argument:

:command -nargs=? CustomTerminal vertical terminal <args>

See :help :command-nargs and :help <args>.

Additionally, you can add :help :command-completion via -complete=shellcmd.

Keeping the terminal and feeding commands to it

If you want to run more than one command and then close the terminal, so to reuse a single terminal session, you have to follow :help terminal-to-job to send commands from Vim to the terminal (received by the interactive shell or whatever application currently is running). It looks like this:

call term_sendkeys(buf, "ls *.java\<CR>")
Sign up to request clarification or add additional context in comments.

2 Comments

This doesnt work as, when the command gets executed then the terminal session is terminated. I want to execute a command (maybe multiple sequentially) on the terminal and then use it.
Well, running a single command and then closing the terminal (you can always open another one) would be easier, but it's also possible to send keys from Vim to the terminal; see my edit. If you want to stick with a single command, that would have to distinguish between no existing terminal and reusing an existing one.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.