Your Search Bar For Shrewd Tips

How Do I Clear Ghci?


If you want to clear the GHCi screen, press Ctrl+L in many terminals. You can also run your terminal’s clear-screen command: use clear on macOS or Linux, or cls in Windows Command Prompt. These options remove the visible output, but they do not erase GHCi’s loaded modules or reset your Haskell session.

To clear more than the screen, use a GHCi command such as :clear if your version supports it, or unload your targets with :load and no module names. The right method depends on what you mean by “clear”: the display, the loaded code, the imports in your context, or the entire session. This guide explains each option and how to choose the one you need.

Quick Answer: How Do I Clear GHCi?

  • Clear the visible screen: press Ctrl+L, or run clear on macOS/Linux and cls in Windows Command Prompt.
  • Clear loaded modules: try :clear. If it is unavailable, use :load with no module names or check :help for your GHCi version.
  • Reload the current project: enter :reload, also written as :r.
  • Reset everything: exit with :quit and start GHCi again.

These commands do different jobs. A screen-clearing shortcut only changes what you can see; it does not remove definitions, reload files, or restart the interpreter.

Clear the GHCi Screen Without Ending Your Session

When the terminal is crowded with output, the quickest fix is usually Ctrl+L. In many terminal setups, this clears or redraws the display while leaving GHCi running. Your current prompt, loaded modules, and interactive definitions remain available.

If the shortcut does not work, use the command for your operating system. On macOS or Linux, enter clear at the shell prompt. In Windows Command Prompt, enter cls. These are shell commands, so they generally need to be run in a terminal shell rather than typed as ordinary Haskell expressions at the GHCi prompt.

To run a shell command from inside GHCi, prefix it with :!. For example:

:!clear
:!cls

Use the command that matches your system. External commands may behave differently depending on the terminal, operating system, and GHCi configuration. If :!clear or :!cls does not work, leave GHCi temporarily with :quit, clear the terminal from the shell, and start GHCi again.

Use :clear to Unload Modules

Some GHCi versions provide the :clear command to remove loaded modules from the current session. This is different from clearing the terminal display: it changes the interpreter’s loaded-module state, but does not necessarily close GHCi or erase every piece of session state.

:clear

GHCi commands can vary between compiler versions. If your prompt responds that :clear is unknown, check the built-in help with :help or ?. You can also consult the documentation for the GHC version you are using. Avoid assuming that a command available in a newer release will work in an older installation.

For a commonly available alternative, enter :load without naming a module. In GHCi versions where this clears the target set, it unloads the modules you asked GHCi to load. If you want to return to a known, clean state and are uncertain how your version handles module clearing, restarting the interpreter is the most reliable option.

Reload Your Haskell Files with :reload

If you have edited a source file and want GHCi to compile it again, use :reload, or its shorter form, :r:

:reload

This tells GHCi to reload the current targets and their dependencies. It is useful after changing a Haskell file, fixing a compile error, or updating a module in your project. A successful reload lets you test the updated code without quitting and reopening GHCi.

:reload is not the same as clearing the screen. It may print compilation messages, and it does not clear old terminal output. Nor should you treat it as a guaranteed full reset of every interactive binding or setting. If your goal is only a cleaner display, use a terminal clear command instead.

Clear the Current Module Context

GHCi has a context that controls which modules and names are available for interactive evaluation. If you have imported several modules and want to change what is in scope, use the :module command. The exact result depends on the arguments you provide and the GHCi version, so check :help if you are unsure.

For example, you can set the context to a particular module by naming it in the command:

:module + Data.List

This is a context-management operation, not a screen clear or a full restart. If your problem is an ambiguous name, an unwanted import, or a value that is not in scope, adjust the module context or reload the relevant module. If you want to remove all uncertainty about what the session contains, exit and restart GHCi.

Clear Interactive Definitions and Values

GHCi lets you enter expressions and definitions directly at the prompt. For example, you might create a value with let and use it in later expressions. Clearing the visible screen will not remove that value. Similarly, unloading a module is not necessarily the same as wiping every interactive definition or changing every setting.

When a definition is masking another name or producing confusing results, first identify where it came from. You can inspect names and modules with GHCi’s information and browsing commands, or simply choose a fresh name. If you need a completely fresh interactive environment, restart GHCi:

:quit
ghci

Run ghci again from your system shell. If you normally start GHCi with project-specific options or through a build tool, use the same startup method so that the correct packages, flags, and project configuration are loaded again.

Clear GHCi by Restarting It

Restarting is the simplest way to reset the interpreter when you are unsure which state needs clearing. Enter :quit or :q to exit. Then launch GHCi again from the terminal. This gives you a new interpreter process, rather than merely hiding old output or reloading selected modules.

A restart is especially helpful when GHCi behaves unexpectedly after several experiments, when you have changed compiler options, or when you cannot tell whether a name comes from an import or an interactive definition. The trade-off is that you must reload your files and recreate any useful definitions from the previous session.

For a project, you may need to start GHCi from the project directory or use the project’s usual command. For example, a project may use a build tool to start GHCi with the correct dependencies already configured. Starting plain GHCi from a different folder could produce a clean session but fail to find your project modules.

Common GHCi Clearing Problems

  • Ctrl+L does nothing: your terminal or line-editing setup may not use that shortcut. Try the operating system’s clear-screen command instead.
  • clear is treated as an error: you may have typed a shell command at the GHCi prompt. Try :!clear on macOS or Linux, or run it after returning to the shell.
  • :clear is unknown: your GHCi version may not support it. Check :help; use the documented module-unloading method or restart the interpreter.
  • Old output returns: clearing the screen does not stop output from commands you run afterward. It also does not remove earlier output from terminal scrollback in every terminal.
  • Your changes are not showing up: use :reload to recompile the loaded targets, and check that GHCi loaded the file you edited.
  • A value still exists after clearing the display: that is expected. A display clear does not reset the interpreter; restart GHCi for a fresh process.

Which GHCi Clear Command Should You Use?

Choose the command based on the result you want. If you only want an uncluttered terminal, use Ctrl+L or the appropriate shell command. If you have edited source code, use :reload. If you want to unload modules, use :clear when your version supports it, or follow the instructions shown by :help. If you want to remove all session state, quit and start GHCi again.

Keeping these actions separate saves time. You do not need to restart GHCi just to tidy the screen, and clearing the screen will not solve a stale-code problem. When in doubt, check the prompt, consult the built-in help, and use a restart as the dependable option for a complete fresh session.

Conclusion

To clear GHCi, first decide whether you mean the screen or the interpreter session. Press Ctrl+L or use clear or cls to tidy the display. Use :reload to rebuild loaded code, and check your GHCi version before relying on :clear to unload modules. For a full reset, enter :quit and launch GHCi again. Understanding the difference between these options helps you get the clean state you need without losing more work than necessary.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →