zoxide/templates
Chawye Hsu f07a76e115
pwsh: handling `z ~` and `z` separately (#1161)
* pwsh: handling `z ~` and `z` separately

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>

* No explicit case needed for ~

---------

Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>
Co-authored-by: Ajeet D'Souza <98ajeet@gmail.com>
2026-03-01 12:43:50 +05:30
..
bash.txt Support symlinks on Windows (#1149) 2026-02-02 11:07:36 +05:30
elvish.txt fix(elvish): before-chdir should assign to oldpwd upvalue (#818) 2024-05-29 15:06:28 +05:30
fish.txt Support symlinks on Windows (#1149) 2026-02-02 11:07:36 +05:30
nushell.txt nushell: use sigil operator (#1070) 2025-05-31 04:53:54 +05:30
posix.txt Support symlinks on Windows (#1149) 2026-02-02 11:07:36 +05:30
powershell.txt pwsh: handling `z ~` and `z` separately (#1161) 2026-03-01 12:43:50 +05:30
tcsh.txt csh -> tcsh 2025-05-06 00:26:07 -07:00
xonsh.txt Add support for csh (#1047) 2025-05-06 11:28:54 +05:30
zsh.txt Support symlinks on Windows (#1149) 2026-02-02 11:07:36 +05:30