4

So I have a new Mac M1

I'm trying to set up the terminal as it was in my previous system.

The problem now is that when using option+arrow (left or right) it jumps to the next space character instead of the next special character.

I have set the keybindings to the "natural text editing" preset in iterm2.

In summary. This happens:

 /example/path/here       option->         /example/path/here
^                                                            ^
cursor                                                       cursor

What I want

 /example/path/here       option->         /example/path/here
^                                                  ^
cursor                                             cursor

2 Answers 2

2

So essentially my problem was with zsh. The default word separation behaviour is different there.

To fix it I just had to include this in my ~/.zshrc file:

# Bash-like navigation
autoload -U select-word-style
select-word-style bash
1
  • After reading a dozen questions, this helped me. Natural text editing was properly set and worked for me in the past, but this time i was struggling with navigating word by word in paths. thank you! Commented Dec 6, 2023 at 13:41
0

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
*?_-.[]~=/&;!#$%^(){}<>
  1. 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
  1. 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
  1. 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
  1. 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
  1. 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .