Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make zsh forward-word behaviour same as in bash/emacs

Tags:

zsh

zshrc

zsh forward-word acts a bit different from bash/emacs, and I'd like to change that.

Instead of description of all differences, let me just show you step by step behaviour of bash. I marked cursor as "^" symbol.

foo bar --non-needed-param --needed-param^

M-b

foo bar --non-needed-param --needed-^param

M-b

foo bar --non-needed-param --^needed-param

M-b

foo bar --non-needed-^param --needed-param

M-b

foo bar --non-^needed-param --needed-param

M-b

foo bar --^non-needed-param --needed-param

M-b

foo ^bar --non-needed-param --needed-param

M-f

foo bar^ --non-needed-param --needed-param

M-d

foo bar^-needed-param --needed-param

M-d

foo bar^-param --needed-param

M-d

foo bar^ --needed-param

This algorithm is both flexible for moving through words, removing parts of them for me. Also it's in emacs, so I'm used to it. I'd like to see it in zsh too. Thanks.

like image 322
Konstantine Rybnikov Avatar asked Jun 01 '12 08:06

Konstantine Rybnikov


1 Answers

I have this in my .zshrc for exactly that purpose:

# Bash-like navigation
autoload -U select-word-style
select-word-style bash

Edit: Ah, I remember what was missing in order to get everything working as I wanted to. I also overwrote forward-word-match by putting the following content into $ZDOTDIR/functions/forward-word-match (assuming your $ZDOTDIR/functions directory is in $fpath; otherwise put it into one or modify the $fpath array as well):

emulate -L zsh
setopt extendedglob

autoload match-words-by-style

local curcontext=":zle:$WIDGET" word
local -a matched_words
integer count=${NUMERIC:-1}

if (( count < 0 )); then
    (( NUMERIC = -count ))
    zle ${WIDGET/forward/backward}
    return
fi

while (( count-- )); do

    match-words-by-style

    # For some reason forward-word doesn't work like the other word
    # commands; it skips whitespace only after any matched word
    # characters.

    if [[ -n $matched_words[4] ]]; then
        # just skip the whitespace and the following word
  word=$matched_words[4]$matched_words[5]
    else
        # skip the word but not the trailing whitespace
  word=$matched_words[5]
    fi

    if [[ -n $word ]]; then
  (( CURSOR += ${#word} ))
    else
  return 1
    fi
done

return 0
like image 71
Moritz Bunkus Avatar answered Sep 27 '22 20:09

Moritz Bunkus