Open a new tab in the running instance of Windows Terminal from CLI - windows-terminal

Is there a way to open a new tab in a running instance of Windows Terminal from CLI and make it point to a specific folder?
Every time I run wt.exe -d c:\my-folder it opens a new window.

Now, we have this option in Windows Terminal's Settings.

If someone needs the commands to create a new tab (and not open a new window) from a context menu item:
(if you are already in the directory):
wt -w 0 nt
(and if you click on the directory):
wt -w 0 nt -d %1
(to open in the current directory):
wt -w 0 nt -d %CD%
You can add these commands in the respective registry of each item (or in a new one)
Source: the GitHub discussion in the other answer.
if you run that in cmd or a script, it launches a new instance if none already running, or adds a tab if there is a running instance.

This is an outstanding issue that is going to get fixed eventually: Add support for wt.exe to run commands in an existing Terminal Window #4472

No, as per the documentation, calling wt.exe
...will always open a new window. Windows Terminal is not yet capable of opening new tabs or panes in an existing instance.

Related

Windows Terminal, how to save split pane layout?

Is there a way I can save current split panel layout and profile for each panel so I don't need to split and open profile one by one again?
This feature does not yet exist as part of Windows Terminal.
However, this feature was suggested, and the suggestion has been accepted and is being tracked in the Windows Terminal github issues backlog:
https://github.com/microsoft/terminal/issues/3759
Work-around:
You can call windows terminal (wt) from the command line and specify starting directories, splits, tabs, profiles, etc.
For example, run this from command line and it will open up 2 Windows Terminal tabs, the first tab will be a split layout with PowerShell profile + your default profile, and the other tab will use the Ubuntu WSL profile:
wt -d "C:\Windows\System32" -p "PowerShell"; sp -V -d "%USERPROFILE%"; nt -p "Ubuntu" --title "wsl" --tabColor "#FF0000"
Better yet, set this command as the file location for a shortcut on your desktop and you can just double click the shortcut to open your favorite Windows Terminal configuration.
See more wt command line options here:
https://learn.microsoft.com/en-us/windows/terminal/command-line-arguments?tabs=windows

Terminal window with running command

Is it possible to start a new window of terminal as in Windows: start example.bat? It opens a new window and runs an command.
You can do this using the open command, or ossascript. Here are some related questions using those commands in their answers:
Programmatically launch Terminal.app with a specified command (and custom colors)
Running a command in a new Mac OS X Terminal window
What arguments does the Mac's Terminal.app accept?
The open command is more straightforward, allowing you to specify the name of a script or command to be run, as noted in one answer. That describes this case (referring to open(1)):
open -a Terminal.app file
where file is the application to be opened. You may have to give its full pathname. The manual describes also a --args option which it states can be used to pass parameters to the application.

(ConEmu + Cygwin) How to change tab name of ConEmu from within a cygwin bash script

i've configuring my ConEmu + Cygwin enviroment. I've created a task, when i start it in a tab, the task will run a batch file, which in turn will change dir into cygwin and run
bash --login -i my_ssh_entry_script.sh
Inside my_ssh_entry_script.sh, it will read a config file in my home directory then print a menu for me to select which host to connect. And finally
...
exec ssh -p$port $userhost
Now i can work on the selected machine in the same conemu tab. This works fine. And the script also works on linux machine too.
But there is a little flaw. I can't change the tab's title. I've tried to change it to:
....
exec $(cygpath ${ConEmuDir})/ConEmu.exe /cmd ssh -p$port $userhost -cur_console:t:$title
But this will always create a new tab. Any suggestion to slove it? Thanks~
1) You need GuiMacro, thoroughly described in the project wiki:
http://conemu.github.io/en/GuiMacro.html
ConEmuC -GuiMacro Rename 0 "Title"
2) You do not need to run batch. It causes extra and useless cmd.exe in yours process tree. You can do all required "CD" and "SET" from the task content directly.
At least for cmd shell:
"-new_console:d:C:\Users\dir_name" cmd /V /K -new_console:t:Tab_renamed

Launching a website via windows commandline

