How to make zsh forward-word behaviour same as in bash/emacs

梦想的初衷 提交于 2019-12-05 13:05:42

问题


zsh forward-word acts a bit different from bash/emacs, and I'd like to change that.

Instead of description of all differences, let me just show you step by step behaviour of bash. I marked cursor as "^" symbol.

foo bar --non-needed-param --needed-param^

M-b

foo bar --non-needed-param --needed-^param

M-b

foo bar --non-needed-param --^needed-param

M-b

foo bar --non-needed-^param --needed-param

M-b

foo bar --non-^needed-param --needed-param

M-b

foo bar --^non-needed-param --needed-param

M-b

foo ^bar --non-needed-param --needed-param

M-f

foo bar^ --non-needed-param --needed-param

M-d

foo bar^-needed-param --needed-param

M-d

foo bar^-param --needed-param

M-d

foo bar^ --needed-param

This algorithm is both flexible for moving through words, removing parts of them for me. Also it's in emacs, so I'm used to it. I'd like to see it in zsh too. Thanks.


回答1:


I have this in my .zshrc for exactly that purpose:

# Bash-like navigation
autoload -U select-word-style
select-word-style bash

Edit: Ah, I remember what was missing in order to get everything working as I wanted to. I also overwrote forward-word-match by putting the following content into $ZDOTDIR/functions/forward-word-match (assuming your $ZDOTDIR/functions directory is in $fpath; otherwise put it into one or modify the $fpath array as well):

emulate -L zsh
setopt extendedglob

autoload match-words-by-style

local curcontext=":zle:$WIDGET" word
local -a matched_words
integer count=${NUMERIC:-1}

if (( count < 0 )); then
    (( NUMERIC = -count ))
    zle ${WIDGET/forward/backward}
    return
fi

while (( count-- )); do

    match-words-by-style

    # For some reason forward-word doesn't work like the other word
    # commands; it skips whitespace only after any matched word
    # characters.

    if [[ -n $matched_words[4] ]]; then
        # just skip the whitespace and the following word
  word=$matched_words[4]$matched_words[5]
    else
        # skip the word but not the trailing whitespace
  word=$matched_words[5]
    fi

    if [[ -n $word ]]; then
  (( CURSOR += ${#word} ))
    else
  return 1
    fi
done

return 0


来源:https://stackoverflow.com/questions/10847255/how-to-make-zsh-forward-word-behaviour-same-as-in-bash-emacs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!