Bash – Readline: Binding an ANSI escape sequence to a key

bashkeyboard shortcutsreadlineterminal

I would like to bind a key with bash / readline, to clear the bottom of my console screen.

The ANSI sequence to clear the bottom of the screen is \e[J, so I tried to add the following line to my .inputrc, but it doesn't work, it simply ignores the three chars:

Control-N: "\e[J"

If I add some test chars inside the string:

Control-N: "BEFORE \e[J AFTER"

, then the string BEFORE is inserted and it stops there.

For the moment, I found this workaround, but it obviously displays a line with the echo command, and I don't want it:

Control-N: "echo -ne \"\\e[J\"\n"

Is it possible to send ANSI sequences with a readline binding?

Best Answer

Two parts:

  • bash lets you bind a key to the readline clear-screen function (which seems to be the original goal).
  • in bash, readline's purpose is to prepare text (by editing, etc) for issuing commands to bash. An escape sequence would be sent to the terminal. None of the listed functions for readline would act as a replacement for echo or printf.

Key bindings for readline expect a single function-name or literal string. In your example, readline read what it considered a possible function name, and ignored the rest of the line.

So: no, bash/readline provide no useful features for echoing an escape sequence directly as a key-binding. You have to go another level, as noted, using echo.