How to set a duplicate tab keybinding in Windows Terminal - windows-terminal

I tried to follow the Microsoft Docs instructions to set a keybinding for opening a duplicate tab with the following line { "command": "duplicateTab", "keys": "ctrl+shift+d" }, but, when I press the key combination, it opens a new tab without duplicating the position (the folder where I am) of the current tab. Here's my json file:
"keybindings":
[
...
//This makes a copy of the current tab and opens it.
{ "command": "duplicateTab", "keys": "ctrl+shift+d" }
]

I've written up a longer version of the answer to this over at https://github.com/MicrosoftDocs/terminal/pull/449. To keep it short:
Typically, the "new tab" and "split pane" actions will always open a new tab/pane in whatever the startingDirectory is for that profile. However, on other platforms, it's common for new tabs to automatically use the working directory of the current tab as the starting directory for a new tab. This allows the user to quickly multitask in a single directory.
Unfortunately, on Windows, it's tricky to determine what the current working directory ("CWD") for a process is. Even if we were able to look it up, not all applications actually set their CWD as they navigate. Notably, Windows PowerShell doesn't change its CWD as you cd around the file system! Duplicating the CWD of PowerShell automatically would almost always be wrong.
Fortunately, there's a workaround. Applications can emit a special escape sequence to manually tell the Terminal what the CWD should be.
For example, for cmd.exe:
cmd uses the %PROMPT% environment variable to configure the prompt. You can easily prepend the prompt with the command to set the CWD with the following command:
set PROMPT=$e]9;9;$P$e\%PROMPT%
This will append $e]9;9;$P$e\ to your current prompt. When cmd evaluates this prompt, it'll replace
the $e with the escape character
the $p with the current working directory

Related

How to change the terminal to the current directory in visual studio code ? (hotkey) [duplicate]

This question already has answers here:
How to quickly change shell folder to match the currently open file
(5 answers)
Closed 3 years ago.
In class we had a hotkey for using the terminal with the currently choosen directory. I fixed an issue now with the debugger and everything seems to run smoothly now. Yet, what hotkey fixes this issue?
With VSCode 1.39 (Sept. 2019), no more plugin needed.
You now can "Open new terminals with custom working directories"
There is a new command that allows the creation of terminals with a custom current working directory (cwd):
{
"key": "cmd+shift+c",
"command": "workbench.action.terminal.newWithCwd",
"args": {
"cwd": "${fileDirname}"
}
}
You can create your own keyboard shortcuts to open new terminals in any number of handy working directories.
The cwd value can either be a normal path or a variable.
For a hotkey to quickly set your terminal folder to your current directory, see How to quickly change shell folder to match the currently open file
{
"key": "alt+t",
"command": "workbench.action.terminal.sendSequence",
"args": {"text": "cd '${fileDirname}'\u000D"}
},
This will change your current terminal, not open a new terminal - if you want to do that see the link above as well for the new command recently added to vscode.
Note that on windows, you must use the following instead:
"args": {"text": "cd /d \"${fileDirname}\"\u000D"}
This is because on Windows, the /d parameter must be used with cd to switch drives.
By default, the terminal will open at the folder that is opened in the Explorer. The hotkey for that depends on your operating system, in my case, on macOS, it's ⌃ + `, on Windows I think it's ctrl + j.
If you want to open at a specific folder you can change that behaviour with this setting:
{
"terminal.integrated.cwd": "/home/user"
}
If you want to open at the current file's directory you can install an extension like Terminal Here.
You can change the shortcuts/hotkeys for many actions on Code. For that open the Keyboard Shortcuts editor under File > Preferences > Keyboard Shortcuts and search for Toggle Integrated Terminal.

How can I open command prompt in the current directory as a shortcut

