I had the same problem, so I am so happy I found this, but I also have some additional context which might be helpful. I'm on Ubuntu, but these issues are shared across platforms. In the effort of appropriateness, I've substituted ctrl
with option
, since that's what it'd be on a Mac.
Quick echo
for reference -- zsh
defines non-alphanumeric characters often used in words, such as punctuation, hyphens, etc. as $WORDCHARS
(I did not set this variable):
❯ echo $WORDCHARS
*?_-.[]~=/&;!#$%^(){}<>
- Initially my
zsh
instance started with option-arrow
generating characters ;5D
and ;5C
for option-left
and option-right
, instead of skipping words as I expected. Commands I found that enabled skipping words based on direction were:
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
- However, I would've expected the cursor to stop on
$WORDCHARS
, but the cursor skipped these, as well. Finding the OP, I tried select-word-style
from the terminal since it has a cli interface, yet, unlike bindkey
, it doesn't appear to work as a command even after including autoload -U select-word-style
in .zshrc
, so I found the only combination that would enable the bash
word-style was including the following in my .zshrc
and restarting the terminal (or running zsh
):
# $HOME/.zshrc
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
# Bash-like navigation <-- Accepted solution
autoload -U select-word-style
select-word-style bash
- After a bit of troubleshooting, I discovered my bindkey -e argument was breaking the
select-word-style bash
behavior, but only if it's invoked before select-word-style
. Therefore, I added it after the select-word-style bash
and my cursor stops on $WORDCHARS
, as expected. At that point, the keybinding portion of my .zshrc
looked like this:
autoload -U select-word-style
select-word-style bash
bindkey -e
bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word
- I couldn't find a specific way to check to see if
bindkey -e
(emacs-style) or bindkey -v
(vi-style) is loaded, so I counted instances of vi
keybindings available, as one would expect emacs-mode
would have fewer vi-style
keybindings (right?):
# when first starting with `.zshrc` settings shown above:
❯ bindkey -e
❯ bindkey | grep vi | wc -l
5
# invoking `bindkey -v` manually:
❯ bindkey -v
❯ bindkey | grep vi | wc -l
14
# and back to `bindkey -e` again:
❯ bindkey -e
❯ bindkey | grep vi | wc -l
5
- Lastly, I had mentioned
select-word-style
has a cli interface - it actually has a menu, so in case you're curious to try other behaviors, here's its options. Oddly, it does appear to work from the cli as long as you've invoked it at least once from .zshrc
(spoiler: bash
style is best):
╭─ ~
╰─❯ select-word-style
Usage: select-word-style word-style
where word-style is one of the characters in parentheses:
(b)ash: Word characters are alphanumerics only
(n)ormal: Word characters are alphanumerics plus $WORDCHARS
(s)hell: Words are command arguments using shell syntax
(w)hitespace: Words are whitespace-delimited
(d)efault: Use default, no special handling (usually same as `n')
(q)uit: Quit without setting a new style