6

When I don't include / in WORDCHARS, ie:

WORDCHARS='~!#$%^&*(){}[]<>?.+;-'

then kill-word works as desired: with Alt+d, I can delete parts of filesystem paths (stopping at each /).

But when using Ctrl+Right and Ctrl+Left, I would like to have different behaviour: I don't want the cursor to stop at each /. I want to skip the whole path as one word.

Can I use different WORDCHARS only for forward-word and backward-word ?

1
8

You could bind the vi-{back,for}ward-blank-word widgets to Ctrl+{left,right}`:

bindkey $'\E[1;5D' vi-backward-blank-word
bindkey $'\E[1;5C' vi-forward-blank-word

or redefine the {back,for}ward-word widgets to use a different WORDCHARS (so it would affect other bindings (like Alt+{B,F}) that use those widgets):

WORDCHARS='~!#$%^&*(){}[]<>?.+;-'
MOTION_WORDCHARS='~!#$%^&*(){}[]<>?.+;-/'

''{back,for}ward-word() WORDCHARS=$MOTION_WORDCHARS zle .$WIDGET
zle -N backward-word
zle -N forward-word

See also info zsh select-word-style for a framework to dynamically select word styles.

You can also use it to select different word styles for different widgets like:

autoload -U select-word-style
zle -N select-word-style
bindkey '\ez' select-word-style

WORDCHARS='~!#$%^&*(){}[]<>?.+;-' # for the "normal" style
select-word-style normal
zstyle ':zle:*ward-word' word-style space
zstyle ':zle:backward-kill-word' word-style space

You'd get the "space"-based word style for the *ward-word and backward-kill-word widgets and whatever is selected by Alt+Z, initially "normal", $WORDCHARS-based word style for the other word widgets.

7
  • vi-{back,for}ward-blank-word is the perfect solution. Thanks! Aug 24 '19 at 7:55
  • I would like Ctrl+w to behave same as Ctrl+Right, (ie not stop at /). I tried adding bindkey '^W' vi-backward-kill-word, but that did not help. Aug 25 '19 at 9:57
  • In other words, vi-backward-kill-word behaves same as backward-kill-word. Aug 25 '19 at 10:03
  • @400theCat, yes there's no vi-backward-kill-blank-word as there's no such concept in vi (as opposed to the B/E/W motions in vi). You can use the second approach. Aug 25 '19 at 11:03
  • can I define MOTION_WORDCHARS to contain every character except space ? I could enumerate all ASCII characters, but what will happen if some unicode character appears on the commandline / Aug 25 '19 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.