Zsh bindkey 映射 Ctrl+c 为 Ctrl+p

Zsh bindkey 映射 Ctrl+c 为 Ctrl+p

我知道如何映射 CTRL+L 来运行“ls”等命令

bindkey -s "^L" 'ls^M'

但是如何在我的 ZSH 中将 CTRL+c 映射到另一个按键,例如 CTRL+p?

或者杀死当前正在运行的进程的命令是什么?

答案1

^C是个打断默认情况下系统终端驱动程序的字符,您按下的字符可以中止命令或长时间运行的完成等。shell 不参与其中。

您需要将其绑定到其他东西,以便 zsh 在其自己的行编辑器中的绑定有效。否则,当您按+时,zle永远不会看到字符出现,因为系统会拦截该字符以生成 SIGINT 信号。^CCtrlc

stty intr '^G'
bindkey -s '^C' whatever

或者:

bindkey '^C' up-line-or-history

绑定与模式^P中默认绑定的相同小部件。emacs

要仅在运行时^G成为角色,您可以执行以下操作:intrzle

autoload add-zle-hook-widget
intr-is-cC() stty intr '^C' < /dev/tty
intr-is-cG() stty intr '^G' < /dev/tty
zle -N intr-is-cC
zle -N intr-is-cG
add-zle-hook-widget line-init intr-is-cG
add-zle-hook-widget line-finish intr-is-cC

bindkey -s '^C' whatever

其中分别intr设置为^Gzle^C开始时(发出提示后)和结束时(离开提示时)。

相关内容