I have a program launching a website via the following command.
cmd "start /max http://url.com"
When launching a website via this method it uses the default browser with its default settings for opening a new window. for example, Firefox and IE will open the window inside the tab of an existing window if they are set to do so. I have reports of IE 6 replacing the content of a current opened window with the content of url.com. I've tested this and sure enough when IE 6 is set as the default browser and with a current webpage opened the above will replace the content of the opened window with url.com rather than opening a fresh window.
Upon running some tests I see the command listed here:
cmd "start /max iexplore.exe http://url.com"
will consistently open a new window( with Internet Explorer of course) regardless of an existing window being present or not.
Can anyone tell me if I'm missing a silly setting in IE 6 or if there is a way to duplicate the "always open a new window" functionality exhibited by calling iexplore.exe directly, but with calling the user default browser instead.
You can just use
explorer "https://google.com"
Which will launch your default browser and navigate to that site.
And on Mac I've using
open "https://google.com"
To open a URL with the default browser, you can execute:
rundll32 url.dll,FileProtocolHandler https://www.google.com
I had issues with URL parameters with the other solutions. However, this one seemed to work correctly.
start chrome https://www.google.com/ or start firefox https://www.google.com/
This worked for me:
explorer <YOUR URL>
For example:
explorer "https://www.google.com/"
This will open https://www.google.com/ in your default browser.
IE has a setting, located in Tools / Internet options / Advanced / Browsing, called Reuse windows for launching shortcuts, which is checked by default. For IE versions that support tabbed browsing, this option is relevant only when tab browsing is turned off (in fact, IE9 Beta explicitly mentions this). However, since IE6 does not have tabbed browsing, this option does affect opening URLs through the shell (as in your example).
You can start web pages using command line in any browser typing this command
cd %your chrome directory%
start /max http://google.com
save it as bat and run it :)
Working from VaLo's answer:
cd %directory to browser%
%browser's name to main executable (firefox, chrome, opera, etc.)% https://www.google.com
start https://www.google.com doesn't seem to work (at least in my environment)
Ok, The Windows 10 BatchFile is done works just like I had hoped. First press the windows key and R. Type mmc and Enter. In File Add SnapIn>Got to a specific Website and add it to the list. Press OK in the tab, and on the left side console root menu double click your site. Once it opens Add it to favourites. That should place it in C:\Users\user\AppData\Roaming\Microsoft\StartMenu\Programs\Windows Administrative Tools. I made a shortcut of this to a folder on the desktop. Right click the Shortcut and view the properties. In the Shortcut tab of the Properties click advanced and check the Run as Administrator. The Start in Location is also on the Shortcuts Tab you can add that to your batch file if you need. The Batch I made is as follows
#echo off
title Manage SiteEnviro
color 0a
:Clock
cls
echo Date:%date% Time:%time%
pause
cls
c:\WINDOWS\System32\netstat
c:\WINDOWS\System32\netstat -an
goto Greeting
:Greeting
cls
echo Open ShellSite
pause
cls
goto Manage SiteEnviro
:Manage SiteEnviro
"C:\Users\user\AppData\Roaming\Microsoft\Start Menu\Programs\Administrative Tools\YourCustomSavedMMC.msc"
You need to make a shortcut when you save this as a bat file and in the properties>shortcuts>advanced enable administrator access, can also set a keybind there and change the icon if you like. I probably did not need :Clock. The netstat commands can change to setting a hosted network or anything you want including nothing. Can Canscade websites in 1 mmc console and have more than 1 favourite added into the batch file.
Using a CLI, the easiest way (cross-platform) I've found is to use the NPM package https://github.com/sindresorhus/open-cli
npm install --global open-cli
Installing it globally allows running something like open-cli https://unlyed.github.io/next-right-now/.
You can also install it locally (e.g: in a project) and run npx open-cli https://unlyed.github.io/next-right-now/
Or, using a NPM script (which is how I actually use it):
"doc:online": "open-cli https://unlyed.github.io/next-right-now/",
Running yarn doc:online will open the webpage, and this works on any platform (windows, mac, linux).

Mac OS X / Open terminal with specified windows

