Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows terminal: open multiple panes and execute specified command

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..?

like image 433
MrSingh Avatar asked Jun 19 '20 10:06

MrSingh


People also ask

How do I open multiple tabs in Windows Terminal?

If you'd like to open a new pane through the dropdown menu, you can hold alt and click on your desired profile. Both of these options will auto split the active window or pane into a new pane of the selected profile.

Is Windows Terminal the same as Command Prompt?

Windows Terminal is a modern host application for the command-line shells you already love, like Command Prompt, PowerShell, and bash (via Windows Subsystem for Linux (WSL)).

How do I open a terminal window from the command line?

You can use wt.exe to open a new instance of Windows Terminal from the command line. You can also use the execution alias wt instead. If you built Windows Terminal from the source code on GitHub, you can open that build using wtd.exe or wtd .

How do I pass a command line argument in Windows?

option. You can test command line arguments by running an executable from the "Command Prompt" in XP, Vista or later, or from the "DOS prompt" in older versions of Windows. You can also use command line arguments in program shortcuts, or when running an application by using Start -> Run.


2 Answers

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...

like image 98
Jonas Jerndin Avatar answered Sep 23 '22 19:09

Jonas Jerndin


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"

like image 35
apena Avatar answered Sep 21 '22 19:09

apena