Skip to content

pwsh: handling z ~ and z separately#1161

Open
chawyehsu wants to merge 1 commit intoajeetdsouza:mainfrom
chawyehsu:patch-respect-pwsh-cd-behavior
Open

pwsh: handling z ~ and z separately#1161
chawyehsu wants to merge 1 commit intoajeetdsouza:mainfrom
chawyehsu:patch-respect-pwsh-cd-behavior

Conversation

@chawyehsu
Copy link

This patch enables the capability of navigating to the current user's home directory with z when cwd is on a PSDrive that does not have a definition of the HOME location.

Context: The fact that ~ can be counterintuitive on PowerShell, as it's relative to the cwd, **and its value is defined with a .Home property of cwd's PSProvider[1]. Not all PSProviders however define this property, leaving that sometimes you cannot navigate to the undefined HOME location with ~.

To validate this, you can use Get-PSDrive to iterate your PSDrives and see which providers define a Home property. On Windows,

Get-PSDrive C,HKLM | select Name,{$_.Provider.Name},{$_.Provider.Home}

Name $_.Provider.Name $_.Provider.Home
---- ---------------- ----------------
C    FileSystem       C:\Users\<username>
HKLM Registry

or on Linux/macOS,

Get-PSDrive /,Function | select Name,{$_.Provider.Name},{$_.Provider.Home}

Name     $_.Provider.Name $_.Provider.Home
----     ---------------- ----------------
/        FileSystem       /Users/<username>
Function Function

When cwd is on a PSDrive without a defined HOME location, the original Set-Location and alias cd in PowerShell handles this case by falling back to the user's home directory when no argument is provided[2], and when ~ is explicitly provided, it results in an error.

PS C:\> Set-Location Function:
PS Function:\> cd ~
Home location for this provider is not set. To set the home location, call "(get-psprovider 'Function').Home = 'path'".
PS Function:\> cd
PS C:\Users\<username>\>

This patch implements the same behavior for z by handling z ~ and z separately.

[1]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_providers
[2]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-location?#-path

Zouziszzm added a commit to Zouziszzm/zoxide that referenced this pull request Jan 27, 2026
fixes ajeetdsouza#1161

Before: When you used the fzf selector, paths in your home directory showed like this:

/home/yourname/Documents
/home/yourname/Downloads
/home/yourname/projects/foo
After: Now they show like this:

~/Documents
~/Downloads
~/projects/foo
How I did it:

When displaying paths → replace your home directory part with ~
When you select a path → convert ~ back to the full path before using it
So it's just cleaner to read in the fzf menu, but everything still works the same way behind the scenes.
@chawyehsu
Copy link
Author

@ajeetdsouza thoughts on this?

This patch enables the capability of navigating to the current user's home
directory with `z` when cwd is on a PSDrive that does not have a definition
of the `HOME` location.

Context: The fact that `~` can be counterintuitive on PowerShell, as it's
relative to the cwd, **and its value is defined with a `.Home` property of
cwd's PSProvider\[1]. Not all PSProviders however define this property, leaving
that sometimes you cannot navigate to the undefined `HOME` location with `~`.

To validate this, you can use `Get-PSDrive` to iterate your PSDrives and see
which providers define a `Home` property. On Windows,

```plain
Get-PSDrive C,HKLM | select Name,{$_.Provider.Name},{$_.Provider.Home}

Name $_.Provider.Name $_.Provider.Home
---- ---------------- ----------------
C    FileSystem       C:\Users\<username>
HKLM Registry
```

or on Linux/macOS,

```plain
Get-PSDrive /,Function | select Name,{$_.Provider.Name},{$_.Provider.Home}

Name     $_.Provider.Name $_.Provider.Home
----     ---------------- ----------------
/        FileSystem       /Users/<username>
Function Function
```

When cwd is on a PSDrive without a defined `HOME` location, the original
`Set-Location` alias `cd` in PowerShell handles this case by falling back
to the user's home directory when no argument is provided\[2], and when `~`
is explicitly provided, it results in an error.

```plain
PS C:\> Set-Location Function:
PS Function:\> cd ~
Home location for this provider is not set. To set the home location, call "(get-psprovider 'Function').Home = 'path'".
PS Function:\> cd
PS C:\Users\<username>\>
```

This patch implements the same behavior for `z` by handling `z ~` and `z` separately.

\[1]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_providers
\[2]: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/set-location?#-path

Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>
@chawyehsu chawyehsu force-pushed the patch-respect-pwsh-cd-behavior branch from a1ebcd8 to 976a721 Compare February 6, 2026 02:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant