Windows Terminal: run CLI command on start up
I was recently interested by new Windows Terminal, so after testing it a bit, I wanted to have one profile which will instantly start development environment of my gatsby website. So I created another profile with config as follows:
"guid": "given unique guid",
"name": "Gatsby DEV",
"startingDirectory": "C:\\Projects\\gatsby-website\\",
"commandline": "gatsby develop"
Unfortunately, after choosing the profile from a menu, I can only see a tab blink, meaning it opens and closes immediately. When I added "closeOnExit": false
option, it didn't close, but the shown tab is empty.
Has anyone tried it already and succeeded?
Solution 1:
I use the following to start a Node.JS environment -
{
"guid": "{CBEAD37E-E945-4569-AAFA-091A9D3321C9}",
"name": "Node.js",
"commandline": "cmd /k \"F:\\Program Files\\nodejs\\nodevars.bat\"",
"colorScheme": "Vintage",
"hidden": false
},
The profile above spawns a CMD shell using the /k flag. /k instructs the shell to execute the command and then return to the cmd prompt allowing further input. In the case of the script above it spawns a shell; runs the batch file that initializes the environment variables and then returns you to a fully configured prompt.
If your tab is appearing and then disappearing immediately it is because the command you are running is providing a return code to announce that it has completed. By wrapping this in a command shell (with the /k flag) the return code is delivered to cmd and consumed rather than being passed to Windows Terminal which will instruct the tab to close.
Solution 2:
Here is a modified version of elaverick to run commands on Windows Terminal when starting up Powershell:
{
// Make changes here to the powershell.exe profile.
"guid": "<Your guid goes here>",
"name": "Custom Powershell",
"commandline": "powershell.exe -NoExit \"<Your command goes here>\"",
"hidden": false
},
Note the -NoExit
flag and the escaped quotes for JSON. Replace <Your command goes here>