Skip to content
Judah Jacobson edited this page Jan 21, 2019 · 3 revisions

Custom Key Bindings

As of version 0.5, the user may specify custom key bindings in their .haskeline file. The two new settings are:

  • bind: <key> <keys> (bind the first key to the actions of the second key)
  • keyseq: <term> <string> <key> (POSIX-only: add a new key sequence)

where <term> is a terminal name (optional), <string> is Haskell string syntax, <keys> is a space-separated sequence of <key>s, and <key> is either:

  • a single character
  • f<n> (a function key)
  • left, right, down, up
  • backspace, delete, home, end, killline
  • return, tab, escape
  • ctrl-<key>, shift-<key>, meta-<key>
  • pageup, pagedown (haskeline>=0.6.2)

Note that the meta- key is ALT on Windows.

These features are pretty new, so feedback and suggestions are encouraged.

Examples

Binding the backspace key

The following lines will cause the backspace key to move one character to the left nondestructively, and meta-backspace to delete the character to the left of the cursor:

bind: backspace left
bind: meta-backspace backspace

Blog: binding F7 to the ghci :reload command

https://blog.rcook.org/blog/2018/ghci-custom-key-bindings/

Vi-style History

(See ViModeCompatibility.)

Control-left/right

The following instructions will make ctrl-left and ctrl-right skip words.

First, we check KeyBindings to see that M-B and M-F are bound by default to the commands we want. So the necessary bind commands are:

bind: ctrl-left meta-b
bind: ctrl-right meta-f

That's sufficient on Windows, but for POSIX systems a couple more steps are needed. Normally Haskeline looks up a key sequence in the system terminfo database; however ctrl-left and ctrl-right don't have a standard capability name, so Haskeline needs to be told manually what their key sequence is.

To find out their control sequences, you can do something like:

$ ghc -e getLine
<press ctrl-left, then return>^[[5D
"\ESC[5D"
$ ghc -e getLine
<press ctrl-right, then return>^[[5C
"\ESC[5C"

Thus we add the following lines (which will be ignored on Windows):

keyseq: "\ESC[5D" ctrl-left
keyseq: "\ESC[5C" ctrl-right

Finally, we may want to restrict the above key sequences to a given terminal (as selected by the $TERM variable). For example, on my system I have:

keyseq: xterm-color "\ESC[5D" ctrl-left
keyseq: xterm-color "\ESC[5C" ctrl-right
keyseq: xterm "\ESC[1;5D" ctrl-left
keyseq: xterm "\ESC[1;5C" ctrl-right

since $TERM==xterm-color means I'm using OS X's Terminal.app program, and $TERM==xterm means I'm using the X11 xterm program.

Sequence macros

Originally suggested by Trent Buck:

bind: { { } left
bind: ( ( ) left
bind: [ [ ] left
bind: } right
bind: ] right
bind: ) right

These bindings automatically closes each opening brace; for example, pressing '(' will first insert "()" and then move the cursor one character to the left.