haskellkeyboardread-eval-print-loopghcihaskeline

How to write ~/.haskeline so that ghci or haskeline can behave properly on `home` and `end`


My question is how to make my ghci react properly on my home and end press.

I learned that ghci use haskeline to react to user input, and haskeline's behavior is partially defined in ~/.haskeline to some extent.

I have being suffering from the lack of support for end key and home key of ghci for a long time. So I tried to define my own ~/.haskeline file.

Firstly:

bind: a home
bind: b end

keyseq: "a" home
keyseq: "b" end

bind: left home
bind: right end

Both behave as when I press a then the cursor to the most left and similarly to others.

Secondly:

bind: home a
bind: end b

It shows that my ghci seems to ignore my home and end press absolutely.

So how could I send home and end key to ghci and haskline?


Solution

  • This should work by default. Most likely Haskeline isn't recognizing whatever your keyboard sends as the home and end keys. This depends on your OS and terminal setup.

    You might want to adjust your $TERM setting to something that supports those keys, after which Haskeline should pick them up automatically.

    Otherwise, the Haskeline trac wiki has a recipe for getting it to recognize unknown key sequences, which should work on POSIX systems (not on Windows, where home and end should also already be working by default). Adapting this to the current case:

    $ ghc -e getLine
    <press home, then return>...some noise here...
    "...haskell string for home..."
    $ ghc -e getLine
    <press end, then return>...some other noise...
    "...haskell string for end..."
    

    Add the corresponding lines to ~/.haskeline:

    keyseq: your-terminal "...haskell string for home..." home
    keyseq: your-terminal "...haskell string for end..." end
    

    where your-terminal is whatever's in your $TERM variable.