34

When I connect to my server via PuTTY, I can clear the visible screen with the clear command.

However, I can still scroll backwards in PuTTY's GUI to see the old stuff. I am aware of PuTTY's Clear Scrollback feature, but that requires mouse clicking.

I'd like to perform that exact same "Clear Scrollback" operation, but from the commandline.

Is it possible?

I read this site, which seems to indicate so.

However, my experiments have failed. For instance:

printf '\033[3J'

Does nothing, for me.

I may not be understanding the meaning of CSI 3 J in that second link, though...

2
  • What version of PuTTY are you using? The page indicates the feature works in PuTTY 0.59 and above. I have PuTTY 0.60 and it works for me. Note it only clears the scrollback buffer, it doesn't clear the screen. (If I have a scroll bar I can actually scroll, then use this sequence, I notice the scroll bar become disabled because there's suddenly nothing available to scroll to. But the currently displayed screen remains in place.)
    – Bavi_H
    Feb 22, 2013 at 2:56
  • @Bavi_H: ah, thank you - I was not realizing that it won't affect the currently displayed text. I see it is working, too. The full solution I want is clear && printf '\033[3J'. If you make an answer, I'll accept it.
    – jwd
    Feb 22, 2013 at 3:10

5 Answers 5

39

The CSI 3 J sequence to clear the scrollback buffer was added to PuTTY 0.59. (On the wish request page for this feature see the "fixed-in" line, or go to the PuTTY changes page and search for CSI 3 J.)

As jwd mentioned, you can enter printf '\033[3J' on a command line to send this sequence to the PuTTY. Be aware this only clears the scrollback buffer, it doesn't clear the screen. If you have a scroll bar you can actually scroll, then use this sequence, you'll see the scroll bar become disabled because there's suddenly nothing available to scroll to. But the currently displayed screen remains in place.

As jwd mentioned, you can clear both the screen and the scrollback buffer using clear && printf '\033[3J'

2
  • And for those seeking some light reading about control codes: xfree86.org/4.8.0/ctlseqs.html (: This one is in under CSI P s J
    – jwd
    Oct 10, 2017 at 14:58
  • 1
    We can replace clear && printf '\033[3J' by printf '\033[2J' that's erase all. Jan 21, 2023 at 6:00
15

If you're accustomed, as I am, to using Ctrl+L for the Reset terminal option available in PuTTY's system menu, then you might find it helpful to enable Configuration -> Window -> Behavior -> "System menu appears on ALT-Space".

Then when you hit Alt+Space, the option for "Clear scrollback" is triggered by L (lowercase; no Shift). Thus, reset + clear becomes the charmingly-mnemonic combination of Ctrl+L and Alt+Space L without any pesky mousing or clutter in your shell history.

2
  • Works and is easy to remember :)
    – Wyrmwood
    Apr 21, 2017 at 19:44
  • Not bad, though FYI: you can also prevent cluttering your shell history by prefixing a command with a space.
    – jwd
    Dec 26, 2017 at 22:58
10

Create a script file and place it in a path-included folder:

$cat > /usr/local/bin/cls
#!/bin/bash
printf '\033[2J\033[H\033[3J'

Press CTRL+d to save and exit

Change permission on file:

chmod a+x /usr/local/bin/cls

Now you can use the cmd cls

1
  • Use #!/usr/bin/env bash instead of #!/bin/bash, which does not work on my system.
    – wolfram77
    Apr 3, 2021 at 18:06
5

There is an option in PuTTY where you can uncheck the default scrollback behaviour.

Just uncheck the option "Push erased text into scrollback".

Putty Settings image

After this when you issue the clear command it will erase the screen and will not put the previous screen contents in the scrollback.

1
  • This is the best answer.
    – MaXi32
    May 17, 2020 at 14:01
2

The solution for me was to transmit '\033\143' characters:

printf("\033\143");

This clears the terminal screen and puts the cursor back in the upper left corner.

2
  • 1
    (1) The question asks for a command-line (i.e., shell) command.  This is not a shell command, it is a line of C code; as such, it is not an answer to this question.  (2) This post would be slightly more useful if you said what the \143 character is, and how it relates to this question, ideally with citations. Feb 15, 2017 at 19:26
  • 2
    For those wondering, this corresponds to the "full reset" escape code (ESC c). ASCII c has value 99 which is octal 143. This will do a bunch of other things in addition to clearing the scrollback, just FYI.
    – jwd
    Oct 10, 2017 at 15:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .