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

Windows Terminal: Open terminal in current folder via single command #620

Closed
nphmuller opened this issue May 9, 2019 · 67 comments
Closed
Labels
Resolution-Answered Related to questions that have been answered

Comments

@nphmuller
Copy link

Currently I can open cmd and powershell in the current folder via typing cmd or powershell in the address bar of explorer (which opens via CTRL+D, so it's really neat and quick).

It would be great if this was also possible with the new Windows Terminal. But since it's a Windows Store app I can't find an easy way to add it to PATH. It also doesn't respect the current folder when opening. It always starts in the default folder (C:\WINDOWS\system32 for cmd and ~ for Ubuntu).

It would be great if this was possible:

  • Make it possible to add Windows Terminal to PATH.
  • Open the command line in the current folder in Windows Terminal.

It would also be nice if it could be opened with a short command.
The executable currently is called WindowsTerminal. Would be nice if we could simply type something like term.

@joandrsn
Copy link

joandrsn commented May 9, 2019

What happens if you use wt.exe ?

https://github.com/microsoft/Terminal/blob/af7316c130059ba15fe16ff0d13608d956a2b449/src/cascadia/CascadiaPackage/Package.appxmanifest#L51

The alias should already launch a WindowsTerminal, but I'm not sure if it launches in the path that you specify

@lennylxx
Copy link

lennylxx commented May 9, 2019

@joandrsn This is great, I didn't know. Thanks.

@nphmuller
Copy link
Author

@joandrsn Thanks! It even launches in the current directory!

@RehanSaeed
Copy link

RehanSaeed commented Aug 30, 2019

How do you pass a directory path to wt.exe. Something like this:

wt.exe "C:\GitHub"

@CEbbinghaus
Copy link

CEbbinghaus commented Sep 11, 2019

Tried but Always opens in Default folder for both CMD and PowerShell.

I can open the WSL Ubuntu shell in the current folder with ubuntu run. but wt always opens in the default folder

@DHowett-MSFT
Copy link
Contributor

Remove startingDirectory from your profiles.json.

@mstyura
Copy link

mstyura commented Sep 19, 2019

Hello @DHowett-MSFT,
is there a way to ask Windows Terminal open new tab in the same folder as current tab? I want the same behavior as exist on Mac OS X: https://apple.stackexchange.com/questions/178017/how-do-i-open-a-new-terminal-window-for-same-directory-as-the-current-window.
Thanks in advance!

@MikenTNT
Copy link

Remove startingDirectory from your profiles.json.

@DHowett-MSFT, this doesn't work in the latest version 0.5.2661.0 :(

@ScientiaEtVeritas
Copy link

Remove startingDirectory from your profiles.json.

For me, it doesn't work either :(

@zadjii-msft
Copy link
Member

As of v0.5, that actually won't work any longer. You'll need to set "startingDirectory": null for this to work actually. That fix regressed unintentionally in #2515.

@itnavigate
Copy link

itnavigate commented Sep 30, 2019

I'd also like to open new tabs in the current directory

@payambordbar
Copy link

As of v0.5, that actually won't work any longer. You'll need to set "startingDirectory": null for this to work actually. That fix regressed unintentionally in #2515.

@zadjii-msft , this doesn't work in v0.5.2681.0

@mantasaudickas
Copy link

mantasaudickas commented Oct 7, 2019

As of v0.5, that actually won't work any longer. You'll need to set "startingDirectory": null for this to work actually. That fix regressed unintentionally in #2515.

Does not work with 0.5.2762.0 either..
I have tried removing value, setting to null.. both does not change anything..

@DHowett-MSFT
Copy link
Contributor

To be clear, removing startingDirectory instead of setting it to null isn’t a feature that’s coming back; we are looking at alternative approaches regarding when exactly to apply startingDirectory, but the setting will always be able to be suppressed by setting it to null.

@ozziepeeps
Copy link

As of v0.5, that actually won't work any longer. You'll need to set "startingDirectory": null for this to work actually. That fix regressed unintentionally in #2515.

Does not work with 0.5.2762.0 either..
I have tried removing value, setting to null.. both does not change anything..

"startingDirectory": null works for me - it needs to be put under a specific profile, rather than being a root-level element

@piotrpalek
Copy link

@DHowett-MSFT it seems to me that an approach of having settings in a config file (startingDirectory) which can be overriden by passing in command line params is pretty common in general. Maybe I'm not getting the issue here :)

@DHowett-MSFT
Copy link
Contributor

If I recall the conversation from last month, it was:

Clearing the “startingDirectory” setting by deleting it from your user profile (in the specific case of the two or three default shipped profiles), instead of setting it to null to clear it, is not a feature that will be coming back.

#607 specifies that we will support command line arguments, one of which will be the starting directory override.

This seems to be in line with my understanding of your understanding of this conversation. 😄

@torpederos
Copy link

For me setting "startingDirectory": "." does the job.

@carlinmack
Copy link

carlinmack commented Nov 26, 2019

is there another issue for having the terminal default open to say "%USERPROFILE%" but null if launched from windows explorer?

@Arcitec
Copy link

Arcitec commented Dec 6, 2019

@carlinmack I don't think that's entirely necessary.

  • The Windows Terminal defaults to the user's home directory if you launch it via the Start menu shortcut or via typing "wt" in Win+R. Which means that if you use "startingDirectory": "." they still open in the user's home folder (%USERPROFILE%).
  • If you launch wt from the Windows Explorer address bar, or via something like cmd.exe (or another shell) navigated to a certain folder, then the "startingDirectory": "." will ensure that Windows Terminal opens in the directory you were in.
  • However... Any additional tabs you open within the Windows Terminal session will open in the "current working dir" from the launch moment, which may be a minor annoyance.

@kerams
Copy link

kerams commented Feb 10, 2020

The Windows Terminal defaults to the user's home directory if you launch it via the Start menu shortcut or via typing "wt" in Win+R. Which means that if you use "startingDirectory": "." they still open in the user's home folder (%USERPROFILE%).

It defaults to system32 in those case for me.

@charlyjose
Copy link

Thanks @torpederos

"startingDirectory": "."
"startingDirectory": "%__CD__%"

Checked. Both works fine for me. My solution.

@RickStrahl have written a good blog regarding this issue. Check it out.

@ghost
Copy link

ghost commented Apr 30, 2020

In my case this worked.

"profiles":
    {
        "defaults":
        {
            "startingDirectory": "."
        },

@frankitox
Copy link

frankitox commented Oct 19, 2020

So, none of the above answers worked for my use case. What I'd like to do is open a new tab using alt+shift+- and keep the previous folder. I tried using null, "." and "%__CD__%" as values for "startingDirectory" in different places of the JSON. I'm using version 1.4.2652.0 of Windows Terminal Preview.

One nice solution would be to have a new option like "keepCurrentDirectoryOnNewTab": true or something like that. Other way (that Kitty uses) is having a special value called current when specifying the directory.

@zadjii-msft
Copy link
Member

@frankitox You're probably interested in #3158 and the like 20 issues that are duped to that thread
image

There's been quite the discussion of why this is hard or nearly impossible to do correctly on windows.

@baddate
Copy link

baddate commented Oct 22, 2020

VERSION: 1.3.2651.0

in my case it works.

    "profiles":
    {
        "defaults":
        {
            // Put settings here that you want to apply to all profiles.
            "startingDirectory": "."
        },

also, you can use "startingDirectory": "%__CD__%"

@james-poulose
Copy link

I am probably very late to the party, but in Windows 10 Pro 20H2, you can just type wt in the address bar and it will do the trick.

@petref
Copy link

petref commented Nov 17, 2020

How do you pass a directory path to wt.exe. Something like this:

wt.exe "C:\GitHub"

I don't know if you found -d and path

@Combo819
Copy link

wt -d .

works well in WSL2

@alexandremucci
Copy link

alexandremucci commented Nov 29, 2020

You can set up the starting directory using your preferred address, either globally or for a specific profile. To have a context menu to open it in the current folder, create and save a .reg file with the content below. Ps.: change {USERNAME} to your username.


Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\windowsterminal]
@="Windows Terminal Here"
[HKEY_CLASSES_ROOT\Directory\Background\shell\windowsterminal\command]
@="\"C:\\Users\\{USERNAME}\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe\" -d ."

Double click the saved file to add these entries to the registry.

@viceice
Copy link

viceice commented Mar 22, 2021

Does anybody know why "startingDirectory": "." in default profil is no longer working on 1.6.10571.0?

Also wt nt will open a new window instead of a new tab.

clicking the + button to open a new tab also no longer reuses the startup directory of the first tab. 😕

@zadjii-msft
Copy link
Member

Does anybody know why "startingDirectory": "." in default profil is no longer working on 1.6.10571.0?

Nope, you should probably file a new issue so we can follow up with you and figure out what's going on

Also wt nt will open a new window instead of a new tab.

That's how it has always worked - in 1.7 there's windowingBehavior and --window <id> to help control this.

clicking the + button to open a new tab also no longer reuses the startup directory of the first tab. 😕

Pretty sure that's how it's always worked - it'll always use the startingDirectory of the default profile, and if your default profile's startingDirectory is ., then it'll use the directory the terminal was launched from.

@viceice
Copy link

viceice commented Mar 22, 2021

Does anybody know why "startingDirectory": "." in default profil is no longer working on 1.6.10571.0?

Nope, you should probably file a new issue so we can follow up with you and figure out what's going on

Also wt nt will open a new window instead of a new tab.

That's how it has always worked - in 1.7 there's windowingBehavior and --window <id> to help control this.

clicking the + button to open a new tab also no longer reuses the startup directory of the first tab. 😕

Pretty sure that's how it's always worked - it'll always use the startingDirectory of the default profile, and if your default profile's startingDirectory is ., then it'll use the directory the terminal was launched from.

I'm using the context menu to open wt in a specific folder (using the entry generated by wt). That will open the first tab in that folder, but new tab will open in system32.

Before i used my own context menu entry:

$exe = (Get-Command wt).Source
$ico = Resolve-Path "$PSScriptRoot\wt.ico"

$title = "Windows Terminal here"

New-Item HKCU:\SOFTWARE\Classes\Directory\shell\WTOpenHere -Force
Set-Item HKCU:\SOFTWARE\Classes\Directory\shell\WTOpenHere $title
New-ItemProperty HKCU:\SOFTWARE\Classes\Directory\shell\WTOpenHere -Name Icon -Value $ico -Force
New-Item HKCU:\SOFTWARE\Classes\Directory\shell\WTOpenHere\command -Force
Set-Item HKCU:\SOFTWARE\Classes\Directory\shell\WTOpenHere\command """$exe"" -d ""%L"""


New-Item HKCU:\SOFTWARE\Classes\Directory\background\shell\WTOpenHere -Force
Set-Item HKCU:\SOFTWARE\Classes\Directory\background\shell\WTOpenHere $title
New-ItemProperty HKCU:\SOFTWARE\Classes\Directory\background\shell\WTOpenHere -Name Icon -Value $ico -Force
New-Item HKCU:\SOFTWARE\Classes\Directory\background\shell\WTOpenHere\command -Force
Set-Item HKCU:\SOFTWARE\Classes\Directory\background\shell\WTOpenHere\command "$exe"


# Currently not supported until v0.9
#Remove-Item HKCU:\SOFTWARE\Classes\Directory\shell\WTOpenHere -Force -Recurse

@zadjii-msft
Copy link
Member

Yep, that's exactly how it works. You might be interested in #8933

@kaaax0815
Copy link

I had the same problem. I fixed it by adding the following line at the end of my .bashrc file.

@manishprivet Is this possible in windows?

@manishprivet
Copy link

I had the same problem. I fixed it by adding the following line at the end of my .bashrc file.

@manishprivet Is this possible in windows?

@kaaax0815 Yes this is for windows only

@kaaax0815
Copy link

@kaaax0815 Yes this is for windows only

@manishprivet But bashrc would be wsl not native windows. Or where should I put it

@manishprivet
Copy link

@kaaax0815 Yes this is for windows only

@manishprivet But bashrc would be wsl not native windows. Or where should I put it

@kaaax0815 In the WSL's bashrc file actually

@kaaax0815
Copy link

@manishprivet the matter has resolved itself. I changed the cmd search shortcut to wt and this fixed the path problem

@KSneijders
Copy link

KSneijders commented Jun 11, 2021

You can now just go to settings -> profiles (Powershell / Command prompt).
And check the checkbox: 'Use parent process directory'

image

@rafapaulin
Copy link

@KSneijders unfortunately we do not have that on wsl :(

@rafapaulin
Copy link

actually we do, but it does not work

@rafapaulin
Copy link

power shell also does not work

@pratikbarve09
Copy link

pratikbarve09 commented Nov 12, 2021

So, none of the above answers worked for my use case. What I'd like to do is open a new tab using alt+shift+- and keep the previous folder. I tried using null, "." and "%__CD__%" as values for "startingDirectory" in different places of the JSON. I'm using version 1.4.2652.0 of Windows Terminal Preview.

One nice solution would be to have a new option like "keepCurrentDirectoryOnNewTab": true or something like that. Other way (that Kitty uses) is having a special value called current when specifying the directory.

I'm using Windows Terminal v1.11.2921.0
this worked for me:

  1. in windows terminal, under default profile, add startingDirectory value to "."
  2. if you are going with explorer, in the address bar type "wt" and it will open your default profile in that directory or right-click and choose "open windows terminal here" option to open the default profile (if you have edited the registry for this)
  3. for every profile add a value of "startingDirectory" to "."

@krlvm
Copy link

krlvm commented Dec 17, 2021

If anyone is looking for a solution without changing startingDirectory, I have placed such a file in %SystemRoot\System32 named wtx.cmd:

@ECHO off
wt -d .
exit /B !ERRORLEVEL!

@AEROGU
Copy link

AEROGU commented Jul 6, 2022

Remove startingDirectory from your profiles.json.

Thanks @DHowett-MSFT, I was making VSCode when pressing ctrl+ñ the external console wt.exe appears, because the integrated one has problems with unicode in windows and, when I did it, it always started in my %USERPROFILE%, your solution worked, now runs in my project directory.

For those who are interested, I used wt --window 0 focus-tab so that if I already have a WindowsTerminal window open, it will be raised instead of creating another instance.

@ErcinDedeoglu
Copy link

You can now just go to settings -> profiles (Powershell / Command prompt). And check the checkbox: 'Use parent process directory'

image

thank you! this solution is the best solution. and still working

@sanayvarghese
Copy link

Current directory in Windows terminal using explorer

You can open current directory in Windows terminal using explorer using
wt -d .

@jleaders
Copy link

jleaders commented Mar 29, 2023

Please add wt -d to wt -h
image

@zadjii-msft
Copy link
Member

@jleaders the help text is understandably a little confusing in this particular case, because of the implicit new-tab subcommand that's used when no subcommand is provided explicitly. You'll note, -d is under wt new-tab --help

image

It's just that CLI11 doesn't know how to wire the args for new-tab up to the help message for the root command's help text.

@filiSpinelli
Copy link

@nphmuller Referring to the first main discussion of thus topic, you can open a new terminal windows, placed in the current working folder, by typing "cmd" and press Enter in the url ##bar.
image

@loan-mgt
Copy link

solution wt -d .

@6philipp9
Copy link

6philipp9 commented Feb 9, 2024

[ Settings : "Startup" → "standard terminal application" ] change this to windows terminal -> All Terminals open now in the Windows Terminal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution-Answered Related to questions that have been answered
Projects
None yet
Development

No branches or pull requests