microsoft / terminal

The new Windows Terminal and the original Windows console host, all in the same place!
MIT License
95.06k stars 8.24k forks source link

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

Closed nphmuller closed 5 years ago

nphmuller commented 5 years ago

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:

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 commented 5 years ago

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 commented 5 years ago

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

nphmuller commented 5 years ago

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

RehanSaeed commented 5 years ago

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

wt.exe "C:\GitHub"
CEbbinghaus commented 5 years ago

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 commented 5 years ago

Remove startingDirectory from your profiles.json.

mstyura commented 5 years ago

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 commented 5 years ago

Remove startingDirectory from your profiles.json.

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

ScientiaEtVeritas commented 5 years ago

Remove startingDirectory from your profiles.json.

For me, it doesn't work either :(

zadjii-msft commented 5 years ago

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 commented 4 years ago

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

payambordbar commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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:

torpederos commented 4 years ago

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

carlinmack commented 4 years ago

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

Arcitec commented 4 years ago

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

kerams commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

In my case this worked.

"profiles":
    {
        "defaults":
        {
            "startingDirectory": "."
        },
AndeYashwanth commented 4 years ago

"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?

manishprivet commented 4 years ago

"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
mrghofrani commented 4 years ago

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.

AndeYashwanth commented 4 years ago

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

LauKT commented 4 years ago

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"
connorjak commented 4 years ago

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": "."
        }
    }
}
nickraphael commented 4 years ago

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?

zadjii-msft commented 4 years ago

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

29kartik commented 4 years ago

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 .
spookyuser commented 4 years ago

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

StJohn3D commented 4 years ago

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

franzwcom commented 4 years ago

I've added this to open in this specific folder

 "startingDirectory": "C:\\webdev"
kmantel commented 4 years ago

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

kankri commented 4 years ago

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

frankitox commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago
wt -d .

works well in WSL2

alexandremucci commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

kaaax0815 commented 3 years ago

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 commented 3 years ago

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