zsh:运行 zle 小部件后刷新提示

zsh: refresh prompt after running zle widget

我定义了以下小部件

function cdd()
{
    cd /
}
zle -N cdd{,}
bindkey "^R" cdd

按下组合键后,cwd 已更改,但终端提示未更新。例如,执行此操作后 ()

~/tmp/todelete$ | # press key ^R  here; "~$" is the prompt; "|" denotes cursor

终端完全保持不变。如果我然后键入 ls -ld .,它会显示

~/tmp/todelete$ ls -ld .
dr-xr-xr-x 23 root root 4096 Sep 14 07:52 ./

/$ |

这意味着 ll 时的 cwd 是 运行 已经是 /.

这很混乱,可能会导致严重的错误。 (例如,如果按 ^R 后我被打扰离开我的办公桌然后回来,我可能会忘记我做了什么)

如何让终端在按键后重新绘制提示?是否有 zle 函数来执行此操作?

reset-prompt 可以拯救:

function cdd()
{
    cd /
    zle reset-prompt # XXX: added
}

reset-prompt

Force the prompts on both the left and right of the screen to be re-expanded, then redisplay the edit buffer. This reflects changes both to the prompt variables themselves and changes in the expansion of the values (for example, changes in time or directory, or changes to the value of variables referred to by the prompt).

Otherwise, the prompt is only expanded each time zle starts, and when the display as been interrupted by output from another part of the shell (such as a job notification) which causes the command line to be reprinted.

--- zshzle(1), reset-prompt, Miscellaneous, Widgets, zsh command line editor