Skip to content

PowerShell --use-on-cd enhancements#1535

Open
ravindUwU wants to merge 3 commits intoSchniz:masterfrom
ravindUwU:pwsh-use-on-cd-enhancements
Open

PowerShell --use-on-cd enhancements#1535
ravindUwU wants to merge 3 commits intoSchniz:masterfrom
ravindUwU:pwsh-use-on-cd-enhancements

Conversation

@ravindUwU
Copy link
Copy Markdown

This PR includes the following changes for the fnm env --use-on-cd script emitted for PowerShell:

Demo

With the following PowerShell profile,

# customCd prints its args to cd before continuing with Set-Location.
function customCd { Write-Host "customCd: [$args]" ; Set-Location @args }
Set-Alias -Option AllScope -Scope Global 'cd' 'customCd'

fnm env --shell powershell --use-on-cd | Out-String | Invoke-Expression

Notice how, on PowerShell 5 & 7,

  • cd-ing uses FNM on cd, and delegates to the customCd function.
  • Parameters of the underlying cd function (in this case the -PassThru option of Set-Location) can be specified.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 21, 2026

⚠️ No Changeset found

Latest commit: d8385d9

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link
Copy Markdown

vercel bot commented Mar 21, 2026

@ravindUwU is attempting to deploy a commit to the Gal Schlezinger's projects Team on Vercel.

A member of the Team first needs to authorize it.

@ravindUwU
Copy link
Copy Markdown
Author

ravindUwU commented Mar 27, 2026

What's emitted now is 😄:

function global:Set-FnmOnLoad { ... }
& {
	$cd = (Get-Command 'cd' -ErrorAction Ignore)
	if (-not $cd) { $cd = 'Set-Location' }
	Set-Item Function:\global:Set-LocationWithFnm { & $cd @args; Set-FnmOnLoad }.GetNewClosure()
}
Set-Alias -Scope global cd_with_fnm Set-LocationWithFnm
Set-Alias -Option AllScope -Scope global cd Set-LocationWithFnm

The cd command before FNM is captured as $cd within the & { ... } scope, and Set-LocationWithFnm is made within and bound to that scope. Thoughts, @HeyItsGilbert? 😅

Re: cd_with_fnm, thoughts on removing this alias? 🤔 It could be useful if a different tool "resets" cd (as FNM did before); but that behaviour should be considered a bug with the other tool and fixed similarly, IMO.

@ravindUwU ravindUwU force-pushed the pwsh-use-on-cd-enhancements branch from ae3c405 to d8385d9 Compare March 27, 2026 23:50
@HeyItsGilbert
Copy link
Copy Markdown

Looks good to me. I agree that you could remove the alias.

Realistically what I'm going to probably end up doing is figuring out what zoxide is doing and merging that and the Set-FnmOnLoad command into my custom one.

@ravindUwU
Copy link
Copy Markdown
Author

Alternatively, I think Set-LocationWithFnm could be removed, and cd_with_fnm made a function; like,

function global:Set-FnmOnLoad { ... }
& {
	$cd = (Get-Command 'cd' -ErrorAction Ignore)
	if (-not $cd) { $cd = 'Set-Location' }
	Set-Item Function:\global:cd_with_fnm { & $cd @args; Set-FnmOnLoad }.GetNewClosure()
}
Set-Alias -Option AllScope -Scope global cd cd_with_fnm

...in fact I think I prefer this! The name cd_with_fnm clarifies that the underlying command is cd; whereas Set-LocationWithFnm is now a bit misleading in that it actually invokes cd instead of Set-Location 🤔.

A search for lang:PowerShell /Set-LocationWithFnm|cd_with_fnm/ shows a few scripts that reference both of these so there might be some breakage if either is removed.

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.

Use existing cd alias in the script that fnm env --shell powershell --use-on-cd emits

2 participants