Add a new environment variable to describe the way terminal is launched #20923

Closed
opened 2026-01-31 07:27:52 +00:00 by claunia · 4 comments
Owner

Originally created by @steve02081504 on GitHub (Nov 30, 2023).

Description of the new feature/enhancement

I want my shell to use the folder path when I start it via open in terminal in the context menu
and when I open the shell in the other menus I want it to use the user path ~.
In the current version of wt I have to do this in my profile.

if pwd=='C:\Windows\System32'
	cd ~

The obvious disadvantage of this is that if I actually start wt in C:\Windows\System32, inevitably the shell will jump back to ~.

But if wt provides an environment variable that describes the way it is started, I can change the logic to something like this

if env:WT_FROM & env:WT_FROM!=('RightCheck' | 'Command')
	cd ~

Proposed technical implementation details (optional)

Add a new environment variable that describes how wt is started.
Add a new environment variable that describes how many tabs the current tab is.
Add a new environment variable that describes wt's version

Originally created by @steve02081504 on GitHub (Nov 30, 2023). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> # Description of the new feature/enhancement <!-- A clear and concise description of what the problem is that the new feature would solve. Describe why and how a user would use this new functionality (if applicable). --> I want my shell to use the folder path when I start it via open in terminal in the context menu and when I open the shell in the other menus I want it to use the user path `~`. In the current version of wt I have to do this in my profile. ```bash if pwd=='C:\Windows\System32' cd ~ ``` The obvious disadvantage of this is that if I actually start wt in C:\Windows\System32, inevitably the shell will jump back to `~`. But if wt provides an environment variable that describes the way it is started, I can change the logic to something like this ```bash if env:WT_FROM & env:WT_FROM!=('RightCheck' | 'Command') cd ~ ``` # Proposed technical implementation details (optional) <!-- A clear and concise description of what you want to happen. --> Add a new environment variable that describes how wt is started. Add a new environment variable that describes how many tabs the current tab is. Add a new environment variable that describes wt's version
claunia added the Issue-FeatureNeeds-TriageNeeds-Tag-FixNeeds-Attention labels 2026-01-31 07:27:52 +00:00
Author
Owner

@zadjii-msft commented on GitHub (Nov 30, 2023):

Wait hold on, all this sounds like overkill to me.

How do you have your profile configured? I'd guess with "startingDirectory": "." or something, right? That would explain why starting it from the run dialog (or other menus) starts your shell in system32 - the . is evaluated relative to the CWD of Terminal itself (which is system32).

The shell extension (read: the file explorer right-click context menu) already launches the Terminal with wt -d <the current path>. That should spawn your profile with a startingDirectory set to the CWD in explorer.

Now, setting the default startingDirectory to ~ is a little tricky. I'm assuming you're using WSL here - if you are, then the following should work:

"startingDirectory": "~",

Now, that may not work, depending on the profile's commandline. More reliable might be:

"startingDirectory": "//wsl$/<distro name>/home/your WSL user/",

(e.g. "startingDirectory": "//wsl$/Ubuntu-18.04/home/zadjii/")

Of course, if you're using PowerShell, I think just plain-old %userprofile% should get you what you're looking for.

Does that all help/?

@zadjii-msft commented on GitHub (Nov 30, 2023): Wait hold on, all this sounds like overkill to me. How do you have your profile configured? I'd guess with `"startingDirectory": "."` or something, right? That would explain why starting it from the run dialog (or other menus) starts your shell in system32 - the `.` is evaluated relative to the CWD of Terminal itself (which is system32). The shell extension (read: the file explorer right-click context menu) already launches the Terminal with `wt -d <the current path>`. That should spawn your profile with a `startingDirectory` set to the CWD in explorer. Now, setting the default `startingDirectory` to `~` is a little tricky. I'm assuming you're using WSL here - if you are, then the following should work: ```json "startingDirectory": "~", ``` Now, that may not work, depending on the profile's commandline. More reliable might be: ```json "startingDirectory": "//wsl$/<distro name>/home/your WSL user/", ``` (e.g. `"startingDirectory": "//wsl$/Ubuntu-18.04/home/zadjii/"`) Of course, if you're using PowerShell, I think just plain-old `%userprofile%` should get you what you're looking for. Does that all help/?
Author
Owner

@steve02081504 commented on GitHub (Nov 30, 2023):

Does that all help/?

Thanks for the answer, I'd totally know how to set the shell to ~.

图片

The problem is that I am pursuing the following:

  1. when terminal is evoked by right clicking from a folder the default working path of the shell is in that folder
  2. When wt is evoked from another program (e.g. CMD) other than the start menu or a shortcut, the current working directory of the shell is the working directory of the parent process
  3. otherwise the working directory is in ~

This seems to be a difficult setup to accomplish in the current wt, I can't tell from the shell script how wt was started, and I even have to write extra functions to look up the version of wt from the registry.
I guess more environment variables are always good.

@steve02081504 commented on GitHub (Nov 30, 2023): > Does that all help/? Thanks for the answer, I'd totally know how to set the shell to `~`. ![图片](https://github.com/microsoft/terminal/assets/31927825/38183f58-7327-46d7-9232-c8df60310ad3) The problem is that I am pursuing the following: 1. when terminal is evoked by right clicking from a folder the default working path of the shell is in that folder 2. When wt is evoked from another program (e.g. CMD) other than the start menu or a shortcut, the current working directory of the shell is the working directory of the parent process 3. otherwise the working directory is in `~` This seems to be a difficult setup to accomplish in the current wt, I can't tell from the shell script how wt was started, and I even have to write extra functions to look up the version of wt from the registry. I guess more environment variables are always good.
Author
Owner

@zadjii-msft commented on GitHub (Nov 30, 2023):

When wt is evoked from another program (e.g. CMD)

For that, run the Terminal with wt -d .
That'll specifically tell the Terminal "run the profile in the current directory".

@zadjii-msft commented on GitHub (Nov 30, 2023): > When wt is evoked from another program (e.g. CMD) For that, run the Terminal with `wt -d .` That'll specifically tell the Terminal "run the profile in the current directory".
Author
Owner

@steve02081504 commented on GitHub (Nov 30, 2023):

For that, run the Terminal with wt -d . That'll specifically tell the Terminal "run the profile in the current directory".

Oh! Thank you for your help. It looks like I did make a silly mistake. Sorry for the delay.

@steve02081504 commented on GitHub (Nov 30, 2023): > For that, run the Terminal with `wt -d .` That'll specifically tell the Terminal "run the profile in the current directory". Oh! Thank you for your help. It looks like I did make a silly mistake. Sorry for the delay.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#20923