Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Make `Ctrl + W` and `Ctrl + Shift + H` in zsh behave the same as in bash



Rik wrote on Sat, 21 Mar 2020 14:29 -0500:
> I've recently started using zsh and I like it. However, coming from 
> bash, some little things I miss.
> 

Welcome!

> *The problem:*
> 
> In bash behavior is like this:
> 
>   * Ctrl + W deletes the word behind the cursor up to the next space
>   * Ctrl + Shift + H deletes the word behind the cursor up to the next
>     seperation charcater like ., ,, -, / etc.
> 
> In zsh both Ctrl + W an Ctrl + Shift + H behave like the latter one in 
> bash. I would like the same behavior as in bash.
> 
> 
> 
> *This is what I've tried:*
> 
>     SPACE_WORDCHARS='~!#$%^&*(){}[]<>?.+;-_/\|=@`'  
>     backward-delete-word() WORDCHARS=$SPACE_WORDCHARS zle .$WIDGET
>     zle -N backward-delete-word
>     bindkey "^W" backward-delete-word
> 
> This works, however, it breaks the functionality that deleting a word 
> puts the word on the paste buffer, so I can't then paste this word with 
> Ctrl + Y. This is quite important functionality for me. To be honest I'm 
> not completely sure how this zle function works and what .$WIDGET does. 
> Would anyone know a way how I can make this work while retaining the 
> cut/paste behavior?

Deleting those four lines and adding just «WORDCHARS='~!#$%^&*(){}[]<>?.+;-_/\|=@`'»
instead seems to do what you want.

(I also tried calling «zle -f kill» in the wrapper but it didn't have
the desired effect.)

Regarding $WIDGET, it's a parameter that gets predefined by zle when
widget functions are invoked.  In the example, its value will be
"backward-delete-char".  Thus, net effect of «zle .$WIDGET» will be to
call the builtin "backward-delete-char" widget.  For a simpler example,
consider:

mywidget() { LBUFFER+="x" }
zle -N mywidget
bindkey "y" mywidget

With this, every time you press "y", you'll get an "x" inserted.  (You
can just paste this example at the prompt to try it.)

Cheers,

Daniel



Messages sorted by: Reverse Date, Date, Thread, Author