I would like to have Ctrl-l for clear in my Bash.
How can you bind Ctrl-l to clear in Bash?
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).
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...
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With