I know I could use something like ncurses, but I don't want to include that dependency in my project.

I'm looking for a way to clear all the output my program generated at a certain point, so I can show more information without flooding the screen.

This is for a program written in Rust. There are libraries used to handle the terminal like Termion.

Also read something about using ANSI characters as an option.

  • What kind of output do you generate? A couple of lines outputting status information that you want to update? Aug 13, 2019 at 6:20
  • @JonasBerlin I print multiple lines to stdout. Some things are generated with user input. So I can't be sure of the exact number of lines produced. Here's an image of how it might look i.imgur.com/hMTOHjo.png
    – madprops
    Aug 13, 2019 at 6:34
  • @JonasBerlin Each time an action is performed the notes and menu are printed again. So basically I want to clear the notes and the menu (and everything else) when I have to show them again. As to not have a long scrollback of repetitive information.
    – madprops
    Aug 13, 2019 at 6:43
  • Basically, you may want to switch to an alternative screen, like less does. See stackoverflow.com/questions/11023929/…
    – Ned64
    Aug 13, 2019 at 7:13

1 Answer 1


If you have a terminal compatible with xterm you could try switching to an alternat(iv)e screen with Termion:

use termion::screen::AlternateScreen;
use std::io::{Write, stdout};

fn main()
        let mut screen = AlternateScreen::from(stdout());
        write!(screen, "Writing to alternat(iv)e screen!").unwrap();
    println!("Writing to main screen again.");

From Rust docs: termion::screen

When you switch back everything you wrote is gone. Switching is done by issuing escape sequences like mentioned here: StackOverflow: switching to alternate screen in a bash script. Of course this means that you do not strictly need that library.

  • Wow this is great! Thank you. Had no idea Termion could do this. Have some doubt about my usage of it. The "show_notes" function works in a loop, after every action it is called, sometimes by actions themselves and sometimes just by looping. I'm creating the screen every time the function runs. Is creating the screen many times like that a problem? I'm not sure if I'm building a stack of screens with that "recursion". Maybe I should have a global screen and use that?
    – madprops
    Aug 13, 2019 at 7:44
  • 1
    @madprops There are exactly two screens, and you can switch back and forth between them. Both support separate clear commands so that you could clean up the alternate screen when done with it, go back to normal, then enter a cleared alternate again whenever you wish.
    – Ned64
    Aug 13, 2019 at 7:49

You must log in to answer this question.

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