14

It seems that clear and bash Ctrl-L are quite different; clear completely removes all previous terminal information (so you cannot scroll up) while Ctrl-L just scrolls the screen so that the cursor is at the top of the page so that you can still scroll up and see previous information. I much prefer the Ctrl-L system. Is there a way to override clear so that it does a Ctrl-L instead of wiping all previous terminal information? This is not a huge issue, but I'm just wondering out of curiosity if there is a way to alias clear to point at my preferred Ctrl-L functionality.

As a side note, I just noticed that PowerShell also has a binding for Ctrl-L and it performs the same way as Ctrl-L on bash; it seems that the PowerShell designers there took a lot from bash, while cmd.exe consoles do not have this functionality.

YorSubs
  • 601
  • 5
  • 13
  • 2
    [See this](https://superuser.com/q/1667569/432690). – Kamil Maciorowski Oct 26 '21 at 07:51
  • 2
    Ah, that's great, as simple as doing `clear -x`, I should have noticed that before ... Thanks. – YorSubs Oct 26 '21 at 08:22
  • 1
    I really dont understand why clear -x isnt just clears default behaviour, and then you could wipe out the history with some customized option. – john-jones Oct 26 '21 at 09:50
  • 1
    Indeed, I agree, that's actually quite strange. "What happened before" is of general interest to almost every person that works on a terminal. Blasting that away in the brutal way that the default behaviour of `clear` does is hard to justify. I think I will put `alias clear='clear -x'` into my `.bashrc` and if I somehow ever want to destroy all session information, I can use `\clear` or create `alias brutally-delete-everything='\clear'` – YorSubs Oct 26 '21 at 09:55
  • 1
    `alias cl='printf "\e[H\e[2J"'` also does the job. – 15 Volts Oct 28 '21 at 04:23

2 Answers2

9

Is there a way to override clear so that it does a Ctrl-L instead of wiping all previous terminal information?

alias clear='tput -x clear'

Yes, Ctrl-L in bash (while in set -o emacs mode) does exactly the same thing.

Or you can just hardwire the escape with alias clear='printf "\033[H\033[2J"' which should work in most terminal emulators, and does not assume that you have ncurses or bash installed.

NB: the clear applet from busybox does NOT wipe off the scrollback buffer, so you don't have to do anything special if you're using some busybox-based system, as most embedded Linux systems are.

  • Very interesting. Is there a guide somewhere to the escape codes that you used here? `"\033[H\033[2J"'`. I only use colour codes, I'd be curious to see what else they can do? – YorSubs Oct 26 '21 at 18:46
  • 2
    @YorSubs: The portable method is to use `tput` with the names listed in `terminfo(5)` (type `man 5 terminfo` and scroll down until you get to the big table; the possible arguments to `tput` are listed under Cap-name). This assumes your TERM environment variable is set correctly, which it should be unless your bashrc is mucking with it (don't do that!). Note that the string capabilities are generally the most interesting/useful. – Kevin Oct 26 '21 at 20:58
4

clear is a terminal command, excerpt of man clear:

DESCRIPTION clear clears your screen if this is possible, including its scrollback buffer (if the extended “E3” capability is defined). clear looks in the environment for the terminal type given by the environment variable TERM, and then in the terminfo database to determine how to clear the screen. clear writes to the standard output. You can redirect the standard output to a file (which prevents clear from actually clearing the screen), and later cat the file to the screen, clearing it at that point.

Ctrl+l is a keybinding to readline's clear-screen command (man 3 readline).

Clear the screen, then redraw the current line, leaving the current line at the top of the screen. With an argument, refresh the current line without clearing the screen.


I can't find a way to alias clear to the readline clear-screen command, and it may not be possible at all.

moo
  • 277
  • 2
  • 11
schrodingerscatcuriosity
  • 12,087
  • 3
  • 29
  • 57