Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make zsh word navigation/deletion work like Vim

Tags:

text

zsh

I am using zsh and I want word navigation/deletion to work exactly how it does in Vim to suit my muscle memory.

In Vim, given the text foo ./bar baz-bob, each forward navigation starting from the first character plays out like so:

foo ./bar baz-bob
^   ^ ^   ^  ^^

In default zsh, it plays out like so:

foo ./bar baz-bob
^   ^     ^      ^

I have managed to achieve some success using WORDCHARS=${WORDCHARS//[\/-]}. As I understand it, this works by removing the / and - chars from WORDCHARS. WORDCHARS is a string of characters which are also part of a word.

foo ./bar baz-bob
^   ^ ^   ^   ^  ^

Note: I am aware of zsh's vi mode, but I would prefer to configure zsh's default mode to behave this way.

like image 713
Oliver Joseph Ash Avatar asked Nov 23 '16 10:11

Oliver Joseph Ash


1 Answers

For newest MacOS Sierra + iTerm2 + oh-my-zsh, if I continuously press CTRL W after typing cd /usr/local/share/gobject-introspection-1.0, then the story becomes like :

cd /usr/local/share/gobject-introspection-1.0
cd /usr/local/share/gobject-introspection-1.
cd /usr/local/share/gobject-introspection-
cd /usr/local/share/gobject-
cd /usr/local/share/
cd /usr/local/

I do not have any key-binding or other profile files. I dislike too much files. Here is my .zshrc needed part :

...
    bindkey -e
    bindkey '^[[1;9C' forward-word
    bindkey '^[[1;9D' backward-word
...

If the above does not work or you not like then you can try this on ~/.zshrc :

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

or :

autoload -U select-word-style
select-word-style bash

or (I found the below a gist which referred here) :

tcsh-backward-delete-word () {
  local WORDCHARS="${WORDCHARS:s#/#}"
  zle backward-delete-word
}
bindkey '^W' tcsh-backward-delete-word
like image 142
Abhishek Ghosh Avatar answered Oct 05 '22 10:10

Abhishek Ghosh