Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sending commands to Terminal tabs through the command line #7452

Closed
Ariane-B opened this issue Aug 28, 2020 · 16 comments
Closed

Sending commands to Terminal tabs through the command line #7452

Ariane-B opened this issue Aug 28, 2020 · 16 comments
Labels
Issue-Question For questions or discussion Needs-Tag-Fix Doesn't match tag requirements Resolution-Answered Related to questions that have been answered

Comments

@Ariane-B
Copy link

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.

@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Aug 28, 2020
@zadjii-msft
Copy link
Member

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 zadjii-msft added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Aug 28, 2020
@Ariane-B
Copy link
Author

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?

@ghost ghost added Needs-Attention The core contributors need to come back around and look at this ASAP. and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Aug 28, 2020
@KalleOlaviNiemitalo
Copy link

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 Allow console allocation policies that are finer-grained than the PE image subsystem #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.

@Ariane-B
Copy link
Author

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.

@DHowett
Copy link
Member

DHowett commented Sep 2, 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 DHowett closed this as completed Sep 2, 2020
@DHowett DHowett added Issue-Question For questions or discussion Resolution-Answered Related to questions that have been answered and removed Needs-Attention The core contributors need to come back around and look at this ASAP. Needs-Tag-Fix Doesn't match tag requirements Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Sep 2, 2020
@ghost ghost added the Needs-Tag-Fix Doesn't match tag requirements label Sep 2, 2020
@Ariane-B
Copy link
Author

Ariane-B commented 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.

@jmoralesv
Copy link

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

@Ariane-B
Copy link
Author

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.

@dennohpeter
Copy link

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

@zadjii-msft
Copy link
Member

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

@dennohpeter
Copy link

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

@mickdewald
Copy link

mickdewald commented 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?

@Ariane-B
Copy link
Author

Ariane-B commented 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.

@DHowett
Copy link
Member

DHowett commented 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 😄

@Ariane-B
Copy link
Author

Ariane-B commented Sep 3, 2021

Oh, thanks for the info!

@mdkling
Copy link

mdkling commented 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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Question For questions or discussion Needs-Tag-Fix Doesn't match tag requirements Resolution-Answered Related to questions that have been answered
Projects
None yet
Development

No branches or pull requests

8 participants