Sending commands to Terminal tabs through the command line #10365

Closed
opened 2026-01-31 02:19:36 +00:00 by claunia · 16 comments
Owner

Originally created by @Ariane-B on GitHub (Aug 28, 2020).

I'm aware that it's possible to send Windows Terminal itself commands such as "new tab" through the CLI command, as outlined in this documentation page.

I want to go further. I wish I could send each tab PowerShell/other commands to execute, such as npx webpack.

My use case is that my development environment involves launching a couple of CLI development server program type commands, and then having a couple more terminal tabs for occasional commands. I wish I could create a Windows shortcut doing all that that would auto-launch Windows Terminal and all my development toolkit by adding it to shell:startup.

Additionally, it would be nice if I could save that "startup bundle" and save it to my taskbar under the normal Windows Terminal icon for quick launch later on, too.

Originally created by @Ariane-B on GitHub (Aug 28, 2020). I'm aware that it's possible to send Windows Terminal itself commands such as "new tab" through the CLI command, as outlined in [this documentation page](https://docs.microsoft.com/en-us/windows/terminal/command-line-arguments). I want to go further. I wish I could send each tab PowerShell/other commands to execute, such as `npx webpack`. My use case is that my development environment involves launching a couple of CLI development server program type commands, and then having a couple more terminal tabs for occasional commands. I wish I could create a Windows shortcut doing all that that would auto-launch Windows Terminal and all my development toolkit by adding it to shell:startup. Additionally, it would be nice if I could save that "startup bundle" and save it to my taskbar under the normal Windows Terminal icon for quick launch later on, too.
claunia added the Issue-QuestionNeeds-Tag-FixResolution-Answered labels 2026-01-31 02:19:36 +00:00
Author
Owner

@zadjii-msft commented on GitHub (Aug 28, 2020):

Could you clarify a bit?

Do you want to just run specific commands in each tab when using the commandline? If you did, you could do something like

wt.exe new-tab ; new-tab npx webpack ; new-tab -p "Command Prompt" npx webpack

That'll open three tabs:

  • one with the default profile
  • one with the default profile, but with the commandline of npx webpack (instead of whatever the profile's commandline is)
  • the last one with your "Command Prompt" profile, but with the commandline of npx webpack (instead of whatever the "Command Prompt" profile's commandline is)

If you want to combine a bunch of these commands into a single entry in the new tab dropdown, I'd wait for #1571. That'll let you bind these commands into a single wt action in that menu.

Similar customization would land in the taskbar after #576

@zadjii-msft commented on GitHub (Aug 28, 2020): Could you clarify a bit? Do you want to just run specific commands in each tab when using the commandline? If you did, you could do something like ``` wt.exe new-tab ; new-tab npx webpack ; new-tab -p "Command Prompt" npx webpack ``` That'll open three tabs: - one with the default profile - one with the default profile, but with the commandline of `npx webpack` (instead of whatever the profile's `commandline` is) - the last one with your "Command Prompt" profile, but with the commandline of `npx webpack` (instead of whatever the "Command Prompt" profile's `commandline` is) <hr> If you want to combine a bunch of these commands into a single entry in the new tab dropdown, I'd wait for #1571. That'll let you bind these commands into a single `wt` action in that menu. Similar customization would land in the taskbar after #576
Author
Owner

@Ariane-B commented on GitHub (Aug 28, 2020):

Hi! Thanks a lot for your help.

I didn't think it was possible to send commands to each tab. (The documentation doesn't seem very explicit on that topic.)

Your comment made me investigate further, and it seems one of the problems was that I wasn't specifying my start directory for each tab properly. It needs to be a complete path, and I was using ~ as a shortcut to my user profile, which wasn't working.

So most tabs seem to work right now. There's just one that's not working, and I can't figure out why. Here's my full command:

wt -p "Windows PowerShell" -d C:\Users\arianeb\backend --title backend-srv docker-compose up `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\frontend --title frontend-srv npm run dev `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\backend --title backend `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\frontend --title frontend

The second one named "frontend-srv" gives me "[erreur 0x80070002 lors du lancement de `npm run dev']" (sorry, French OS), and then the tab is entirely unresponsive. not even Ctrl-C can save it.

There's also another issue: My command is too long for a Windows shortcut. I don't know what the limit is exactly, but when I try to paste it into the "Target" field of a shortcut's properties, it gets cut off. What do you think I should do about that?

@Ariane-B commented on GitHub (Aug 28, 2020): Hi! Thanks a lot for your help. I didn't think it was possible to send commands to each tab. (The documentation doesn't seem very explicit on that topic.) Your comment made me investigate further, and it seems one of the problems was that I wasn't specifying my start directory for each tab properly. It needs to be a complete path, and I was using ~ as a shortcut to my user profile, which wasn't working. So most tabs seem to work right now. There's just one that's not working, and I can't figure out why. Here's my full command: ```shell wt -p "Windows PowerShell" -d C:\Users\arianeb\backend --title backend-srv docker-compose up `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\frontend --title frontend-srv npm run dev `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\backend --title backend `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\frontend --title frontend ``` The second one named "frontend-srv" gives me "[erreur 0x80070002 lors du lancement de `npm run dev']" (sorry, French OS), and then the tab is entirely unresponsive. not even Ctrl-C can save it. There's also another issue: My command is too long for a Windows shortcut. I don't know what the limit is exactly, but when I try to paste it into the "Target" field of a shortcut's properties, it gets cut off. What do you think I should do about that?
Author
Owner

@KalleOlaviNiemitalo commented on GitHub (Aug 29, 2020):

Possible solutions for the overlong command line, from easier to harder:

  1. Save the command as a PowerShell script file and make the shortcut then launch PowerShell with it in a minimized window. It would cause a taskbar button to annoyingly appear and disappear but https://github.com/microsoft/terminal/issues/7335 could eventually fix that in future version of Windows.
  2. Write a GUI-subsystem wrapper program that just runs wt.exe with the correct arguments. Then make the shortcut point to that.
  3. Write a program that creates a shortcut with a longer command line than the property sheet allows. This would involve CLSID_ShellLink, IShellLinkW::SetPath, IShellLinkW::SetArguments, and IPersistFile::Save.
  4. Change Windows Terminal to make it support a @responsefile argument. This feature would perhaps go in wt.exe rather than WindowsTerminal.exe.
@KalleOlaviNiemitalo commented on GitHub (Aug 29, 2020): Possible solutions for the overlong command line, from easier to harder: 1. Save the command as a PowerShell script file and make the shortcut then launch PowerShell with it in a minimized window. It would cause a taskbar button to annoyingly appear and disappear but <https://github.com/microsoft/terminal/issues/7335> could eventually fix that in future version of Windows. 2. Write a GUI-subsystem wrapper program that just runs `wt.exe` with the correct arguments. Then make the shortcut point to that. 3. Write a program that creates a shortcut with a longer command line than the property sheet allows. This would involve `CLSID_ShellLink`, `IShellLinkW::SetPath`, `IShellLinkW::SetArguments`, and `IPersistFile::Save`. 4. Change Windows Terminal to make it support a `@responsefile` argument. This feature would perhaps go in `wt.exe` rather than `WindowsTerminal.exe`.
Author
Owner

@Ariane-B commented on GitHub (Aug 31, 2020):

Hi! Creating a .ps1 file with my command allows me to solve the shortcut command length limit. Thanks for the tip!

I still have a problem with my second tab giving me an error though.

@Ariane-B commented on GitHub (Aug 31, 2020): Hi! Creating a .ps1 file with my command allows me to solve the shortcut command length limit. Thanks for the tip! I still have a problem with my second tab giving me an error though.
Author
Owner

@DHowett commented on GitHub (Sep 3, 2020):

The second tab is producing an error because you need to provide the full path to npm. It looks like you are using some powershell automation that adds npm to your path, but Terminal cannot find it because it does not consult powershell before trying to run something.

If you run get-command npm | select -expand Path and use that path to npm, it should work fine. 😄

@DHowett commented on GitHub (Sep 3, 2020): The second tab is producing an error because you need to provide the full path to `npm`. It looks like you are using some powershell automation that adds `npm` to your path, but Terminal cannot find it because it does not consult powershell before trying to run something. If you run `get-command npm | select -expand Path` and use that path to npm, it should work fine. :smile:
Author
Owner

@Ariane-B commented on GitHub (Sep 3, 2020):

Oh, thanks! That'll work. Are you considering adding that functionality? Using programs that were added to the path with the wt command, I mean.

@Ariane-B commented on GitHub (Sep 3, 2020): Oh, thanks! That'll work. Are you considering adding that functionality? Using programs that were added to the path with the `wt` command, I mean.
Author
Owner

@jmoralesv commented on GitHub (Sep 27, 2020):

Hi! Thanks a lot for your help.

I didn't think it was possible to send commands to each tab. (The documentation doesn't seem very explicit on that topic.)

