103

How can I clear a terminal screen after my user has selected an option from my application's menu?

Rodrigo de Azevedo
  • 1,097
  • 9
  • 17
Nubkadiya
  • 3,285
  • 13
  • 40
  • 45
  • 2
    Can you be more clear about what user interface system you are using in your Haskell program? Do you mean that you are simply reading and writing text to stdin/stdout? If so, those have no implicit concept of "screen". If you intend your program to work assuming that there is a terminal attached to stdin/stdout, then you should be coding your program to use some terminal control package, such as indicated @ZackS' answer. – MtnViewMark Mar 19 '10 at 05:07
  • 3
    Please choose the correct answer. People have to go through all answers! – Shishir Gupta Aug 24 '16 at 14:01

8 Answers8

221

:! run the shell command
:! cls under windows
:! clear under linux and OS X

lol
  • 3,910
  • 2
  • 37
  • 40
void
  • 2,267
  • 1
  • 13
  • 2
  • 2
    is there any cheatsheet for ghci? – tugberk Jun 22 '14 at 14:51
  • 2
    @Peter my best guess is it's because they don't think it's a haskell solution (rightfully so; it only works in GHCi), it doesn't work for them (shell-dependent) or they just consider ZachS' answer a better one. `:! cls` is plenty sufficient for my use case but doesn't work for the asker's case as the asker isn't using GHCi. – John Dvorak Jun 29 '15 at 08:58
  • Just what I was looking for, thank you for this. – nlhnt Aug 07 '23 at 13:54
63

This is what you may be looking for:

ansi-terminal: Simple ANSI terminal support, with Windows compatibility

You can find it in Hackage and install using cabal install ansi-terminal. It specifically has functions for clearing the screen, displaying colors, moving the cursor, etc.

Using it to clear the screen is easy: (this is with GHCI)

import System.Console.ANSI

clearScreen

ZachS
  • 1,238
  • 7
  • 11
  • 4
    This is the correct answer. Try to look for the package in your system’s package manager though, as cabal generally doesn’t work with that and doesn’t offer an uninstall command. – Evi1M4chine Mar 21 '14 at 05:03
21

Just press Ctrl+L (works on Windows)

mitaness
  • 695
  • 7
  • 13
18

On a terminal that understands ANSI escape sequences (I believe every term in Unix/Linux systems) you can do it simply with:

clear = putStr "\ESC[2J"

The 2 clears the entire screen. You can use 0 or 1 respectively if you want to clear from the cursor to end of screen or from cursor to the beginning of the screen.

However I don't think this works in the Windows shell.

Federico Squartini
  • 1,188
  • 8
  • 14
9

On Unix systems you can do System.system "clear" which just invokes the command-line utility clear. For a solution that does not depend on external tools, you'd need a library that abstracts over different terminal-types like for example ansi-terminal.

Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
sepp2k
  • 363,768
  • 54
  • 674
  • 675
9

On Windows, use Ctrl + L for Haskell command prompt terminal. And, for GUI use Ctrl + S.

Shuvam Shah
  • 357
  • 2
  • 6
  • 13
7

A quick way on Windows would be to

import System.Process

clear :: IO ()
clear = system "cls"
Boris
  • 5,094
  • 4
  • 45
  • 71
0

Under Linux (Ubuntu at least), that's the code I use for clearing the terminal:

import qualified System.Process as SP

clearScreen :: IO ()
clearScreen = do
  _ <- SP.system "reset"
  return ()
Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169