我实际上已经问过这个问题超级用户和堆栈溢出。我正在使用 VSCode 并且有一个很好的行为:当我从行尾执行Alt+时,它会停止在→
foo/bar/test_wait_what
^ ^ ^ ^
当我执行Delete+时Alt,它会删除what
, then _
, thenwait
等等。
我想做或多或少相同的事情zsh
(没有“oh-my-zsh”,因为我已经在使用zimfw
)。开箱即用,它似乎不考虑_
作为单词分隔符,并且 for /
,它同时删除它。
我发现了一些类似的问题,他们建议使用select-word-style bash
,但 bash 在删除时没有我想要的行为。
另外,我发现很难找到有关zsh
、示例等的明确信息,因此,如果您对我如何自己找到答案有任何建议,请不要犹豫。
答案1
您可以删除_
and /
from $WORDCHARS
(或您不希望被视为单词一部分的任何其他字符)字)并定义一个小部件,删除要绑定的单词或非单词字符序列Alt+Del
delete-word-or-non-word() {
emulate -L zsh # restore default zsh options locally to the function
set -o extendedglob # extended glob needed for the ## operator (locally)
# ${var##pattern} ksh operator to remove the longest string that matches
# the pattern off the start of $var. Here applied to $RBUFFER which in a
# zle widget is the part of the line editor buffer to the right of the
# cursor. [[:WORD:]] matches a *word* character (alnums + $WORDCHARS),
# ## is *one or more* (like ERE's + or ksh's +(...))
RBUFFER=${RBUFFER##([[:WORD:]]##|[^[:WORD:]]##)}
}
zle -N delete-word-or-non-word # define a new zle widget using that function
# bind that new widget
bindkey '\ed' delete-word-or-non-word # Alt+D
bindkey '^[[3;3~' delete-word-or-non-word # Alt+Del on xterm at least
WORDCHARS=${WORDCHARS//[\/_]} # remove _ and / from WORDCHARS
# using the ${var//pattern/replacement} ksh
# operator
WORDCHARS= # or make it empty so that the only *word*
# characters are alphanumerics
(这不需要select-word-style
)