zle backward-char 没有按预期工作

zle backward-char not working as expected

我正在编写一个简单的 ZLE 小部件以使用 <C-j> 快速创建子外壳。这是我拥有的:

function zle_subshell {
  zle -U '$()'
  zle .backward-char
}

# register as widget
zle -N zle_subshell

# create kbd
bindkey '^j' zle_subshell

但是,zle .backward-char 似乎不起作用。更令人困惑的是,如果我将脚本修改为:

function zle_subshell {
  zle -U '$('
  zle -U ')'
  zle .backward-char
}

我得到类似 )$(...

的输出

zle_subshell 函数似乎正在反向求值。 ZLE 小部件是否有一些我不知道的明显问题?

zle -U 用法是特例。似乎该行为是有意的:

zle -U string
...
This pushes the characters in the string onto the input stack of ZLE. After the widget currently executed finishes ZLE will behave as if the characters in the string were typed by the user.

As ZLE uses a stack, if this option is used repeatedly the last string pushed onto the stack will be processed first. However, the characters in each string will be processed in the order in which they appear in the string.

-- zshzle(1), ZLE BUILTINS, zle -U

因此,zsh 的行为就像在 zle_subshell 完成后键入 )$(

我们可以修改 (R)BUFFER 来直接更改编辑器缓冲区,如下所示:

function zle_subshell {
  RBUFFER='$()'"$RBUFFER"
  repeat 2 do zle .forward-char; done
  # ((CURSOR=CURSOR+2)) # We could do this instead.
}