terminal icon indicating copy to clipboard operation
terminal copied to clipboard

Opening Windows Terminal in current directory from right-click folder context menu (the `ubuntu.exe` bug)

Open marcodiiga opened this issue 2 years ago • 31 comments

Description of the new feature/enhancement

I couldn't find a way to open up wt.exe in a linux profile (e.g. Ubuntu) via right-click in the context menu that pops up in explorer by setting the current working directory to whatever I was browsing before.

To rephrase: I added some HKCR\Directory\Background\shell\wt\command key with wt.exe -d "%V" so that I can Shift+RightClick in a folder in explorer.exe and have a Open in Windows Terminal with Linux item in the context menu. Windows Terminal opens but the starting directory, no matter what I set the profile startingDirectory field in the JSON to, is always set to my home directory. It would be nice if wt.exe -d "C:\\" or whatever actually opened, for the linux profile, in /mnt/c as starting directory.

marcodiiga avatar Apr 21 '22 14:04 marcodiiga

What version are you on? Could you share your settings.json file?

I'm mildly worried that our ~ auto promotion may have accidentally interfered with the ability to use the explorer context menu entry with a WSL profile 😬

aside: Is there a reason you're not just using the explorer context menu entry that the Terminal creates itself when you install it/?

zadjii-msft avatar Apr 21 '22 14:04 zadjii-msft

Sure, here's my settings.json:

{
    "$help": "https://aka.ms/terminal-documentation",
    "$schema": "https://aka.ms/terminal-profiles-schema",
    "actions":
    [
        {
            "command":
            {
                "action": "copy",
                "singleLine": false
            },
            "keys": "ctrl+c"
        },
        {
            "command": "paste",
            "keys": "ctrl+v"
        },
        {
            "command": "find",
            "keys": "ctrl+shift+f"
        },
        {
            "command":
            {
                "action": "splitPane",
                "split": "auto",
                "splitMode": "duplicate"
            },
            "keys": "alt+shift+d"
        }
    ],
    "copyFormatting": "none",
    "copyOnSelect": false,
    "defaultProfile": "{51855cb2-8cce-5362-8f54-464b92b32386}",
    "language": "en-US",
    "profiles":
    {
        "defaults": {},
        "list":
        [
            {
                "commandline": "%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "hidden": false,
                "name": "Windows PowerShell"
            },
            {
                "commandline": "%SystemRoot%\\System32\\cmd.exe",
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "hidden": false,
                "name": "Prompt dei comandi"
            },
            {
                "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
                "hidden": true,
                "name": "Ubuntu",
                "source": "Windows.Terminal.Wsl",
                "startingDirectory": "%USERPROFILE%"
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            },
            {
                "guid": "{5b849eef-e172-55b9-8019-ef031bec368c}",
                "hidden": false,
                "name": "Developer Command Prompt for VS 2022",
                "source": "Windows.Terminal.VisualStudio"
            },
            {
                "guid": "{8c6e956b-317a-5194-b80e-c5281032870f}",
                "hidden": false,
                "name": "Developer PowerShell for VS 2022",
                "source": "Windows.Terminal.VisualStudio"
            },
            {
                "guid": "{51855cb2-8cce-5362-8f54-464b92b32386}",
                "hidden": false,
                "name": "Ubuntu",
                "source": "CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc",
                "startingDirectory": "%USERPROFILE%"
            }
        ]
    },
    "schemes":
    [
       ... unimportant I guess...
    ]
}

For the second question: the context menu entry created at install time doesn't set the starting directory to the current one I was browsing while I clicked with Shift+RightClick either, seems to work with the WSL profile, not with the other Ubuntu profile.

marcodiiga avatar Apr 21 '22 14:04 marcodiiga

            {
                "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
                "hidden": true,
                "name": "Ubuntu",
                "source": "Windows.Terminal.Wsl",
                "startingDirectory": "%USERPROFILE%"
            },
			// ...
            {
                "guid": "{51855cb2-8cce-5362-8f54-464b92b32386}",
                "hidden": false,
                "name": "Ubuntu",
                "source": "CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc",
                "startingDirectory": "%USERPROFILE%"
            }

Interesting - canonical is shipping their own profile, but not one layered with the existing profile. Curious....

zadjii-msft avatar Apr 21 '22 14:04 zadjii-msft

Interesting. I really can't get this to repro... Is this always affecting a specific path? I know there have been troubles with doing this in the root of a drive before (e.g. c:\, or D:\), but child directories seem to work fine.

Which OS version and Terminal version are you on/? IIRC there were some os-side issues with Directory/Background entries in the past

zadjii-msft avatar Apr 21 '22 14:04 zadjii-msft

