Zsh – How to Rebind stty lnext to ^q?

sttyzsh

You can type characters literally by using

the "lnext" functionality (often ^V per default) in your tty driver

However, I bind Ctrl+v to "paste" in my terminal emulator. (Since I don't use control flow) I'd like to rebind lnext to Ctrl+q. I tried the following in ~/.zshrc

setopt noflowcontrol  # Don't use ^s and ^q for control flow
bindkey -r "^Q"       # Unbind ^q from push-line
stty lnext '^Q'       # Bind ^q to lnext

However, it doesn't seem to work. Is there a way to rebind lnext to Ctrl+q?

EDIT

I've done more troubleshooting, and can't seem to rebind other stty keys. I removed setopt noflowcontrol for testing, then tried stty start '^A' or stty start '^B'. Neither had any effect; start was still bound to Ctrl+q. (FWIW I tried both a literal ^A or ^B and the character itself with lnext preceding it.)

Best Answer

stty lnext only affects the terminal device line discipline internal editor (the very limited one you get when running applications like cat that don't have their own line editor). For zsh's editor, you'd need to use bindkey (zle does not do like readline (bash's line editor) that queries the tty LD setting to do the same in its own editor).

stty lnext '^Q' start '' -ixon # for tty LD editor
bindkey '^Q' quoted-insert     # for zle

Note that you'd need to do the stty part for every terminal, and do it again any time the tty LD settings are reverted to defaults like after stty sane.

Some systems allow you do change the default tty settings like HPUX with stty lnext '^Q' < /dev/ttyconf.

And for ^V to paste the content of the X11 CLIPBOARD selection at the cursor when in the zsh line editor:

get-clipboard() {
  local clip
  clip=$(xclip -sel c -o 2> /dev/null && echo .) || return
  LBUFFER+=${clip%.}
}
zle -N get-clipboard
bindkey '^V' get-clipboard
Related Question