Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To bind clear to ^l in Bash

Tags:

bash

binding

I would like to have Ctrl-l for clear in my Bash.

How can you bind Ctrl-l to clear in Bash?

like image 713
Léo Léopold Hertz 준영 Avatar asked Jul 14 '09 20:07

Léo Léopold Hertz 준영


2 Answers

Put this in your ~/.inputrc:

C-L: backward-kill-line

(assuming by "clear" you mean "clear current input line"; if you mean "clear screen" then put clear-screen instead of backward-kill-line).

like image 52
chaos Avatar answered Oct 22 '22 16:10

chaos


in a particular case where the clear-screen didn't work for me either, I found out that putting in ~/.bashrc the line: bind -x $'"\C-l":clear;' was better than "\C-l":'clear\n' in ~/.inputrc because it cleared the screen and left the currently typed command in place; for example (^L show where I hit the combo):

With "\C-l": clear-screen in ~/.inputrc:

user@darkstar:~$ date^L
user@darkstar:~$ date
user@darkstar:~$ ^L
user@darkstar:~$ 

With "\C-l":'clear\n' in ~/.inputrc:

user@darkstar:~$ date^L
-bash: dateclear: command not found
user@darkstar:~$ ^L
# screen effectively redrawn

With bind -x $'"\C-l":clear;' in ~/.bashrc:

user@darkstar:~$ date^L
# screen redrawn and the top line is now:
user@darkstar:~$ date

And for now I have not been able to get the same result as bind -x using only the inputrc file...

Edit

I found that in some cases where clear-screen wasn't working for me were caused by my attempts to get more colors in the CLI. For example I had the issue with TERM=xterm-256color (or screen-256color, etc.) and removing the -256color part solved the problem.

I have not yet found a way to get a 256 colors term working along CTRL+l (in xterm, urxvt, etc).

like image 35
bufh Avatar answered Oct 22 '22 15:10

bufh