如何使 zsh 前向行为与 bash/emacs 相同 [英] How to make zsh forward-word behaviour same as in bash/emacs
问题描述
zsh forward-word 与 bash/emacs 有点不同,我想改变它.
zsh forward-word acts a bit different from bash/emacs, and I'd like to change that.
我不再描述所有差异,而是向您展示 bash 的逐步行为.我将光标标记为^"符号.
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
这个算法既可以灵活地移动单词,也可以为我删除部分单词.它也在 emacs 中,所以我已经习惯了.我也想在 zsh 中看到它.谢谢.
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.
推荐答案
我在我的 .zshrc
中有这个:
I have this in my .zshrc
for exactly that purpose:
# Bash-like navigation
autoload -U select-word-style
select-word-style bash
啊,我记得为了让一切按我想要的方式工作而缺少什么.我还通过将以下内容放入 $ZDOTDIR/functions/forward-word-match
(假设您的 $ZDOTDIR/functions
code> 目录在 $fpath
中;否则把它放在一个或修改 $fpath
数组):
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
这篇关于如何使 zsh 前向行为与 bash/emacs 相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!