Windows 10 (10.0.19044 Build 19044), wt 1.12.10982.0. It is affecting any path that I could try, not only root drives but also child dirs.

marcodiiga avatar Apr 21 '22 15:04 marcodiiga

I found a workaround to make this work: this opens up the shit+right-click context menu directory in Windows Terminal in the default profile (which I have set to Canonical's own profile).

image

marcodiiga avatar Apr 21 '22 15:04 marcodiiga

Oh heck that's probably it! I bet they've got the commandline of their profile set up so that it doesn't actually obey the startingDirectory!

zadjii-msft avatar Apr 21 '22 15:04 zadjii-msft

Is your profile using wsl.exe -d Ubuntu or Ubuntu.exe?

The latter will not respect the starting directory, and always opens in $HOME

jacob-pro avatar May 06 '22 09:05 jacob-pro

The latter will not respect the starting directory, and always opens in $HOME

That's exactly it. There's definitely and issue with the Ubuntu profiles that Canonical is shipping. Sorry about that! I'm reaching out to them to see what we can do here.

zadjii-msft avatar May 06 '22 10:05 zadjii-msft

Same problem here after removing one of the distros I had installed; Windows Terminal settings also still recognizing two distros at the selection for the default one.

trizotti avatar Jun 26 '22 23:06 trizotti

I had the same problem with Ubuntu-22.04 installed via Microsoft Store. The terminal always started in /home/, no matter the current directory where context menu was invoked (on right click). Adding "commandline": "wsl.exe -d Ubuntu-22.04 /bin/bash" to terminal profile fixed the issue. image

elliason avatar Aug 06 '22 07:08 elliason

Is your profile using wsl.exe -d Ubuntu or Ubuntu.exe?

The latter will not respect the starting directory, and always opens in $HOME

WORKS!!!!!

muhammedanaskhan avatar Aug 06 '22 11:08 muhammedanaskhan

Thanks for this! Managed to get it working by adding the commandLine to it. However, now when opening it normally, it will always open default to /mnt/c/WINDOWS/System32. Is there a way to keep opening in current directory context menu, while also having it open in the default /home directory when opening terminal on it's own?

nrobinson12 avatar Oct 07 '22 02:10 nrobinson12

Is your profile using wsl.exe -d Ubuntu or Ubuntu.exe? The latter will not respect the starting directory, and always opens in $HOME

WORKS!!!!!

sir can you help me out , what i need to change here , please sir thankyou

"list": [ { "commandline": "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe", "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}", "hidden": false, "name": "Windows PowerShell" }, { "commandline": "%SystemRoot%\System32\cmd.exe", "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}", "hidden": false, "name": "Command Prompt" }, { "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}", "hidden": false, "name": "PowerShell", "source": "Windows.Terminal.PowershellCore" }, { "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}", "hidden": false, "name": "Azure Cloud Shell", "source": "Windows.Terminal.Azure" }, { "commandline": "wsl.exe -d Ubuntu-22.04 /bin/bash", "guid": "{51855cb2-8cce-5362-8f54-464b92b32386}", "hidden": false, "name": "Ubuntu", "source": "CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc" }, { "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}", "hidden": false, "name": "Ubuntu", "source": "Windows.Terminal.Wsl", "startingDirectory": null } ]

iamLazyCode avatar Jan 15 '23 19:01 iamLazyCode

Is your profile using wsl.exe -d Ubuntu or Ubuntu.exe? The latter will not respect the starting directory, and always opens in $HOME

WORKS!!!!!

sir can you help me out , what i need to change here , please sir thankyou

"list": [ { "commandline": "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe", "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}", "hidden": false, "name": "Windows PowerShell" }, { "commandline": "%SystemRoot%\System32\cmd.exe", "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}", "hidden": false, "name": "Command Prompt" }, { "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}", "hidden": false, "name": "PowerShell", "source": "Windows.Terminal.PowershellCore" }, { "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}", "hidden": false, "name": "Azure Cloud Shell", "source": "Windows.Terminal.Azure" }, { "commandline": "wsl.exe -d Ubuntu-22.04 /bin/bash", "guid": "{51855cb2-8cce-5362-8f54-464b92b32386}", "hidden": false, "name": "Ubuntu", "source": "CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc" }, { "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}", "hidden": false, "name": "Ubuntu", "source": "Windows.Terminal.Wsl", "startingDirectory": null } ]

Check if you really have sat up right distro name:

Try to run from CMD or PS:

wsl -l -v

Then open terminal settings (json file). Search for your profile and change "commandline" value to this:

"commandline": "C:\\windows\\System32\\wsl.exe -d Ubuntu-22.04",

or

"commandline": "wsl.exe -d Ubuntu-22.04",

sergey-gr avatar Feb 01 '23 19:02 sergey-gr

Is your profile using wsl.exe -d Ubuntu or Ubuntu.exe? The latter will not respect the starting directory, and always opens in $HOME

WORKS!!!!!

sir can you help me out , what i need to change here , please sir thankyou "list": [ { "commandline": "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe", "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}", "hidden": false, "name": "Windows PowerShell" }, { "commandline": "%SystemRoot%\System32\cmd.exe", "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}", "hidden": false, "name": "Command Prompt" }, { "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}", "hidden": false, "name": "PowerShell", "source": "Windows.Terminal.PowershellCore" }, { "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}", "hidden": false, "name": "Azure Cloud Shell", "source": "Windows.Terminal.Azure" }, { "commandline": "wsl.exe -d Ubuntu-22.04 /bin/bash", "guid": "{51855cb2-8cce-5362-8f54-464b92b32386}", "hidden": false, "name": "Ubuntu", "source": "CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc" }, { "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}", "hidden": false, "name": "Ubuntu", "source": "Windows.Terminal.Wsl", "startingDirectory": null } ]

Check if you really have sat up right distro name:

Try to run from CMD or PS:

wsl -l -v

Then open terminal settings (json file). Search for your profile and change "commandline" value to this:

"commandline": "C:\\windows\\System32\\wsl.exe -d Ubuntu-22.04",

or

"commandline": "wsl.exe -d Ubuntu-22.04",

i just cant thankyou enough this worked like a charm...thankyou once again just one more thing if u can help sir ...some icon error

image

iamLazyCode avatar Feb 01 '23 20:02 iamLazyCode

Is your profile using wsl.exe -d Ubuntu or Ubuntu.exe? The latter will not respect the starting directory, and always opens in $HOME

WORKS!!!!!

sir can you help me out , what i need to change here , please sir thankyou "list": [ { "commandline": "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe", "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}", "hidden": false, "name": "Windows PowerShell" }, { "commandline": "%SystemRoot%\System32\cmd.exe", "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}", "hidden": false, "name": "Command Prompt" }, { "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}", "hidden": false, "name": "PowerShell", "source": "Windows.Terminal.PowershellCore" }, { "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}", "hidden": false, "name": "Azure Cloud Shell", "source": "Windows.Terminal.Azure" }, { "commandline": "wsl.exe -d Ubuntu-22.04 /bin/bash", "guid": "{51855cb2-8cce-5362-8f54-464b92b32386}", "hidden": false, "name": "Ubuntu", "source": "CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc" }, { "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}", "hidden": false, "name": "Ubuntu", "source": "Windows.Terminal.Wsl", "startingDirectory": null } ]

Check if you really have sat up right distro name: Try to run from CMD or PS:

wsl -l -v

Then open terminal settings (json file). Search for your profile and change "commandline" value to this:

"commandline": "C:\\windows\\System32\\wsl.exe -d Ubuntu-22.04",

or

"commandline": "wsl.exe -d Ubuntu-22.04",

i just cant thankyou enough this worked like a charm...thankyou once again just one more thing if u can help sir ...some icon error

image

You can pick icon from the internet, by setting Icon value to:

https://assets.ubuntu.com/v1/49a1a858-favicon-32x32.png

Or download and save it on your computer, then just set correct path to the icon.

For example I use one of local app icons (Linux icon):

ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png

sergey-gr avatar Feb 02 '23 09:02 sergey-gr

Is your profile using wsl.exe -d Ubuntu or Ubuntu.exe? The latter will not respect the starting directory, and always opens in $HOME

WORKS!!!!!

sir can you help me out , what i need to change here , please sir thankyou "list": [ { "commandline": "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe", "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}", "hidden": false, "name": "Windows PowerShell" }, { "commandline": "%SystemRoot%\System32\cmd.exe", "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}", "hidden": false, "name": "Command Prompt" }, { "guid": "{574e775e-4f2a-5b96-ac1e-a2962a402336}", "hidden": false, "name": "PowerShell", "source": "Windows.Terminal.PowershellCore" }, { "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}", "hidden": false, "name": "Azure Cloud Shell", "source": "Windows.Terminal.Azure" }, { "commandline": "wsl.exe -d Ubuntu-22.04 /bin/bash", "guid": "{51855cb2-8cce-5362-8f54-464b92b32386}", "hidden": false, "name": "Ubuntu", "source": "CanonicalGroupLimited.Ubuntu_79rhkp1fndgsc" }, { "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}", "hidden": false, "name": "Ubuntu", "source": "Windows.Terminal.Wsl", "startingDirectory": null } ]

Check if you really have sat up right distro name: Try to run from CMD or PS:

wsl -l -v

Then open terminal settings (json file). Search for your profile and change "commandline" value to this:

"commandline": "C:\\windows\\System32\\wsl.exe -d Ubuntu-22.04",

or

"commandline": "wsl.exe -d Ubuntu-22.04",

i just cant thankyou enough this worked like a charm...thankyou once again just one more thing if u can help sir ...some icon error image

You can pick icon from the internet, by setting Icon value to:

https://assets.ubuntu.com/v1/49a1a858-favicon-32x32.png

Or download and save it on your computer, then just set correct path to the icon.

For example I use one of local app icons (Linux icon):

ms-appx:///ProfileIcons/{9acb9455-ca41-5af7-950f-6bca1bc9722f}.png

thankyou so much will try later in evening , thankyou again sir

iamLazyCode avatar Feb 02 '23 10:02 iamLazyCode

Came here to report that using ubuntu.exe DOES work with the run command proceeding, which starts the default shell in the current directory (where invoked). So, for my Windows Terminal profile, I have the following in the Command Line setting:

ubuntu.exe run

I would like to see the default change to use this setting value.

Source

mrtumnus avatar May 31 '23 16:05 mrtumnus

Thanks for this! Managed to get it working by adding the commandLine to it. However, now when opening it normally, it will always open default to /mnt/c/WINDOWS/System32. Is there a way to keep opening in current directory context menu, while also having it open in the default /home directory when opening terminal on it's own?

The ubuntu.exe run method also has this problem. Anyone have ideas? I assume this is because the starting directory for wt.exe by default is the system directory (C:\WINDOWS\system32).

EDIT: I can create a shortcut to the wt.exe executable and set the starting location to my WSL $HOME (\wsl.localhost\Ubuntu\home<user>), but I can't pin the shortcut to the taskbar and have it merge with any open Terminal windows.

mrtumnus avatar Jun 09 '23 14:06 mrtumnus

Workaround - add the following in .bashrc inside WSL:

# Change starting directory for the default system directory
if [ "$PWD" = "/mnt/c/WINDOWS/system32" ]; then
  cd ~
fi

mrtumnus avatar Jun 09 '23 15:06 mrtumnus

Interesting - canonical is shipping their own profile, but not one layered with the existing profile. Curious....

Did you ever figure out why we have two? Which one should we use?

mrtumnus avatar Jun 09 '23 15:06 mrtumnus

Workaround - add the following in .bashrc inside WSL:

# Change starting directory for the default system directory
if [ "$PWD" = "/mnt/c/WINDOWS/system32" ]; then
  cd ~
fi

Thank you! Why didn't I think of this?

nrobinson12 avatar Jun 10 '23 06:06 nrobinson12

Thank you! Why didn't I think of this?

Can't really think of a downside, unless you explicitly want to open terminal in the system directory.

The only thing that still doesn't work with this setup is opening terminal within a network share. It ends up landing in $HOME in this case. Still have to look around issues here to see if it's reported already.

mrtumnus avatar Jun 10 '23 14:06 mrtumnus

The only thing that still doesn't work with this setup is opening terminal within a network share. It ends up landing in $HOME in this case. Still have to look around issues here to see if it's reported already.

WSL does not automatically map network shares. It cannot be launched in any directory that is not represented or representable in the Linux filesystem. You would need to mount them explicitly in /etc/fstab, and even then it may not be able to figure it out.

This would make an excellent feature request for the WSL repo :smile:

DHowett avatar Jun 12 '23 17:06 DHowett

WSL does not automatically map network shares.

I could have sworn I had this working in WSL2. But I could be getting confused with my previous cygwin setup.

mrtumnus avatar Jun 12 '23 17:06 mrtumnus

Ah, even WSL 1 doesn't automatically map network shares.

DHowett avatar Jun 12 '23 17:06 DHowett

Interesting - canonical is shipping their own profile, but not one layered with the existing profile. Curious....

Did you ever figure out why we have two? Which one should we use?

Exactly my question. Why are 2 profiles generated and the bad one is enabled and not vice versa?

From all the related bugs, the profile generated by ? (with the Tux icon) is the correct one and the one generated by Canonical (with the Ubuntu icon, ubuntu.exe) is wrong. So why is it so that both profiles are generated and moreover that the correct one is disabled by default? Basically, it comes to the question, why the WSL / Terminal team cannot do anything about this (yet)?

IMO, from user's perspective, this seems absurd, so it would deserve at least a short explanation. And also a dedicated issue like #15560 - @zadjii-msft, are you really sure you want to continue the discussion here, where logically stuff quite unrelated to this profiles bug (like "mapping network shares" just above) were and will be discussed?

Anyway, thank you guys for pushing this further, I believe this confuses tons of people and is a huge waste of time in the sum.

pbodnar avatar Jul 23 '23 07:07 pbodnar