Your comment made me investigate further, and it seems one of the problems was that I wasn't specifying my start directory for each tab properly. It needs to be a complete path, and I was using ~ as a shortcut to my user profile, which wasn't working.

So most tabs seem to work right now. There's just one that's not working, and I can't figure out why. Here's my full command:

wt -p "Windows PowerShell" -d C:\Users\arianeb\backend --title backend-srv docker-compose up `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\frontend --title frontend-srv npm run dev `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\backend --title backend `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\frontend --title frontend

The second one named "frontend-srv" gives me "[erreur 0x80070002 lors du lancement de `npm run dev']" (sorry, French OS), and then the tab is entirely unresponsive. not even Ctrl-C can save it.

There's also another issue: My command is too long for a Windows shortcut. I don't know what the limit is exactly, but when I try to paste it into the "Target" field of a shortcut's properties, it gets cut off. What do you think I should do about that?

Is it necessary to always specify a directory, with the -d parameter, in order to run an app or command inside of each tab? I would like to avoid doing that as the command I want to run (in my case dotnet run) has the whole path of the file it will run already.

The documentation lacks this kind of advanced examples to run apps or commands in each new tab or new pane. It would be useful to have these in the docs.

@jmoralesv commented on GitHub (Sep 27, 2020): > Hi! Thanks a lot for your help. > > I didn't think it was possible to send commands to each tab. (The documentation doesn't seem very explicit on that topic.) > > Your comment made me investigate further, and it seems one of the problems was that I wasn't specifying my start directory for each tab properly. It needs to be a complete path, and I was using ~ as a shortcut to my user profile, which wasn't working. > > So most tabs seem to work right now. There's just one that's not working, and I can't figure out why. Here's my full command: > > ```shell > wt -p "Windows PowerShell" -d C:\Users\arianeb\backend --title backend-srv docker-compose up `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\frontend --title frontend-srv npm run dev `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\backend --title backend `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\frontend --title frontend > ``` > > The second one named "frontend-srv" gives me "[erreur 0x80070002 lors du lancement de `npm run dev']" (sorry, French OS), and then the tab is entirely unresponsive. not even Ctrl-C can save it. > > There's also another issue: My command is too long for a Windows shortcut. I don't know what the limit is exactly, but when I try to paste it into the "Target" field of a shortcut's properties, it gets cut off. What do you think I should do about that? Is it necessary to always specify a directory, with the `-d` parameter, in order to run an app or command inside of each tab? I would like to avoid doing that as the command I want to run (in my case `dotnet run`) has the whole path of the file it will run already. The documentation lacks this kind of advanced examples to run apps or commands in each new tab or new pane. It would be useful to have these in the docs.
Author
Owner

