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

Created on 9 May 2019  ·  43Comments  ·  Source: microsoft/terminal

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.

Resolution-Answered

Most helpful comment

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.

All 43 comments

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

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

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

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

wt.exe "C:\GitHub"

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

Remove startingDirectory from your profiles.json.

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!

Remove startingDirectory from your profiles.json.

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

Remove startingDirectory from your profiles.json.

For me, it doesn't work either :(

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.

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

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

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

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.

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

@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 :)

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. :smile:

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

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

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

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.

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.

In my case this worked.

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

"startingDirectory": "." works but when I open terminal from search it defaults to system32.
Is there any way to open in the user directory when the path is not specified?

"startingDirectory": "." works but when I open terminal from search it defaults to system32.
Is there any way to open in the user directory when the path is not specified?

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

export CURR_DIRECTORY=`pwd`
if [ "$CURR_DIRECTORY" = "/mnt/c/WINDOWS/system32" ]; then
    cd ~
fi

Solution of @manishprivet was great, except that In order to work properly, I have changed the system32 to System32. It's a good idea to run pwd and copy the output.

I found a workaround by adding "open terminal here" to right-click.

step-1:-
Remove the starting directory from the settings.json for all profiles.

step-2:-
Add the open terminal to right-click according to this post.
https://github.com/microsoft/terminal/issues/1060#issuecomment-497539461
I changed the 2nd registry value from @="%LOCALAPPDATA%\\Microsoft\\WindowsApps\\wt.exe" to @="C:\Users\[your username]\AppData\Local\Microsoft\WindowsApps\wt.exe -d ."

Behaviour after modifications:
So whenever I open terminal using search/taskbar/shortcut it will open in my user folder.
Whenever I want to open the terminal using the explorer, I just right-click and select Windows terminal here option.

Note 1: Don't forget to add -d . at the end of the second registry value, otherwise, this will not work.

Note 2: Typing wt in explorer bar still opens the terminal in system32. You should still type wt -d . if you want to open terminal in the current folder using the "explorer bar".

Just add

"startingDirectory": null

It works with wt V1.0 properly.

If you prefer making wt shown in SHIFT-Menu just like "Open PowerShell window here" or Open WSL options, you can merge this registry file to your Registry.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt]
@="Open Windows Terminal here"
"ShowBasedOnVelocityId"=dword:00639bc8
"Extended"=""

[HKEY_CLASSES_ROOT\Directory\Background\shell\wt\command]
@="C:\\Users\\[YourUserName]\\AppData\\Local\\Microsoft\\WindowsApps\\wt.exe"

Please note the startingDirectory parameter should go in this place in the JSON structure:

{
    //...

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

            // Attempt to start in current directory
            // https://github.com/microsoft/terminal/issues/620
            "startingDirectory": "."
        }
    }
}

I'm yet to see a solution that will do the following:

  1. Allow me to select a default directory when I open Terminal from start menu.
  2. When running wt.exe from explorer window will open Terminal to that directory.

Is this possible?

From my investigation, that wasn't something that was technically possible.

If you want both to work, you'll need to use wt -d . in the explorer address bar. This will let launching the Terminal from the start menu and the run box work the same as before, and still let you open the Terminal in the CWD in explorer

From my initial exploration here is what I found based on https://docs.microsoft.com/en-us/windows/terminal/command-line-arguments?tabs=windows

Approach 1:

  1. Press WIN key + R
  2. Type wt -d D:\SomeDirectory

This will open up the default shell present in the settings.json with that directory
P.S. Choose path based on the OS of the shell in default

Approach 2:

  1. Go to the desired folder using the File Explorer
  2. In the address bar of the desired folder Type wt -d .

@AndeYashwanth That works perfectly, thank you. Now I can have it open in my user directory from the taskbar and only open in the current directory when I right click 😎

Thanks @29kartik approach 1 worked for me. I was able to get GitKraken to use a custom terminal command of wt -d %d so that now it's terminal shortcut launches WT at the current repo's directory. 💯

I've added this to open in this specific folder

 "startingDirectory": "C:\\webdev"

This is my take on it via context menu shortcut: https://gist.github.com/kmantel/9cd6bf9f0fb25694a5b08119817281b7

@nickraphael:

I'm yet to see a solution that will do the following:

  1. Allow me to select a default directory when I open Terminal from start menu.
  2. When running wt.exe from explorer window will open Terminal to that directory.

Having this in settings.json:

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

makes Windows Terminal start in the current directory for me when launched from Explorer location box, win+r or from other programs (VS Code etc.). However, when starting from Start menu, either by searching or navigating to the shortcut, it starts in C:\WINDOWS\system32.

To fix the Start menu issue, I pinned a shortcut to wt.exe to the Start menu (where wt tells its location), renamed the shortcut to my liking and then modified it to have:

Start in: %USERPROFILE%

Now Windows Terminal starts in my home directory when launched from the Start menu.

It would be nice if Windows Terminal wouldn't change its directory by default to make it more transparent. There aren't many other programs which do that.

I'm also wondering if the shortcut to wt.exe could be made by the installer automatically.

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.

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

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__%"

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.

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

wt -d .

works well in WSL2

Was this page helpful?
0 / 5 - 0 ratings