ZSH 跳转到单词末尾

ZSH 跳转到单词末尾

我已经成功定制了我的,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,存在一些极端情况,如果光标位于大量非单词字符的右侧,直觉上可能会感觉它跳得有点太远。但对于您上面提出的测试用例,它完全正确。


如需更好的文字移动和自定义选项,请查看我的插件Zsh 编辑

相关内容