I'm using zsh with Macos. Currently, ctrl+w deletes a 'word' but stops at non-word characters. It's kinda odd though because it'll often delete far more than it should or stop at odd places:
Examples:
open -n https://www.google.com // deletes [com][google.][www.][https://(why so much?)]
open -n 'https://www.google.com' // deletes [com'][google.][www.][https://][n '(wtf?)][open -]
I just want to delete the current string backwards until a space character, effectively deleting an argument. I've looked online at various bash/zsh hotkeys you can add to .bashrc, but none seem to do what I'm looking for.
Update: The select-word-style
function[0] is available as an easy way to customize the word style:
$ 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
To only stop at argument boundaries,
autoload -Uz select-word-style
select-word-style shell
should suffice.
[0] https://github.com/zsh-users/zsh/blob/master/Functions/Zle/select-word-style
The original answer below assumes the normal style.
^W
is bound to backward-kill-word
by default in ZLE[1]. What it considers as word is controlled by WORDCHARS
[2].
WORDCHARS <S>
A list of non-alphanumeric characters considered part of a word by the line editor.
In order to recognize https://www.google.com
as a word, you at the very least need
WORDCHARS+=':/.'
Add this to your ~/.zshrc
, and add whatever non-alphanumeric characters you would like to be treated as word chars (all of them if you always want to kill the last argument).
[1] http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html
[2] http://zsh.sourceforge.net/Doc/Release/Parameters.html#index-WORDCHARS
I personaly configure zsh
to use vim
style editing, and then, I add some keybindings similar to emacs
style. Examples:
<ctrl+w>
: delete word backward
<escape><d><T><space>
: delete back until space (like vim
). This might be what you want.
My ~/.zshrc
:
## editing mode e=emacs v=vim
export KEYTIMEOUT=1
bindkey -v
## more keys for easier editing
bindkey "^a" beginning-of-line
bindkey "^e" end-of-line
bindkey "^f" history-incremental-search-forward
bindkey "^g" send-break
bindkey "^h" backward-delete-char
bindkey "^n" down-history
bindkey "^p" up-history
bindkey "^r" history-incremental-search-backward
bindkey "^u" redo
bindkey "^w" backward-kill-word
bindkey "^?" backward-delete-char
My logic, take the good of both vim
and emacs
styles:
The style of <ctrl+a> <ctrl+e> <ctrl-w> ...
is so convenient. I've been using it for decades so I keep it. No changes in WORDCHARS
or something.
However, when I need some more advance editing, then I use vim
style, which is better.
As for arrows and <home> <end> ...
keys:
## create a zkbd compatible hash;
## to add other keys to this hash, see: man 5 terminfo
typeset -A key
key[Home]=${terminfo[khome]}
key[End]=${terminfo[kend]}
key[Insert]=${terminfo[kich1]}
key[Delete]=${terminfo[kdch1]}
key[Up]=${terminfo[kcuu1]}
key[Down]=${terminfo[kcud1]}
key[Left]=${terminfo[kcub1]}
key[Right]=${terminfo[kcuf1]}
key[PageUp]=${terminfo[kpp]}
key[PageDown]=${terminfo[knp]}
## setup keys accordingly
[[ -n "${key[Home]}" ]] && bindkey "${key[Home]}" beginning-of-line
[[ -n "${key[End]}" ]] && bindkey "${key[End]}" end-of-line
[[ -n "${key[Insert]}" ]] && bindkey "${key[Insert]}" overwrite-mode
[[ -n "${key[Delete]}" ]] && bindkey "${key[Delete]}" delete-char
[[ -n "${key[Up]}" ]] && bindkey "${key[Up]}" up-line-or-history
[[ -n "${key[Down]}" ]] && bindkey "${key[Down]}" down-line-or-history
[[ -n "${key[Left]}" ]] && bindkey "${key[Left]}" backward-char
[[ -n "${key[Right]}" ]] && bindkey "${key[Right]}" forward-char
[[ -n "${key[PageUp]}" ]] && bindkey "${key[PageUp]}" history-beginning-search-backward
[[ -n "${key[PageDown]}" ]] && bindkey "${key[PageDown]}" history-beginning-search-forward
[[ -n "${key[Home]}" ]] && bindkey -M vicmd "${key[Home]}" beginning-of-line
[[ -n "${key[End]}" ]] && bindkey -M vicmd "${key[End]}" end-of-line
[[ -n "${key[Insert]}" ]] && bindkey -M vicmd "${key[Insert]}" overwrite-mode
[[ -n "${key[Delete]}" ]] && bindkey -M vicmd "${key[Delete]}" delete-char
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With