Simple ANSI terminal support, with Windows compatibility

Version on this page:0.11
LTS Haskell 18.18:0.11
Stackage Nightly 2021-12-04:0.11.1
Latest on Hackage:0.11.1

See all snapshots ansi-terminal appears in

BSD-3-Clause licensed by Max Bolingbroke
This version can be pinned in stack with:ansi-terminal-0.11@sha256:97470250c92aae14c4c810d7f664c532995ba8910e2ad797b29f22ad0d2d0194,3307

Module documentation for 0.11


A Haskell package providing support for ‘ANSI’ control character sequences for terminals on Unix-like operating systems and Windows


‘ANSI’ terminal escape code support for Haskell, which allows:

  • Colored text output, with control over both foreground and background colors
  • Clearing parts of a line or the screen
  • Hiding or showing the cursor
  • Moving the cursor around
  • Reporting the position of the cursor
  • Scrolling the screen up or down
  • Changing the title of the terminal

By using emulation, it is compatible with versions of ‘Command Prompt’ and ‘PowerShell’ on Windows that did not recognise ‘ANSI’ escape codes before Windows 10 version 1511 was released in November 2015.

If you like this, you may be interested in ansi-wl-pprint, which provides a pretty-printer that can construct strings containing ‘ANSI’ colorisation.

Not all ‘ANSI’ escape codes are suported by this library but most (if not all) of the popular ones that are well-supported by terminal software are, including:

  • Select Graphic Rendition mode (colors and other attributes): setSGR
  • Clearing parts of the screen: clearFromCursorToScreenEnd, clearFromCursorToScreenBeginning, clearScreen, clearFromCursorToLineEnd, clearFromCursorToLineBeginning and clearLine
  • Cursor visibility changes: hideCursor and showCursor
  • Cursor movement by character: cursorUp, cursorDown, cursorForward and cursorBackward
  • Cursor movement by line: cursorUpLine and cursorDownLine
  • Directly changing cursor position: setCursorColumn and setCursorPosition
  • Saving, restoring and reporting cursor position: saveCursor, restoreCursor and reportCursorPosition
  • Scrolling the screen: scrollPageUp and scrollPageDown
  • Changing the title: setTitle

Each supported escape code or family of codes has a corresponding function that comes in three variants:

  • A straight IO variant that doesn’t take a Handle and just applies the escape code to stdout and any terminal attached to it
  • An IO variant similar to above, but which takes a Handle to which the escape code should be applied
  • A String variant that returns a literal string that should be included to get the effect of the code. However, on Windows systems where emulation has been necessary, these strings will always be blank!


A full example is available, but for a taste of how the library works try the following code:

import System.Console.ANSI

main = do
    setCursorPosition 5 0
    setTitle "ANSI Terminal Short Example"

    setSGR [ SetConsoleIntensity BoldIntensity
           , SetColor Foreground Vivid Red
    putStr "Hello"

    setSGR [ SetConsoleIntensity NormalIntensity
           , SetColor Foreground Vivid White
           , SetColor Background Dull Blue
    putStrLn "World!"


Haddock documentation is available at Hackage.


The library is originally written by Max Bolingbroke


Mike Pilgrem and Roman Cheplyaka are the primary maintainers.

Oliver Charles is the backup maintainer. Please get in touch with him if the primary maintainers cannot be reached.



Version 0.11

  • Remove deprecated getCursorPosition0. (Use getCursorPosition instead.)
  • On Unix-like operating systems, the temporary turning off of echoing is moved from getReportedCursorPosition to hGetCursorPositon.
  • On Unix-like operating systems, fix a bug in getCursorPosition and hGetCursorPosition, where the console input stream was was not always clear before the cursor position was emitted into it.

Version 0.10.3

  • Add getCursorPosition as a synonym of getCursorPosition0 and deprecate the latter.

Version 0.10.2

  • hGetTerminalSize now assumes a terminal is no bigger than 9,999 by 9,999 (previously, no bigger than 999 by 999).
  • On Windows, fix a bug where emulated cursor movement functions differed from Windows 10 (movement bounded by the current viewport).

Version 0.10.1

  • Add hGetCursorPosition and hGetTerminalSize.
  • On Unix-like operating systems, fix a bug where getReportedCursorPosition could block indefinitely if no information was forthcoming on the console input stream.
  • Improvements to Haddock documentation.

Version 0.10

  • Add support for setting the default color with new SetDefaultColor constructor of the SGR type.
  • getTerminalSize now flushes the stdout channel, to ensure the cursor position is unaffected.

Version 0.9.1

  • Flag modules with GHC’s ‘Safe Haskell’ language extensions (from GHC 7.2.1).
  • Improvements and corrections to Haddock documentation.

Version 0.9

  • Add support for 256-color palettes with new SetPaletteColor constructor of the SGR type, and xterm6LevelRGB, xterm24LevelGray and xtermSystem.
  • Remove deprecated getCursorPosition. (Use getCursorPosition0 instead.)
  • Add hSupportsANSIColor.
  • Add getTerminalSize.
  • Improvements to Haddock documentation.

Version 0.8.2

  • Add getCursorPosition0 and deprecate getCursorPosition. Any position provided by the latter is 1-based. Any position provided by the former is 0-based, consistent with setCursorColumn and setCursorPosition.
  • Improvements to Haddock documentation in respect of 0-based and 1-based cursor positions.

Version 0.8.1

  • Add hSupportsANSIWithoutEmulation. On Windows 10, if the handle is identifed as connected to a native terminal (‘Command Prompt’ or ‘PowerShell’), the processing of ‘ANSI’ control characters will be enabled.


  • On Windows, hSupportsANSI now recognises if the handle is connected to a ‘mintty’ terminal.
  • Drop support for GHC versions before GHC 7.0.1 (released November 2010)


  • On Windows, try to enable ANSI on ConHost terminals even if a TERM environment variable exits (such as with the Hyper 2 terminal)
  • Minor improvements to Haddock documentation


  • Improve README and Haddock documentation
  • On Windows, fix compatability with earlier GHC versions
  • Drop support for GHC versions before 6.12.1 (released December 2009)


  • On Windows, if the standard output channel is valid but not a ConHost terminal, assume it is ANSI-enabled rather than failing
  • On Windows, output the improved error message to the standard error channel rather than the standard output channel

Version 0.8

  • Make the fields of SGR strict
  • Make compatible with GHC 8.2.2
  • Improve the error message on Windows when not ANSI-capable or ConHost
  • Recognise Appveyor build environment as ANSI-enabled


getReportedCursorPosition: don’t let the cursor reporting code be echo’d

Version 0.7.1

  • Allow saving, restoring, and querying the current cursor position
  • Fix a couple of issues with the Reset emulation on Windows

Version 0.7

Add 24-bit RGB color support


Fix Windows + ghc 7.8 compatibility

Version 0.6.3

  • Add ANSI support for Windows
  • Add compatibility with Win32- and above


Add an example to the haddocks


Fix a GHC 7.10 warning


Restore compatibility with GHC 7.4 and older

Version 0.6.2

  • Add hSupportsANSI
  • Drop support for base < 4


Fix to build with GHC 7.8 on Windows

Version 0.6.1

  • BoldIntensity no longer changes background color on Windows
  • setSGR [] was not equivalent to setSGR [Reset] on Windows, even though it should be according to the documentation. This is now fixed.