38

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,704
  • 3
    Just open new tab and close current tab :D – Tachyons Apr 03 '12 at 12:50
  • 2
    by the way pressing ctrl+l is much fastern than typing clear and hitting enter – Xn0vv3r Apr 03 '12 at 13:21
  • 1
    It's bad habit. Try to dislearn it. You only need it rarely for some kind of ascii art, but deleting old information shouldn't be done routinely. Read The art of unix programming for further information. – user unknown Apr 03 '12 at 13:58
  • 3
    @userunknown Old information is deleted all the time. When you close a terminal window or log off a session in SSH or a virtual console. When an update replaces one version of installed software with another. When you run apt-get --purge .... When cookies/cache are cleared in a browser. To think clearing a terminal--a fundamental function of Unix-style terminals which works exactly the same way as in Windows but without the special command--is somehow inconsistent with good principles for operating *nix systems, seems like a failure of the imagination. – Eliah Kagan Jan 11 '13 at 11:15
  • @userunknown for me, the common use case is that i run a command that prints out a lot of information. then i want to run it again (maybe with a different param or whatever). when it's done, i want to scroll back up to where it started. without clearing the buffer, it can be hard to spot where the most recent command finished. before i bothered to google it (and found this answer), my workaround was to hold enter key for a few seconds to fill the console with a bunch of blank space – Kip Sep 11 '19 at 18:37
  • 1
    @Kip: You might be interested in learning about diff and graphical enhancements. Start your command with paramlist 1 and guide it into file1 (CMD p1a p1b p1c > CMD-1.log 2>&1), then with paramlist 2 and guide it to a second file file2 (CMD p2a p2b p2c > CMD-2.log 2>&1), then do a diff CMD-[12].log or with very convenient highlightening - needs installation - tkdiff CMD-[12].log. – user unknown Sep 11 '19 at 20:44

5 Answers5

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
  • 70,465
lgarzo
  • 19,832
  • Why is it overkill? – zpletan Apr 03 '12 at 15:07
  • 1
    @zpletan Because it does a lot more than just clearing the scrollback buffer. Basically initializes the terminal as it was just opened. – lgarzo Apr 03 '12 at 15:15
  • OK. I often thought that reset was kind of a melodramatic name for Linux's cls, but it makes sense now. In any case, it sounds like unless you do weird stuff with terminal customization, all it basically does is clear the screen/buffer/whatever, yes? – zpletan Apr 03 '12 at 15:20
  • @zpletan I could just quote the manual, but to put it another way: there are situations when the terminal screen gets garbled and a simple clear would not do, but resetting it back to the default settings (and clearing the buffer along the way) will result again in a usable terminal. The first few paragraphs of man reset page describe the additional procedure. – lgarzo Apr 03 '12 at 15:43
  • This doesn't work in Konsole. It just scrolls down. – Mechanical snail Jan 11 '13 at 06:11
  • @Mechanicalsnail What do you exactly mean by "It just scrolls down"? It does not scroll in any way in Konsole here on my system. It completely resets the current terminal window instead and a new cursor blinks at the top of it. – gertvdijk Jan 16 '13 at 12:18
  • @gertvdijk: When I try it in Konsole 4.8.5, the old text is still there if you scroll up. – Mechanical snail Feb 07 '13 at 04:02
  • 1
    @Mechanicalsnail Ah that. Well, that's more of a feature. There's Ctrl+Shift+X for Konsole (reset and clear scrollback). – gertvdijk Feb 07 '13 at 08:36
19

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
  • 117,780
6

In your current terminal window, just type the following:

printf "\033c"
user207900
  • 61
  • 1
  • 1
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,216
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