Zsh - 完全删除当前/上一个参数

Kar*_*ric 7 zsh

我在 Macos 上使用 zsh。目前,ctrl+w 删除一个“单词”但在非单词字符处停止。不过这有点奇怪,因为它通常会删除比它应该删除的更多或停在奇怪的地方:

例子:

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 -]
Run Code Online (Sandbox Code Playgroud)

我只想向后删除当前字符串直到一个空格字符,从而有效地删除一个参数。我已经在网上查看了可以添加到 .bashrc 的各种 bash/zsh 热键,但似乎没有一个能满足我的要求。

4ae*_*1e1 7

更新:select-word-style函数 [0] 可作为自定义单词样式的简单方法:

$ 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
Run Code Online (Sandbox Code Playgroud)

只停留在论证边界,

autoload -Uz select-word-style
select-word-style shell
Run Code Online (Sandbox Code Playgroud)

应该足够了。

[0] https://github.com/zsh-users/zsh/blob/master/Functions/Zle/select-word-style

下面的原始答案采用正常样式。


^Wbackward-kill-word在 ZLE[1] 中默认绑定到。它所认为的词是由WORDCHARS[2]控制的。

WORDCHARS <S>

行编辑器认为是单词一部分的非字母数字字符列表。

为了识别https://www.google.com一个词,你至少需要

WORDCHARS+=':/.'
Run Code Online (Sandbox Code Playgroud)

将此添加到您的~/.zshrc,并添加您希望被视为单词字符的任何非字母数字字符(如果您总是想取消最后一个参数,则所有这些字符)。

[1] http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html

[2] http://zsh.sourceforge.net/Doc/Release/Parameters.html#index-WORDCHARS


Bac*_*ien 5

我个人配置zsh为使用vim样式编辑,然后,我添加了一些类似于emacs样式的键绑定。例子:

  • <ctrl+w>: 向后删除单词

  • <escape><d><T><space>: 删除直到空格(如vim)。这可能就是你想要的。

我的~/.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
Run Code Online (Sandbox Code Playgroud)

我的逻辑,兼顾两者vimemacs风格:

  • 的风格<ctrl+a> <ctrl+e> <ctrl-w> ...太方便了。我已经使用它几十年了,所以我保留了它。没有变化WORDCHARS什么的。

  • 但是,当我需要一些更高级的编辑时,我会使用vim样式,这更好。

至于箭头和<home> <end> ...键:

## 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
Run Code Online (Sandbox Code Playgroud)