Partially delete path in zsh (Option + Backspace)

terminalzsh

In zsh Option + Backspace deletes the whole path on a prompt. For example:

ls /usr/local/bin

Option + Backspace returns:

ls

How can I set the same behavior so that the shortcut deletes just one word, till the next delimiter, like:

ls /usr/local/bin

Option + Backspace should return:

ls /usr/local/

Best Answer

An arbitrary number of alphabetic characters and digits as well as chars defined by WORDCHARS are considered all one word in zsh.

The default value of WORDCHARS is:

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

By defining another/a new WORDCHARS in .zprofile (and/or .zshrc|... - depends on how you configured your zsh environment), you can modify the behaviour of werase (word erase - usually ctrl/^w).

Simply add the line export WORDCHARS='*?_-.[]~=&;!#$%^(){}<>' in .zprofile|..., close the Terminal window or source .zprofile|... and werase works as you want afterwards.

Keep in mind that other shortcuts (like alt/⌥) will also be affected then.


If you want a different behaviour, check this question & answer: zsh: using different WORDCHARS for kill-word and forward-word / backward-word