Bash behaviour
I’ve just migrated from bash to zsh. In bash, I had the following line in ~/.inputrc.
"eC-?": unix-filename-rubout
Hence, Alt+Backspace would delete back to the previous slash, which was useful for editing paths.
Separately, bash defaults to making Ctrl+w delete to the previous space, which is useful for deleting whole arguments (presuming they don’t have spaces). Hence, there two slightly different actions performed with each key combination.
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, with two independent actions? If it’s important, I have oh-my-zsh installed.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
Method 2
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 () {
local WORDCHARS=${WORDCHARS//}
zle backward-kill-word
zle -f kill
}
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
Method 3
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 () {
local WORDCHARS=${WORDCHARS//}
zle backward-kill-word
zle -f kill # Ensures that after repeated backward-kill-dir, Ctrl+Y will restore all of them.
}
zle -N backward-kill-dir
bindkey '^[^?' backward-kill-dir
# Alt+Left
backward-word-dir () {
local WORDCHARS=${WORDCHARS//}
zle backward-word
}
zle -N backward-word-dir
bindkey "^[[1;3D" backward-word-dir
# Alt+Right
forward-word-dir () {
local WORDCHARS=${WORDCHARS//}
zle forward-word
}
zle -N forward-word-dir
bindkey "^[[1;3C" forward-word-dir
For details on the zle -f kill, see Zsh – pasting text killed in a custom widget only works for last word, can that be fixed?
Method 4
I achieved your desired result by sticking the following in $ZDOTDIR/.zshrc:
# Use bash-like word definitions for navigation and operations
autoload -Uz select-word-style
select-word-style bash
# Use C-w to kill back to the previous space
zle -N backward-kill-space-word backward-kill-word-match
zstyle :zle:backward-kill-space-word word-style space
bindkey '^W' backward-kill-space-word
This has an advantage over other answers by not limiting the target delimiter of Alt+Backspace to /; for example, it will delete back to -, as well. Still, Ctrl+w will delete back to the previous space. This is more bash-like than other answers.
I am using zsh 5.8 on macOS, without oh-my-zsh or any other configuration framework.
Bonus: The following makes zsh even more bash-like:
# Use C-u to kill to the beginning of the line
bindkey '^U' backward-kill-line
# Use C-x C-e to edit the current command line in $VISUAL
autoload -Uz edit-command-line
zle -N edit-command-line
bindkey '^X^E' edit-command-line
Method 5
Just wanted to add this in nh2’s answer
- alt + left/right arrow to move
- alt + delete/w for deleting word forward/backward
# Alt+Delete
forward-kill-dir () {
local WORDCHARS=${WORDCHARS//}
zle kill-word
}
zle -N forward-kill-dir
bindkey '^[[3;3~' forward-kill-dir
# Alt+w
backward-kill-dir () {
local WORDCHARS=${WORDCHARS//}
zle backward-kill-word
}
zle -N backward-kill-dir
bindkey '^[w' backward-kill-dir
# Alt+Left
backward-word-dir () {
local WORDCHARS=${WORDCHARS//}
zle backward-word
}
zle -N backward-word-dir
bindkey "^[[1;3D" backward-word-dir
# Alt+Right
forward-word-dir () {
local WORDCHARS=${WORDCHARS//}
zle forward-word
}
zle -N forward-word-dir
bindkey "^[[1;3C" forward-word-dir
Method 6
bindkey '^[^?' vi-backward-kill-word
As far as I can tell, its effect on Meta-BackSpace is the same as with the answer suggested by @thomas-dickey, and it’s a oneliner rather than two ;-). However, this only affects Meta-BS. If you are looking for the full bash-like behavior with Meta-(Left/Right) navigation then select-word-style is the right way.
(Footnote: the select-word-style-thing is documented in man zshcontrib but they are all but incomprehensible to me. vi-backward-kill-word is documented in man zshzle.)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0