Windows terminal: open multiple panes and execute specified command
Asked Answered
K

2

19

I recently downloaded the new Windows Terminal. I have created the shortcut for opening the multiple panes(which is working fine). However, I am trying to execute a command for the respective pane.

wt -d <path> -c "cls && php artisan serve" ; 
   split-pane -p "Command Prompt" -H -d <path> -c "npm run watch"

I googled for the solution but no luck.

Is this even possible..?

Kass answered 19/6, 2020 at 10:13 Comment(1)
Some interesting ideas here.Moreta
M
13

I got a similar setup working. Im running Windows Terminal version 1.8.1444.0

My goal was to setup a dotnet core app running in left pane and a react-app running in right pane:

wt -d "C:\path\to\dotnetcoreapp" -p "Command Promt" cmd /k dotnet watch run ; split-pane -d "C:\path\to\reactapp" cmd /k yarn start

Also tried to start an interactive elixir session: wt -d "C:\dev\elixir" cmd /k IEx which also worked fine...

Murderous answered 3/6, 2021 at 15:51 Comment(0)
E
8

The short answer is: Yes it is possible but it is a workaround.

The Challenges

  1. wt.exe does not currently have a command line option to execute a command from a split-pane
  2. wsl.exe (which runs your default shell such as bash) does not currently support opening a shell with a command without exiting the shell right after the command is run.

The workaround

To get around the first challenge we can launch a custom profile that executes the command via wsl.exe in the key value pair (in settings json) "commandline": "wsl.exe 'commands go here"

To get around the second challenge we need to execute the wsl.exe 'commands go here' via powershell.exe because Powershell has a -NoExit option which will keep the shell open after the command is executed. So for example if you wanted to open a shell that runs wsl.exe (your linux shell) with the command watch ps then the line in the custom profile would look like this:

"commandline": "powershell.exe -NoExit -Command wsl.exe watch ps"

The Solution:

Create a profile in Windows Terminal settings.json for each command you want to run. Each profile should have a unique guid that you can generate in Powershell by running the command [guid]::NewGuid(). So the profile to run the command watch ps would look something like this:

{
    "guid": "{b7041a85-5613-43c0-be35-92d19002404f}",
    "name": "watch_ps",
    "commandline": "powershell.exe -NoExit -Command wsl.exe watch ps",
    "hidden": false,
    "colorScheme": "One Half Dark"
},

Now you can open a tab in windows terminal with two panes, the pane on the right will run the command watch ps and the shell will stay open. Put something like the below line of code in your shortcut (or from the command line) where the value of the option -p is equal to the value of the profile you created. Each additional pane you open will need a profile that has the command you want to run in it.

wt split-pane -p "watch_ps"

Eugenol answered 19/6, 2020 at 18:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.