0

In zsh, how can I bind (say) c-<cr> to insert &>/dev/null &<cr> at point?

I use urxvt.

Toothrot
  • 3,435

1 Answers1

4

There are two steps: you need to get your terminal emulator to emit a distinct escape sequence for Ctrl+Return, and you need to tell zsh what to do when that escape sequence is typed.

An application in a terminal receives sequences of bytes, most of which either form printable characters or are control characters. Most function keys and keychords need to be encoded as escape sequences consisting of the escape character followed by a few characters that encode which keychord was pressed. See How do keyboard input and text output work? for more background. There's no standard escape sequence for Ctrl+Return, and most terminals, including (U)rxvt, simply send a CR character, just like for a plain Return. So you need to tell Urxvt to send a different escape sequence. In your X resources file (~/.Xresources — load it with xrdb -merge ~/.Xresources), add

URxvt.keysym.C-Return: \033[27;5;13~

In zsh, to bind custom code to a key, write that code in a function and declare that function as a zle widget with zle -N. Inside your user-defined widget, the variable BUFFER contains the content of the line being edited, and CURSOR contains the cursor position. To insert text around the cursor, there are also more convenient variables: you can append text to LBUFFER to insert it before the cursor, or prepend text to RBUFFER to insert it after the cursor.

I think what you want to do is add text at the end of the line and run the current command. Running the current command is not done by inserting a CR character, that would just insert a CR in the command. Call the widget accept-line (which is bound to CR by default) to run the command.

accept-line-run-in-background-with-output-hidden () {
  BUFFER+=" &>/dev/null &"
  zle accept-line
}
zle -N accept-line-run-in-background-with-output-hidden
bindkey $'\033[27;5;13~' accept-line-run-in-background-with-output-hidden