zsh - 使用 ctrl+c、ctrl+x、ctrl+v 优化复制、剪切、粘贴

zsh - 使用 ctrl+c、ctrl+x、ctrl+v 优化复制、剪切、粘贴

我在我的.zshrc.

if [[ -t 0 && $- = *i* ]]
then
# change Ctrl+C to Ctrl+I
stty start ''
stty stop ''
stty quit ''
stty erase ''
stty kill ''
stty eof '' # Ctrl + D
stty rprnt ''
stty werase ''
stty lnext ''
stty discard ''

fi

# change Ctrl+C to Ctrl+Q
stty intr '^q'

# change Ctrl+z to Ctrl+j
stty susp '^j'

# change Ctrl+V to Ctrl+K
bindkey '^k' quoted-insert # for zle

_copy-using-xsel() {
  if ((REGION_ACTIVE)) then
  zle copy-region-as-kill
  printf "$CUTBUFFER" | xsel -i --clipboard
  ((REGION_ACTIVE = 0))
  fi
}

zle -N _copy-using-xsel
bindkey '^C' _copy-using-xsel # Copy text

_cut-using-xsel() {
  if ((REGION_ACTIVE)) then
       zle copy-region-as-kill
       printf "$CUTBUFFER" | xsel -i --clipboard
       zle kill-region
  fi
}

zle -N _cut-using-xsel
bindkey '^X' _cut-using-xsel # Cut text

_paste-copy-using-xsel() {
  LBUFFER+="$(xsel -b -o)"
}

zle -N _paste-copy-using-xsel
bindkey '^V' _paste-copy-using-xsel # Paste

它使我能够使用ctrl+复制、使用+c剪切、使用+粘贴。然而,我注意到操作很慢。我的意思是,在我看来,切割需要半秒钟。是因为使用了吗?如何优化我的按键绑定以使延迟消失。ctrlxctrlvxsel

答案1

总结评论:

如果“剪切”本身很慢,则可能是您将其^X作为其他按键绑定的前缀。您可以检查存在哪些绑定,并更改相关的绑定:

$ bindkey | grep '\^X'
"^X^B" vi-match-bracket
"^X^F" vi-find-next-char
"^X^J" vi-join
"^X^K" kill-buffer
"^X^N" infer-next-history
"^X^O" overwrite-mode
"^X^U" undo
"^X^V" vi-cmd-mode
"^X^X" exchange-point-and-mark
"^X*" expand-word
"^X=" what-cursor-position
"^XG" list-expand
"^Xg" list-expand
"^Xr" history-incremental-search-backward
"^Xs" history-incremental-search-forward
"^Xu" undo

正如您所看到的,在我的系统上 zsh 有几个开箱即用的此类绑定。

如果您不想更改这些,那么将KEYTIMEOUT环境变量设置为比默认 0.4 秒短的值也会减少您的等待时间,但需要注意的是,像这样的多键绑定将需要更快的输入速度。


脚本质量的事情:

使用不受控制的变量作为第一个参数printf是有问题的。由于第一个参数是格式字符串,因此它会被重新解释,并且可能不会执行您想要的操作。

printf '%d'      # prints a "0" with no trailing newline
printf '%s' '%d' # prints "%d"

所以你应该使用printf '%s' "$CUTBUFFER".

此外,如果您想设置REGION_ACTIVE为 0,则不需要在赋值周围加上括号。而不是((REGION_ACTIVE = 0))你可以只是你REGION_ACTIVE=0

相关内容