Search code examples
bashzshzsh-completion

Word forward/backward delimiter difference between bash and zsh


Having been dragged into the 21st century by macOS Catalina now defaulting to zsh instead of bash for its shell, I'm trying to restore some of the bash's line editing behavior. Specifically, if I am at the end of the line:

bash$ ls /foo/bar/baz

and I hit Alt-Left Arrow, the cursor jumps to "baz", then "bar", then "foo". It is treating the slash as a word delimiter, as do most environments (such as Safari and apps that use Cocoa text editing features).

In zsh, when I try to do the same thing, the first Alt-Left Arrow jumps the cursor all the way back to the beginning of "/foo/bar/baz", which makes it much less useful for editing parts of a long directly.

Can this be configured in zsh? If so, how?


Solution

  • Characters in zsh variable WORDCHARS are considered part of a word.

    You just need to remove / from it

    % echo "$WORDCHARS"
    *?_-.[]~=/&;!#$%^(){}<>
    % WORDCHARS="${WORDCHARS/\//}"
    # Now Alt-left-arrow will stop at /
    

    Just be aware that $WORDCHARS is used in other situations as well. See man zshall