2

I am using the following config in my .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

It enables me to copy using ctrl+c, cut using ctrl+x, paste using ctrl+v. However, I noticed that the operations are slow. I mean it appears to me that, it takes half a second to cut. Is it due to the use of xsel? How can I optimize my keybindings so that the lag is gone.

Ahmad Ismail
  • 2,478
  • 1
  • 22
  • 47

1 Answers1

2

Summarizing the comments:

If "cut" alone is slow, it is likely that you have ^X as a prefix to some other keybind. You could check what bindings exist, and change the relevant ones:

$ 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

As you can see, on my system zsh has several such bindings out of the box.

If you'd prefer not to change these, then setting the KEYTIMEOUT environment variable to something shorter than its default 0.4s will also reduce your wait times, with the caveat that multiple-key bindings like those will require more speed to input.


Script quality things:

Using uncontrolled variables as the first argument to printf is problematic. Since the first argument is the format string, it gets reinterpreted and might not do what you want.

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

So you should use printf '%s' "$CUTBUFFER".

Additionally, if you want to set REGION_ACTIVE to 0, you don't need parens around the assignment. Rather than ((REGION_ACTIVE = 0)) you can just you REGION_ACTIVE=0.

Fox
  • 8,013
  • 1
  • 26
  • 44