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 編集

関連情報