How to REALLY clear the Terminal
When working in a Terminal window, I many times need to clear the previous output, especially if the program writes a lot of output and I need to easily tell the difference between executions, e.g. to test the most recent changes.
A common way to do that is to use the `clear` command, or its keyboard shortcut CTRL+L. The problem is that while it clears the current screen, it does not clear the scroll buffer, so on programs that generate lots of output, and clearing is most helpful, this is not very effective because scrolling up shows all of the old output again, making it difficult to find where the old output ended and the new one began (sure, I add blank lines like everyone else but good luck finding them with touchpad edge scrolling on a laptop).
Today I decided that enough is enough! I have to find a better way of clearing the screen... and I did! Apparently running the following command does exactly that:
printf "\ec"
It clears the terminal and the scroll buffer. It works because "\e" is the ESCape code, and the "ESC c" sequence sends the RIS (Reset to Initial State) command to ANSI Terminals [1]. The nice thing is that while it resets the output, it does not reset the history of the current session, so you can still use CTRL+R or the Up Arrow key.
So now the obvious next thing was to create an easy to use alias to that command:
alias cls='printf "\ec"'
And now when I want to REALLY clear the Terminal I run the command:
cls
[1] https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences
Senior Software Engineer at Rubrik
3 年Super useful, thanks!
Love this one!!
Senior Software Developer
6 年ctrl+l