From 249a6df4a489dacd28e2c50395909571671ff707 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 31 May 2026 12:34:58 +0900 Subject: [PATCH] [shell][zsh] Don't resolve symlinks in ALT-c (#4816) (#4817) This way ALT-c behaves more aligned with `cd`. Imagine a setup like: ``` /foo -> foo_real /foo_real/bar ``` Right now if we first `cd foo` (a symlink to `foo_real`), and then use ALT-c to goto `bar`, then we would end up executing `cd /foo_real/bar` instead of `cd /foo/bar`. `$PWD = /foo_real/bar`. For comparison, if we first `cd foo` and then `cd bar`, we end up with `$PWD = /foo/bar`. This commit changes the internal logic of `fzf-cd-widget` to first run `cd ` in a subshell to simulate the behavior of `cd`, and then insert the target PWD into the shell history. This way we get behavior consistent with the builtin `cd` command, while also recording reusable shell history. Co-authored-by: Yi-Yo Chiang <5255547+silverneko@users.noreply.github.com> Close #4816 --- shell/key-bindings.zsh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/shell/key-bindings.zsh b/shell/key-bindings.zsh index a14c7132..bd2e4378 100644 --- a/shell/key-bindings.zsh +++ b/shell/key-bindings.zsh @@ -110,8 +110,14 @@ fzf-cd-widget() { zle redisplay return 0 fi + # Use subshell expansion to get the absolute PWD of the target dir. + # This allows the recorded shell history to be reused even from a different + # working directory. + # If failed, fallback to the unexpanded path to surface the error to the user. + # NOTE: Don't use the `:a` modifier as it resolves symlinks like `pwd -P`. + dir=$(builtin cd >/dev/null -- "${dir}" && echo "${PWD}" || echo "${dir}") zle push-line # Clear buffer. Auto-restored on next prompt. - BUFFER="builtin cd -- ${(q)dir:a}" + BUFFER="builtin cd -- ${(q)dir}" zle accept-line local ret=$? unset dir # ensure this doesn't end up appearing in prompt expansion