Is it possible to open a terminal window with 3 tabs. Each tab should have different path.
Example:
Tab1: /etc
Tab2: /bin
Tab3: /www/ tail -f file.txt
This is absolutely possible, but it will take some work on your part. The first thing you need is to set up each window/tab you want in your Settings:
I have 4 tabs that I open automagically every time I open Terminal. DB Shell, Editor, Server, and Shell. These are all within the Sasquatch (don't ask) project, thus the naming. Each of these should then have a unique command associated with them:
In this case, I'm executing vim. If you happen to have a specific directory you'd like to start off in, you can use something like vim ~/projects/main/. Really whatever you want to go in there is the command the shell will execute when it opens. Now you need to open all your windows/tabs:
Close everything.
Open a new window for each of your profiles.
Go to the Shell menu => New Tab/New Window => Select the profile you created above.
Repeat for each window or tab you want.
Once you have all of your windows and/or tabs open, save them as a Window Group.
Go to the Window menu => Save Window As Group....
Give your Window Group a name (this is helpful later).
If you want this group to open every time you open Terminal, check the box at the bottom of this screen, and then hit save.
Close out of all of your Windows/Tabs (time to test it!).
Open your new Window Group.
Go to the Window menu => Open Window Group => Select the group you just made.
This should pop up all the windows you just had, in the same position. Each of the commands you set up in Settings should be launched in their respective tabs.
As of Mac OS X Lion 10.7, Terminal makes this much easier to do, without creating new profiles for each command.
By default, Terminal will remember and restore the current working directory for each terminal in a Window Group. (If the working directory has been communicated to Terminal using an escape sequence. The default shell, bash, will do this at every command prompt. For other shells, you'll need to adapt the code in /etc/bashrc.)
If you create a terminal with Shell > New Command, Terminal will automatically run that command when a Window Group is opened. Terminal will automatically run a limited set of "safe" commands†, and when saving a Window Group there's an option to run all commands in the group.
Terminal also automatically does these for all windows when restarting Terminal with Resume enabled. So, you may not even have to create a Window Group, depending on your circumstances.
For your example case:
Use Shell > New Command to run "tail -f /www/file.txt".
Create a new tab and "cd /etc".
Create a new tab and "cd /bin".
Save them with Window > Save Windows as Group. Be sure to check the "Restore all commands" checkbox.
Each time you open that Window Group, it will recreate those windows and run the commands. If you need to run a command and specify the starting directory, in the New Command dialog check the "Run command inside a shell" checkbox and make the command "cd ; ".
Also note that you can tell Terminal to open your Window Group at startup with Terminal > Preferences > Startup > On startup, open > Window group. There's even a checkbox to set this when saving a new Window Group.
† The "safe" commands include anything listed in /etc/shells, plus: screen, tmux, emacs, vi/vim, nano, pico, and top. You can customize the list with "defaults write com.apple.Terminal RestorableCommands". Set it to an array of strings containing command names or full paths. Some commands have parameters that are "unsafe" to run automatically without user intervention, so by default these commands are only considered "safe" if they do not have any arguments. To make a command safe to run with arguments, add an asterisk, e.g., "top *" is in the default value for this preference.
You can do what you wish from within Terminal.
If in Terminal preferences (Settings tab) you create a new profile (or copy one with Duplicate Settings), you can then set each profile to run a command on startup (the "Shell" subgroup within the profile).
Then setup your tabs by using the Shell > New Tab menu to create the new tabs from each of the specific profiles that you created for the three different executables.
Then do the Save Window Group to save the group of tabs (and it will save their profiles as well).
I suggest the use if iTerm instead of Terminal. If only because it is more configurable. You can script it, but more important to you is that you can create a bookmark folder (one for each tab) and then "open in tabs" which will give you the behavior you seek.
I suspect you can control commands to be executed too. One of the programs I use creates a single-tab terminal window and arranges to execute a profile-setting script before continuing to the command prompt - the same should be feasible for a multi-tab terminal. The file is a MacOS X properties XML file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CommandString</key>
<string>. /Applications/IBM/informix/demo/server/profile_settings</string>
<key>FontAntialias</key>
<false/>
<key>RunCommandAsShell</key>
<false/>
<key>ShowShellCommandInTitle</key>
<true/>
<key>TerminalType</key>
<string>xterm</string>
<key>WindowTitle</key>
<string>IDS Command Window</string>
<key>name</key>
<string>IDS Command Window</string>
<key>type</key>
<string>Window Settings</string>
</dict>
</plist>
You can click on it and the terminal window is launched, the profile settings are set, and then you have a command prompt to type at. Presumably, changing the 'dot' command into the 'tail' command of the question would work; it might be that the 'RunCommandAsShell' key set to '<true\>' would replace the normal shell with the command - which is perhaps more appropriate for the question.
Another way of doing this is by using the Elscripto ruby gem: https://github.com/Achillefs/elscripto. It allows yuo to easily specify terminal tabs using a YAML file
Open the tabs you want and set each one up as you wish, i.e. in tab 1, cd /etc, tab 2 cd /bin and so on. Now go to Window > Save Windows as Group. Click the checkbox 'Use window group when Terminal starts' and hey presto!
http://www.iterm2.com/#/section/features/split_panes
"Divide a tab up into multiple panes, each one of which shows a different session. You can slice vertically and horizontally and create any number of panes in any imaginable arrangement."
How to Create Custom iTerm2 Window Arrangements
Create a custom keyboard shortcut to automatically spawn a set of windows and splits with processes running.
With a shortcut cmd+shift+w transform split your window arrangement into 3 panels
https://www.youtube.com/watch?v=Rg8AT-nds1Q

Resources