I'm just wondering how I can make a shortcut that will open command prompt in the folder it is currently in.
Why I need this.
I save my c program file in the directory Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\
So I would have to type cd Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\ every time I want to open the command prompt in that directory.
Is there a shortcut I can make where I can move that shortcut around anywhere (don't need to change the shortcut) and it will open command prompt in the directory it is current in, so I don't need to keep typing cd .....
you can go to the directory and just type start . in the same directory
or you can make a batch file like test.bat and add below line in the same:
cd Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\
Save it anywhere and whenever you will run that batchfile, it will automatically move PWD to the required directory
Right-click in the open space of your desktop and click New > Shortcut
For the location, type or copy and paste one of the following lines.
%windir%\system32\cmd.exe /k cd Users\PC\Documents\Other\My_Programs\WIP\Project_1\Test\
As commented by aschipfl, you can already do it, (may be OS dependent)!
In Windows Explorer, or I suppose the desktop, just hold down the shift key, right click in a blank area of it and choose Open command window here, (or whatever your locale states).
I think in Windows 10 at least, the option is even available in the Explorer toolbar menu too.

How to select a different type of command shell in the Visual Studio Code integrated terminal?

I looked at this link which talks about changing the default command shell in the integrated terminal of Visual Studio Code. I was able to change it to Git Bash using the below setting in the settings.json file. You can open settings.json file by pressing the Ctrl + , or from File → Preferences → Settings menu:
{
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"
}
There is a + sign in the integrated terminal with the help of which you can have multiple terminals running parallelly as shown in the screenshot below:
I want to load a Git Bash shell in terminal # 1, Windows PowerShell in terminal # 2, and so on. What is the relevant configuration to achieve it?
No need to keep changing your default terminal setting. Install the Shell Launcher extension, configure it, and then ctrl-shift-t to select which terminal you want to open inside of VS Code.
As of June 17 '2018, things have become really smooth in Visual Studio (VS) Code when it comes to changing the integrated command shell. I'm enlisting all the options here:
Select a different command shell on the go - Name of the command shell is itself a command to switch the current command shell to the target command shell. For example, let's say my integrated command shell is currently showing Bash, and I want to switch to PowerShell. Then type powershell command and press Enter. The command shell will change to PowerShell. Similarly it works for all other types of command shells installed in VS Code.
More instances of command shell - Press Ctrl + Shift + `(back-tick). Every time you press the keyboard shortcut combination, a new instance of the command shell will get added.
Change the configuration of default command shell type - Press F1 in Visual Studio Code and type or select Terminal: Select Default Shell as shown in the snapshot below.
Once selected, then choose the default command shell of your choice to change the settings permanently:
In recent version (1.15.0) you can
change the settings, then run the terminal, you've set
then change the settings again and run the another type of terminal by pressing the + sign
and so on...
You can put this in your settings file, and uncomment which one you need.
// 64-bit cmd if available, otherwise 32-bit
//"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\cmd.exe"
// 64-bit PowerShell if available, otherwise 32-bit
//"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe"
// Git Bash
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe"
// Bash on Ubuntu (on Windows)
//"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe"
Click on Arrow then click something like Select template profile
so then , will Open the search bar automatically, so... change by your choice
Terminal profiles is what you are looking for - https://code.visualstudio.com/docs/terminal/profiles. It didn't exist by the time the question was posted and the only solution by that time was the somewhat limited "terimnal.integrated.terminal.xxx" solution.
Example configuration:
"terminal.integrated.profiles.windows": {
"Cmd": {
"path": "C:\\Windows\\System32\\cmd.exe",
"icon": "terminal-cmd"
},
"GitBash": {
"path": "C:\\Program Files\\Git\\bin\\bash.exe", "icon": "terminal-bash",
"icon": "terminal-bash"
},
"PowerShell": {
"path": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
"icon": "terminal-powershell"
},
},

Is it possible to permanently alter the title of a command prompt, like it is to alter the 'prompt' value?

I am aware that it is possible to permanently alter the 'prompt' variable (the command prompt that begins every command line, defaulting to the current drive and path, followed by a greater than sign (>)) in the Command Prompt by adding an Environment Variable named 'Prompt' and setting the value to that which can be set by running the prompt command.
However, I am curious to know if it is possible to set the Window Title of the command prompt (this is possible by running title [insert value here]), perhaps by setting a parameter in the cmd.exe file settings. Unfortunately I do not know if this is possible, and if so the correct syntax to get it to work. Hence my question here. I have looked in many places for an answer to this question, but I seemingly find only sites with details on command parameters being executed within the prompt itself.
There is now a way to set the title text in Windows 10 using the PROMPT variable itself, since the Windows 10 console host now recognizes ANSI control codes, but it can be a bit tricky. This is the value of my PROMPT variable:
$E]0; $p ^G$E[1;37;44m$t$s$p$g$e[0m
This gives me a prompt with the time, the current directory and a > with bold white foreground on a blue background. It also sets the window title to the current working directory.
$p and $g you presumably already know. $t inserts the time. $s inserts a space. $e inserts an ESC character. (PROMPT documentation)
$e]0; begins the ANSI sequence to set the window title. It is ended with a ^G (\007) the BEL character. This is where the tricky part lies. Setting this on the command line is trivial; you can simply type Alt+007 or hit ^G on your keyboard. Setting it in the environment variables in the registry is the hard part. The Windows edit box for environment variables ignores the control codes. I resorted to a .reg file to get it set.
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
"PROMPT"="$E]0; $p $E[1;37;44m$t$s$p$g$e[0m"
The ^G character shows as a square box for me here. You need to get that ^G character into that spot in the prompt string.
See the Wikipedia article on ANSI codes for more on what you can do with it.
Simpler way: Start ---> cmd ---> Right-Click ---> Open File Location ---> Right-Click "Command Prompt.exe" ---> Rename ---> ---> Enter ---> Steps 1-2 ---> Click/Right-Click+Run as administrator.
What to change through Properties
Permanent change to cmd title when you open it through Start ---> cmd ---> Left-Click/Right-Click+Run as administrator:
One can create a String key in Computer\HKEY_CURRENT_USER\Software\Microsoft\Command Processor or HKEY_CURRENT_USER\Software\Microsoft\Command Processor named AutoRun, with a value for the Window Title. This will a set the title bar of Command Prompt to the value specified in the key.
Make a registry file:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Command Processor]
"CompletionChar"=dword:00000009
"DefaultColor"=dword:00000000
"EnableExtensions"=dword:00000001
"AutoRun"="title Hello &cls"
change autorun's value to any command, that always run at the begining. just likeÆdx said ;)
Easiest Way:
Right click on desktop and go to "new"
Click "shortcut" and type in "cmd.exe" for file location
Title it whatever you want!
(It will always have the title you selected unless you open it through the "run" box.)
Go to this link below for more help:
https://www.youtube.com/watch?v=dQw4w9WgXcQ

Zsh new terminal tab opens in Home folder instead of CWD if foreground process

I just started using zsh and I have the terminalapp and git plugins so that new terminal tabs open in the current working directory. However, if a process is running in the foreground (a server, vim, etc), new tabs open in the home directory.
How can I change this behavior?
I'm using Terminal.app on OSX Mavercks.
Look at the "Startup" section at the Terminal preferences, "Same Working Directory".
See this blog post
http://russellfinn.com/2013/10/25/current-working-directory-restoration-in-mavericks-terminal-with-tcsh/
Most likely the value is getting cleared when you issue a new command and it is set again after the command finishes. Try placing the escape sequence (from the linked blog post) at the precmd_functions hook (i.e. search the zshall manual page for add-zsh-hook).
PS: see this answer https://superuser.com/questions/313650/resume-zsh-terminal-os-x-lion

Resources