How can I clear a terminal screen after my user has selected an option from my application's menu?
-
2Can 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
-
3Please choose the correct answer. People have to go through all answers! – Shishir Gupta Aug 24 '16 at 14:01
8 Answers
:! run the shell command
:! cls under windows
:! clear under linux and OS X
-
2
-
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
-
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 usingcabal 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

- 1,238
- 7
- 11
-
4This 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
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.

- 1,188
- 8
- 14
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.

- 19,687
- 20
- 75
- 125

- 363,768
- 54
- 674
- 675
-
8
-
8@Pradeep: No need to shout. (See http://en.wikipedia.org/wiki/All_caps#Internet ) – Jared Updike Mar 18 '10 at 18:57
On Windows, use Ctrl + L for Haskell command prompt terminal. And, for GUI use Ctrl + S.

- 357
- 2
- 6
- 13
A quick way on Windows would be to
import System.Process
clear :: IO ()
clear = system "cls"

- 5,094
- 4
- 45
- 71
-
This was the cleanest solution for me, and for Linux systems use `system "clear`. – Iain J. Reid Mar 07 '20 at 14:36
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 ()

- 38,876
- 35
- 121
- 169