@Ariane-B commented on GitHub (Sep 27, 2020):

As far as I can tell, the -d parameter is entirely optional. But what folder tabs automatically open to and whether path variables are available there was really confusing, so I opted to mention the whole thing at length and tell it to run a shell, too, for good measure.

@Ariane-B commented on GitHub (Sep 27, 2020): As far as I can tell, the `-d` parameter is entirely optional. But what folder tabs automatically open to and whether path variables are available there was really confusing, so I opted to mention the whole thing at length and tell it to run a shell, too, for good measure.
Author
Owner

@dennohpeter commented on GitHub (Feb 10, 2021):

```shell
wt -p "Windows PowerShell" -d C:\Users\arianeb\backend --title backend-srv docker-compose up `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\frontend --title frontend-srv npm run dev `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\backend --title backend `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\frontend --title frontend

Did you get a solution for how to run command after launch?
e.g After running wt --title "backend-srv" ls `; I get the error below
Error

@dennohpeter commented on GitHub (Feb 10, 2021): > ```shell > ```shell > wt -p "Windows PowerShell" -d C:\Users\arianeb\backend --title backend-srv docker-compose up `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\frontend --title frontend-srv npm run dev `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\backend --title backend `; new-tab -p "Windows PowerShell" -d C:\Users\arianeb\frontend --title frontend > ``` > ``` Did you get a solution for how to run command after launch? e.g After running ```wt --title "backend-srv" ls `;``` I get the error below ![Error](https://user-images.githubusercontent.com/29105876/107540690-61ed1480-6bd7-11eb-8000-00de69e90ea1.JPG)
Author
Owner

@zadjii-msft commented on GitHub (Feb 10, 2021):

@dennohpeter the commandline has to be a valid Windows commandline. So if you want to run ls from your WSL distro, you'll need to do something like

wt --title backend -- wsl ls

That'll tell wsl.exe to run your commandline, which will start the ls in your default distro.

Though of course, that'll exit as soon as ls is done. You might try:

wt --title backend -- wsl ls\;$SHELL

(thanks stackoverflow)

That \;$SHELL will tell your shell to immediately start your configured $SHELL program as soon as ls exits. The \; is to get a ;, but escape it so wt doesn't parse the semicolon itself. It's not particularly elegant, but it works? You might not need that if the commandline is something practical.

@zadjii-msft commented on GitHub (Feb 10, 2021): @dennohpeter the commandline has to be a valid _Windows_ commandline. So if you want to run `ls` from your WSL distro, you'll need to do something like ``` wt --title backend -- wsl ls ``` That'll tell `wsl.exe` to run your commandline, which will start the `ls` in your default distro. Though of course, that'll exit as soon as `ls` is done. You might try: ``` wt --title backend -- wsl ls\;$SHELL ``` (thanks [stackoverflow](https://askubuntu.com/questions/20330/how-to-run-a-script-without-closing-the-terminal)) That `\;$SHELL` will tell your shell to immediately start your configured `$SHELL` program as soon as `ls` exits. The `\;` is to get a `;`, but escape it so `wt` doesn't parse the semicolon itself. It's not particularly elegant, but it works? You might not need that if the commandline is something practical.
Author
Owner

@dennohpeter commented on GitHub (Feb 11, 2021):

@zadjii-msft thank you very much for explanation and clarification. That works 🥂

@dennohpeter commented on GitHub (Feb 11, 2021): @zadjii-msft thank you very much for explanation and clarification. That works 🥂
Author
Owner

@mickdewald commented on GitHub (Sep 3, 2021):

I tried the command wt --title backend -- wsl ls\;$SHELL, but my terminal windows still keep closing. I use zsh inside of WSL. It that the problem?

@mickdewald commented on GitHub (Sep 3, 2021): I tried the command `wt --title backend -- wsl ls\;$SHELL`, but my terminal windows still keep closing. I use zsh inside of WSL. It that the problem?
Author
Owner

@Ariane-B commented on GitHub (Sep 3, 2021):

What are those two hyphens followed by a space? --wsl would look like a command line argument called "wsl", but -- wsl looks like an argument with no name whose value is "wsl", which confuses me a little.

@Ariane-B commented on GitHub (Sep 3, 2021): What are those two hyphens followed by a space? `--wsl` would look like a command line argument called "wsl", but `-- wsl` looks like an argument with no name whose value is "wsl", which confuses me a little.
Author
Owner

@DHowett commented on GitHub (Sep 3, 2021):

What are those two hyphens followed by a space? --wsl would look like a command line argument called "wsl", but -- wsl looks like an argument with no name whose value is "wsl", which confuses me a little.

There's a convention we found among Linux utilities (mostly, ones that use GNU getopt) where -- denotes the end of a list of arguments. They say, "Everything after this is a filename, or a command, or ...".

It makes it easier to delete a file called -rf: Obviously rm -rf would suck, but in rm -- -rf, the -- tells rm to stop processing arguments 😄

@DHowett commented on GitHub (Sep 3, 2021): > What are those two hyphens followed by a space? `--wsl` would look like a command line argument called "wsl", but `-- wsl` looks like an argument with no name whose value is "wsl", which confuses me a little. There's a convention we found among Linux utilities (mostly, ones that use GNU `getopt`) where `--` denotes the end of a list of arguments. They say, "Everything after this is a filename, or a command, or ...". It makes it easier to delete a file called `-rf`: Obviously `rm -rf` would suck, but in `rm -- -rf`, the `--` tells rm to stop processing arguments :smile:
Author
Owner

@Ariane-B commented on GitHub (Sep 3, 2021):

Oh, thanks for the info!

@Ariane-B commented on GitHub (Sep 3, 2021): Oh, thanks for the info!
Author
Owner

@mdkling commented on GitHub (Nov 9, 2021):

I tried the command wt --title backend -- wsl ls\;$SHELL, but my terminal windows still keep closing. I use zsh inside of WSL. It that the problem?

@micktg, you may need to tweak it depending on how/where you invoke it. If you're trying to invoke it from powershell, you may need to use --% instead of just -- before wsl to escape the rest of the line. (see: https://docs.microsoft.com/en-us/windows/terminal/command-line-arguments?tabs=linux#using-start)

Try this from powershell: wt --% wsl ls\;$SHELL

That works for me invoking from powershell with bash as my shell and Ubuntu as my default profile.
Below is a little more complicated example that should also work.

wt --% -M -w -1 wsl ls -lAh --color\;$SHELL `; sp -V wsl top\;$SHELL `; sp -H wsl nano\;SHELL

--% treats any subsequent text as input to wt, not powershell
-M starts the window maximized. Add -f to get rid of the title bar ("Focus Mode")
-w -1 forces a new window
sp creates a new split
-V and -H force those splits to be Vertical or Horizontal, respectively.

The end result should be a new, maximized window with ls, top, and nano running on 3 separate splits and top and nano should dump you to your default shell after exiting.

@mdkling commented on GitHub (Nov 9, 2021): > I tried the command `wt --title backend -- wsl ls\;$SHELL`, but my terminal windows still keep closing. I use zsh inside of WSL. It that the problem? @micktg, you may need to tweak it depending on how/where you invoke it. If you're trying to invoke it from powershell, you may need to use `--%` instead of just `--` before `wsl` to escape the rest of the line. (see: https://docs.microsoft.com/en-us/windows/terminal/command-line-arguments?tabs=linux#using-start) Try this from powershell: `wt --% wsl ls\;$SHELL` That works for me invoking from powershell with bash as my shell and Ubuntu as my default profile. Below is a little more complicated example that should also work. ``wt --% -M -w -1 wsl ls -lAh --color\;$SHELL `; sp -V wsl top\;$SHELL `; sp -H wsl nano\;SHELL`` `--%` treats any subsequent text as input to wt, not powershell `-M` starts the window maximized. Add `-f` to get rid of the title bar ("Focus Mode") `-w -1` forces a new window `sp` creates a new split `-V` and `-H` force those splits to be Vertical or Horizontal, respectively. The end result should be a new, maximized window with `ls`, `top`, and `nano` running on 3 separate splits and `top` and `nano` should dump you to your default shell after exiting.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#10365