zsh: stop backward-kill-word on directory delimiter
Asked Answered
F

4

76

In zsh, how can I set up the line editor such that backward-kill-word stops on a directory separator? Currently in my bash setup, if I type

cd ~/devel/sandbox

and then hit C-w point will be right after devel/. In my zsh setup, point would be after cd . I'd like to set up zsh so it behaves similarly to bash.

Faithfaithful answered 14/1, 2009 at 22:30 Comment(1)
This might be a readline issue, I should add...Faithfaithful
L
-4

A quick google reveals:

Backward Kill

Or, perhaps a better fix:

Bash Style Backward Kill

Lilithe answered 14/1, 2009 at 22:36 Comment(4)
For future readers: both of the other answers are strictly better and easier.Ecto
link-only answers are discouraged as the link may die. Please include the relevant part in your answerIllegal
Not a good answer and snarky as well "All you have to do is google the solution". I'll give this a down vote.Oakland
@Emil, indeed, that's a better solution than the ones presented here. For posterity, I'll copy the gist of it here 1) Define a new function tcsh-backward-delete-word () { local WORDCHARS="${WORDCHARS:s#/#}"; zle backward-delete-word; } 2) Register it with ZSH zle -N tcsh-backward-delete-word 3) Add a keybinding bindkey '^W' tcsh-backward-delete-word.Epistrophe
M
118

For recent versions of zsh, you can simply add:

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

to your zshrc as described in the zsh manual (also man zshcontrib).

Mitchell answered 17/9, 2009 at 12:20 Comment(4)
This is probably fairly specific, but if this doesn't work, it may be because of the zsh-syntax-highlighting plugin: github.com/zsh-users/zsh-syntax-highlighting/issues/67. Make sure to source that plugin at the end of your zshrc.Amadavat
This works, but only partially, i.e. jump a word forward and back still considers / to be a part of a word.Landgrave
Ah, I fixed it, turns out for whatever reason the two lines should be at the very beginning of the file.Landgrave
Note that for some reasons this work for the function backward-kill-word but not for backward-delete-word (the later should not copy the result in the yank ring)Homothermal
G
88

Another option is to set WORDCHARS (non-alphanumeric chars treated as part of a word) to something that doesn't include /.

You can also tweak this if you'd prefer ^w to break on dot, underscore, etc. In ~/.zshrc I have:

WORDCHARS='*?_-.[]~=&;!#$%^(){}<>'
Gentilis answered 26/6, 2012 at 5:1 Comment(5)
I can't find this explicitly mentioned in the manual but setting WORDCHARS to empty string (i.e: add the line WORDCHARS= to .zshrc) has the same effect.Phillane
The empty string doesn't include / 😄Gentilis
I don’t want to hardcode the $WORDCHARS, so I used WORDCHARS=${WORDCHARS/\/} to remove slash from it.Cryobiology
Just WORDCHARS= gives the best effect imoDrumbeat
Building on Franklin's answer, ${WORDCHARS//[\/]} allows removing several characters by including them in the square brackets.Contumelious
B
2

Here's what worked for me. unspecified word-style was required otherwise zsh didn't seem to respect the WORDCHARS.enter code here

WORDCHARS=' *?_-.[]~=&;!#$%^(){}<>/'
autoload -Uz select-word-style
select-word-style normal
zstyle ':zle:*' word-style unspecified

Here's more info on why this works.

Baribaric answered 2/1, 2023 at 20:44 Comment(1)
This is the only solution on this page that worked for me thanks! - Macos ZSHEsra
L
-4

A quick google reveals:

Backward Kill

Or, perhaps a better fix:

Bash Style Backward Kill

Lilithe answered 14/1, 2009 at 22:36 Comment(4)
For future readers: both of the other answers are strictly better and easier.Ecto
link-only answers are discouraged as the link may die. Please include the relevant part in your answerIllegal
Not a good answer and snarky as well "All you have to do is google the solution". I'll give this a down vote.Oakland
@Emil, indeed, that's a better solution than the ones presented here. For posterity, I'll copy the gist of it here 1) Define a new function tcsh-backward-delete-word () { local WORDCHARS="${WORDCHARS:s#/#}"; zle backward-delete-word; } 2) Register it with ZSH zle -N tcsh-backward-delete-word 3) Add a keybinding bindkey '^W' tcsh-backward-delete-word.Epistrophe

© 2022 - 2024 — McMap. All rights reserved.