This page is a place to collect advice about how to use GHCi beyond what User's Guide covers. Please add to it!

2 Advanced customization

2.1 Using .ghci, a mini-tutorial

There is a lot more one can do to customize and extend GHCi. Some extended examples can be found in an email posted to haskell-cafe, titled
getting more out of ghci. Dating from September 2007, and using GHC 6.6.1, some of the GHCi tickets mentioned in there have since been fixed, but the message should still serve as a useful introduction to writing your own .ghci files. It also provides several useful commands you might want to copy into your own file!-) Newer GHCis support the multiline commands mentioned in the message, allowing for more readable .ghci files (at the time, definitions had to be squashed into single lines, so you have to read the message to understand the `.ghci` file). For those still using older GHCis, a variant file for 6.4.1 is available, too:

2.2 Customized GHCi interactive environments

You can create shell commands that start up GHCi and
initialize it for use as a specialized interactive
computing environment for any purpose that you can
imagine.

The idea is that if you put the following lines in your
.ghci file, GHCi will load commands at startup
from whatever file whose path you specify in
the GHCIRC environment variable. You can then easily write
shell scripts that exploit this to initialize GHCi in
any manner you please.

2.4 Package and documentation lookup

Ever tried to find the users guide for the version of GHCi you are currently running? Or information about the packages installed for it? The new ghc-paths package makes such tasks easier by exporting a GHC.Paths module:

Tip: if you accidentally unload the GOA module, use :m + GOA to load it.

3 Frequently Asked Questions (FAQ)

3.1 How do I stop GHCi from printing the result of a bind statement?

Sometimes you want to perform an IO action at the prompt that will produce a lot of data (e.g. reading a large file). When you try to do this, GHCi will helpfully spew this data all over your terminal, making the console temporarily unavailable.

To prevent this, use :set -fno-print-bind-result. If you want this option to be permanently set, add it to your .ghci file.

4 Additional command advice

4.1 The :def command

The :def command, documented here, allows GHCi's commands to be extended in quite a powerful way.

This defines a new command :pasteCode, which allows you to paste Haskell code directly into GHCi. You type the command :pasteCode, followed by the code you want, followed by ^D, followed (unfortunately) by enter, and your code is executed. Thus:

5 Compatibility/shell/platform integration

5.1 Readline/editline

There are some tricks to getting readline/editline to work as expected with GHCi.

5.1.1 A readline-aware GHCi on Windows

Mauricio reports: I've just uploaded a package (rlwrap) to Cygwin that I like to use with ghci. You can use it like this:

rlwrap ghcii.sh

and then you will use ghc as if it were readline aware (i.e., you can
press up arrow to get last typed lines etc.). rlwrap is very stable
and I never had unexpected results while using it.

Since the issue of ghci integration with terminals has been raised
here sometimes, I thought some guys here would be interested (actually,
I found rlwrap looking for a better way to use ghci).

5.1.2 rlwrap (for GHCI compiled without readline/editline)

GHCi has support for session-specific command-line completion, but only if it was built with the readline or editline package, and some versions of GHCi aren't. In such cases, you can try rlwrap (readline wrapper) to attach readline "from the outside", which isn't as specific, but gives basic completion support. In particular, there's an rlwrap package for Cygwin.

For starters,

rlwrap -cr ghci

gives you filename completion, and completion wrt to previous input/output in your GHCi session (so if a GHCi error message suggests to set

AnnoyinglyLongVerySpecificOption

, that will be available for completion;-).

If you want to get more specific, you need to supply files with possible completions - flags and modules spring to mind, but where to get those?

1. extracting a list of options from the flag-reference in the users guide:

will give you completion wrt filenames, options, module names, and previous session contents, as well as the usual readline goodies, like history search and editing. The main drawback is that the completion is neither session nor context-specific, so it will suggest filenames where module names are expected, it will suggest module names that may not be exposed, etc.