I have WSL2 and an Ubuntu instance, is there a way in windows terminal to open multiple tabs from the command line and run things in them ?
To be more detailed I would like to do the following, so please advise if there is a way:
- open windows terminal manually(click on it). Here I already set that Ubuntu is my default
- In my Ubuntu tab, I want to have a shell script which I can run
- The shell script should open some n number of tabs as Ubuntu
- Then in each of the new tabs automatically run a particular ssh (login to remote machine) in each of them.
- bonus set a color for each of the tabs that I just opened
I currently do all of this manually, and would like to automate it somehow.
I say this post which had some indicators and promise that this can happen, but I couldn't figure out how to do that.
2 Answers
Note, the following requires Windows 11. For Windows 10, use cmd.exe /c to call wt.exe per @AnarKi's answer. See footnote at the bottom of this answer for the reason why.
Using the long-form of each option for clarity, that shell script should typically be:
#!/usr/bin/env sh
wt.exe --window 0 new-tab --profile "Ubuntu" --title "host1" --tabColor "#F00" ssh host1
wt.exe --window 0 new-tab --profile "Ubuntu" --title "host2" --tabColor "#0F0" ssh host2
wt.exe --window 0 new-tab --profile "Ubuntu" --title "host3" --tabColor "#00F" ssh host3However, you may find that either:
- Something in your Windows Terminal's Ubuntu profile commandline
- Or something in your Ubuntu shell startup scripts
- Or (while I didn't see this problem), as the article you linked mentions, the starting directory directory
... is getting in the way of this being as "clean" as it should be. In that case, I recommend setting up a new Windows Terminal profile that will launch Ubuntu with just /bin/sh.
- Create a new profile in Windows Terminal with a Command line option of:
wsl.exe ~ -e "/bin/sh" - Name the profile something like "Ubuntu /bin/sh"
Then use:
#!/usr/bin/env sh
wt.exe --window 0 new-tab --profile "Ubuntu /bin/sh" --title "host1" --tabColor "#F00" ssh host1
wt.exe --window 0 new-tab --profile "Ubuntu /bin/sh" --title "host2" --tabColor "#0F0" ssh host2
wt.exe --window 0 new-tab --profile "Ubuntu /bin/sh" --title "host3" --tabColor "#00F" ssh host3Footnote: Windows Terminal's wt.exe is what is known as an "App Execution Alias". It's essentially an .exe that is just designed to launch a Microsoft Store (a.k.a. "UWP", "Universal", "Metro", and "Modern") app, rather than a "real" Windows binary.
Under Windows 10, WSL cannot run app execution aliases, as they aren't "real" executables that are handled by the WSL binfmt_misc implementation.
However, under Windows 11, this appears to have been changed, and app execution aliases are properly handled in WSL.
Just a theory, but this may have been added to handle the addition of WSL itself to the Microsoft Store (currently in Preview under Windows 11). This will allow the wsl.exe command, which is probably now an App Execution Alias (when installed from the Store) to still function within WSL itself.
This solution will work if you are starting from a linux profile on windows-terminal and want to use a script to run multiple ssh tabs within the same window that can be easily identified by tab color:
#!/bin/sh
cmd.exe /c "wt.exe" --window 0 new-tab --tabColor "#a64d79" ssh user@remote1
cmd.exe /c "wt.exe" --window 0 new-tab --tabColor "#3d85c6" ssh user@remote2
cmd.exe /c "wt.exe" --window 0 new-tab --tabColor "#f1c232" ssh user@remote3The details:
Going to the main source for windows-terminal commands here: . I figured that:
- You always need to use
cmd.exe /cif you are running on a linux profile. - The SSH session will automatically change the title of the tab after logging in, so no point setting the
--titleoption. (maybe there is a way to do it? couldn't solve this) - You cannot just run ssh from a linux profile. So do not set the profile to for e.g.
--profile Ubuntu-20.04(maybe there is a way to do it? couldn't solve this)
FYI an even better solution in case you want to have these ssh sessions just saved and to open them manually in tabs is to create new profiles for them in the JSON configuration of windows-terminal like so:
"profiles":
{ "defaults": { "acrylicOpacity": 0.2, "bellStyle": "none", "useAcrylic": true }, "list": [ { "commandline": "ssh user@remote1", "guid": "{xxxxxxxxx}", "hidden": false, "name": "some_name", "tabColor": "#f1c232", "icon": "C:\\some\\dir\\some_icon.png" }, { ... } ]you can generate GUIDs here for e.g.