
我已經成功地定制了我的zsh
處理WORDCHARS
我想要的權利。但是,當跳躍、刪除或自動完成單字時,它會跳到每個單字的開頭,我更喜歡它跳到單字的末尾。
例如下面的命令中,使用backward-word
和跳躍時forward-word
,遊標會跳到^
以下字元標記的位置:
$ kubectl --context stage --namespace kube-system get pods
^ ^ ^ ^ ^ ^ ^ ^ ^
我更希望 zsh 跳到單字末尾,而不是像這樣:
$ kubectl --context stage --namespace kube-system get pods
^ ^ ^ ^ ^ ^ ^ ^
這樣可以配置嗎?
答案1
幹得好:
# Replace `forward-word` with `emacs-forward-word`. Problem solved.
zle -A emacs-forward-word forward-word
# For `backward-word`, it's a bit more complex. We'll have to
# create a new widget for this.
zle -N backward-word backward-word-end
backward-word-end() {
# Move to the beginning of the current word.
zle .backward-word
# If we're at the beginning of the buffer, we don't need to do
# anything else.
(( CURSOR )) ||
return
# Otherwise, move to the end of the word before the current one.
zle .backward-word
zle .emacs-forward-word
}
對於這個版本的backward-word
,存在一些邊緣情況,如果遊標位於大量非單字字符的右側,直觀上可能會感覺跳得太遠了。但對於您上面介紹的測試案例,它的工作原理完全正確。
要獲得更好的文字移動和自訂選項,請查看我的插件編輯。