17

zsh forward-word 与 bash/emacs 有点不同,我想改变它。

我不描述所有差异,而是一步一步地向您展示 bash 的行为。我将光标标记为“^”符号。

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

兆字节

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

兆字节

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

兆字节

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

兆字节

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

兆字节

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

兆字节

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

MF

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

医学博士

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

医学博士

foo bar^-param --needed-param

医学博士

foo bar^ --needed-param

该算法既可以灵活地在单词中移动,也可以为我删除部分单词。它也在emacs中,所以我已经习惯了。我也想在 zsh 中看到它。谢谢。

4

2 回答 2

26

为了这个目的,我有这个.zshrc

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

编辑:啊,我记得为了让一切按我的意愿工作而缺少什么。我还forward-word-match通过将以下内容放入$ZDOTDIR/functions/forward-word-match(假设您的$ZDOTDIR/functions目录位于 中$fpath;否则将其放入其中或修改$fpath数组)中进行了覆盖:

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
于 2012-06-02T07:30:53.400 回答
3

要使用 Emacs 键绑定获得所需的行为,我所要做的就是:

bindkey '\ef' emacs-forward-word

Moriz Bunkus 回答的这一部分现在似乎也可以自行工作:

autoload -U select-word-style
select-word-style bash

在某些边缘情况下它似乎表现得更好(例如,它被视为?单词边界,而上述情况则没有。)

于 2020-03-08T12:53:34.410 回答