I want to delete a word by Ctrl+W in zsh like.

vim /foo/bar^W
vim /foo/

And found a solution for bash, but bind is not in zsh function.

Is it possible to configure ctrl-w (delete word)?

How can I configure Ctrl+W as a delete-word?


Here's a snippet from .zshrc i've been using:

my-backward-delete-word() {
    local WORDCHARS=${WORDCHARS/\//}
    zle backward-delete-word
zle -N my-backward-delete-word
bindkey '^W' my-backward-delete-word

I recall this was the original source: http://www.zsh.org/mla/users/2001/msg00870.html

  • Thanks! I put this code in ~/.zshrc and it works perfectly! – ironsand Dec 21 '15 at 13:03
  • The default emacs key binding table in ZLE binds ^W to backward-kill-word rather than to backward-delete-word, note. – JdeBP Dec 21 '15 at 13:59

Just for your information, I found this solution here to be far more elegant. I quote:

Another option is to set WORDCHARS (non-alphanumeric chars treated as part of a word) to something that doesn't include /.

You can also tweak this if you'd prefer ^w to break on dot, underscore, etc. In ~/.zshrc I have:

  • This makes M-d less useful. – Toothrot Dec 27 '18 at 23:34
  • 1
    This somehow doesn't work anymore with zsh 5.7 (x86_64-apple-darwin18.2.0). – Patryk Feb 1 at 13:29

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.