4

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?

  • As Thomas Dickey remarks, how does what you want differ from pressing Ctrl+L or binding Ctrl+N to clear-screen? – Gilles Dec 16 '15 at 0:11
  • I know about clear-screen, but I like to keep an eye on what was written above, and clear-screen clears everything. – yolenoyer Dec 16 '15 at 0:47
2

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.

0

By improving a bit the use of the echo command inside .inputrc command, if finally got the result I wanted exactly:

I put the following func in my .bashrc, choosing deliberately a short name for the func:

cbs()
{
    echo -ne '\e[J\e[F\e[2K'
    history -d $(history 1)
}

It uses ANSI sequences to clear the bottom of the screen (\\e[J), then move the cursor back to the same line it was originally (\\e[F) before finally clearing the line where the command was inserted (\\e[2K).

The command is also directly removed from the bash history to not polluate it.

A short name for the command is important, because if the line wraps inside a too small console window, the trick won't work correctly.

Then I bind Ctrl-N accordingly in .inputrc:

Control-N: "cbs\n"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.