Bash behaviour

I've just migrated from bash to zsh. In bash, I had the following line in ~/.inputrc.

"\e\C-?": unix-filename-rubout

Hence, Alt+Backspace would delete back to the previous slash, which was useful for changing entered paths.

In addition, bash defaults to making Ctrl+w delete to the previous space, which is useful for deleting whole arguments (presuming they don't have spaces).

Zsh behaviour

In zsh, both Alt+Backspace and Ctrl+w do the same thing. They both delete the previous word, but they are too liberal with what constitutes a word-break, deleting up to the previous - or _. Is there a way to make zsh behave similarly to bash? If it's important, I have oh-my-zsh installed.

  • 1
    Do you want to change zsh's definition of what constitutes a word? If so: unix.stackexchange.com/questions/48577/… Or do you want to define a new command with a different definition of word? – Gilles Jan 30 '16 at 13:22
  • @Gilles I guess a combination of both. In bash, I'd use the two distinct commands regularly: i.e. delete to the last / and delete to the last ` `. They have different functions. – Sparhawk Jan 30 '16 at 13:24
  • 1
    @Gilles So yes, that link works for my bash-like Alt+Backspace behaviour, but I'd still like the (different) bash-like Ctrl+w behaviour, i.e. delete to last space. – Sparhawk Jan 30 '16 at 13:35

Edit: The next google result after your question was this one with same solution : zsh: make ALT+BACKSPACE stop at non-alphanumeric characters

This answer was provided by /nick FoH from #zsh on freenode.

backward-kill-dir () {
    zle backward-kill-word
zle -N backward-kill-dir
bindkey '^[^?' backward-kill-dir

This way you can use ctrl+w for deleting a Word (in vim lingo) and alt+bkspc to delete a word

  • Brilliant! Thanks for taking the time to investigate and answer. – Sparhawk Oct 30 '16 at 10:00
  • Re Google: except I asked my question nine months ago, and they asked theirs less than a month ago. – Sparhawk Oct 30 '16 at 10:06

A similar question was asked here: zsh: stop backward-kill-word on directory delimiter

and a workable solution given: add these settings to your zshrc:

autoload -U select-word-style
select-word-style bash
  • Thanks for the answer. However, none of those answers work for me. (Specifically, the one you quoted makes no change.) Possibly, it's because I have oh-my-zsh installed. In any case, this doesn't seem to address the two different functions and key bindings as per my question. (And if my question were it dupe it should be closed as a dupe. Which it is not.) – Sparhawk Jan 30 '16 at 3:15
  • You need to tweak WORDCHARS variable. In zsh it's easy vared WORDCHARS – Jshura Jan 30 '16 at 4:34
  • It would only be a duplicate if we matched it against an answered question in this forum. – Thomas Dickey Jan 30 '16 at 12:19
  • @ThomasDickey Oh yes, apologies, I didn't notice I'd swapped SE sites. – Sparhawk Jan 30 '16 at 13:24
  • @Jshura No, as per the question, I'd like two different functions, which is possible in bash. – Sparhawk Jan 30 '16 at 13:28

Expanding on JunkMechanic's answer, I wanted that

  • existing zsh shortcuts (CtrlW, Ctrl and Ctrl) works as in zsh defaults
  • Alt-based shortcuts (AltW, Alt and Alt) work similarly, but "finer grained", e.g. up to the closest /

Here's what I use now:

# Alt+Backspace
backward-kill-dir () {
    zle backward-kill-word
zle -N backward-kill-dir
bindkey '^[^?' backward-kill-dir

# Alt+Left
backward-word-dir () {
    zle backward-word
zle -N backward-word-dir
bindkey "^[[1;3C" forward-word-dir

# Alt+Right
forward-word-dir () {
    zle forward-word
zle -N forward-word-dir
bindkey "^[[1;3D" backward-word-dir

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.