How to REALLY clear the Terminal

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

Oliver Ullman

Senior Software Engineer at Rubrik

3 年

Super useful, thanks!

回复
Mustafa Dumlupinar

Senior Software Developer

6 年

ctrl+l

回复

要查看或添加评论,请登录

Igal S.的更多文章

  • Fraud on LinkedIn! Beware!

    Fraud on LinkedIn! Beware!

    I've been meaning to post this article for some time now. For some reason I am being targeted by scammers, or maybe…

    73 条评论
  • Farewell Windows (it's not you, it's me)

    Farewell Windows (it's not you, it's me)

    Farewell Windows, it's been a great ride but I've outgrown this relationship and it's time for me to move on. People…

    71 条评论
  • Getting Started with Lucee in Docker

    Getting Started with Lucee in Docker

    In this video I show how to get started with Lucee in Docker: I am using the project that I recently published at…

    2 条评论
  • Cockpit - Linux Web Console

    Cockpit - Linux Web Console

    So I installed CentOS 8 in VirtualBox today. Why, are you asking? Because that's what I "do for fun".

    14 条评论
  • I ?? Lucee

    I ?? Lucee

    Last month I gave a talk titled "I Love Lucee" at the Tomcat track of ApacheCon (link to video below though the preview…

    22 条评论
  • Easily Run Postgres in Docker

    Easily Run Postgres in Docker

    I made a video tutorial with an introduction to running Postgres in Docker. There are plenty of tutorials with lots of…

    12 条评论
  • Gotta love Postgres Procedural Languages

    Gotta love Postgres Procedural Languages

    I recently had to parse some text that was stored in a Postgres database. The text in each record had multiple lines of…

    5 条评论
  • How to use your regular IP when connected to a VPN

    How to use your regular IP when connected to a VPN

    Consulting for different Enterprise and Government organizations means that each of them requires me to connect to…

    11 条评论
  • Gotta Love Postgres Arrays

    Gotta Love Postgres Arrays

    I recently helped a client migrate their database from SQL Server to Postgres. Some of their tables, e.

    8 条评论
  • time.Format(layout) in Go

    time.Format(layout) in Go

    A common task in programming is to format a point in time in a certain layout that is easily understood by humans or…

    12 条评论

社区洞察

其他会员也浏览了