40

I used to use cmd back in windows, and the command line I used a lot, was cls. It's kind of like the clear command used in Linux, but it cleans the screen permanently.

If you use the clear command, it just scroll down, so that you don't see the command you where working on.

I like both a lot, but my question is, how do i get a cls like command, that clears the screen, and can't browse up, to see the command you where working on?

blade19899
  • 26,994

5 Answers5

22

Add this line to ~/.bashrc (i.e., the file called .bashrc, located in your home folder -- you can see it in Nautilus by pressing Ctrl+H):

alias cls='printf "\033c"'

Now the cls command will clear the screen like in Windows. It will put you back to the top of a Terminal window, with no text shown above it. (It will not delete the shell's command history.)

This works because:

  • .bashrc runs every time a bash shell starts up.
  • The alias command defines the cls command to run the command quoted on the right-hand side.
  • printf command writes characters to the terminal. It accepts escape codes. The octal 033 is the character used to signal the beginning of a terminal control code. The control code c tells the terminal to clear itself.
  • So, with this modification to .bashrc. running cls sends the necessary data to the terminal to tell it to clear itself.
Eliah Kagan
  • 119,640
20

You can use reset. This resets the whole terminal, so that may be a bit overkill though.

Note on Konsole:
@Mechanical snail noticed "in Konsole 4.8.5, the old text is still there if you scroll up". @gertvdijk explained that it is "a feature. There's Ctrl+Shift+K for Konsole (reset and clear scrollback)."

Zanna
  • 72,312
lgarzo
  • 20,492
7

In your current terminal window, just type the following:

printf "\033c"
2

If the aim is to avoid only casual rediscovery of command history, reset may be a viable choice.

However, bear in mind that by default, the shell logs your command history to a file as well - this is also eminently discoverable. If you want to prevent other persons from browsing your command history, you should also clean that out.

history -c # clear your history

Adrian
  • 5,256
0

cls works by clearing the buffer, which is fixed 25 or 50 lines in case of DOS and Windows, respectively. You can achieve something like this by writing so many lines in the buffer that it overflows (2000 is a typical value), but then reset is also a viable option as @lgarzo said.

dnet
  • 136