Default key bindings for utop

Just getting into OCaml, and utop is really nice, but I can’t seem to find any of the default key bindings listed anywhere online.

It took quite some time to find out C-p can be used to move to the previous lines, which was really needed when coming across multiline use cases like this:

let ratio x y =
  Float.of_int x /. Float.of_int y
;;

Is there anywhere that I can find the default key bindings for the repl?

You can use #utop_bindings;; command in the prompt. Check out other useful #utop_* commands as well.

$ utop
────────────────────────────────┬─────────────────────────────────────────────────────────────┬────────────────────────────────
                                β”‚ Welcome to utop version 2.3.0 (using OCaml version 4.07.0)! β”‚                                
                                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                

Type #utop_help for help about using utop.

# #utop_bindings;;
enter       : accept                        -> accept the current input.
escape      : cancel-search                 -> cancel search mode.
tab         : complete                      -> complete current input.
up          : history-prev                  -> go to the previous entry of the history.
down        : history-next                  -> go to the next entry of the history.
left        : prev-char                     -> move the cursor to the previous character.
right       : next-char                     -> move the cursor to the next character.
home        : goto-bot                      -> move the cursor to the beginning of the text.
end         : goto-eot                      -> move the cursor to the end of the text.
insert      : switch-erase-mode             -> switch the current erasing mode.
delete      : delete-next-char              -> delete the character after the cursor.
backspace   : delete-prev-char              -> delete the character before the cursor.
M-enter     : newline                       -> insert a newline character.
M-tab       : complete-bar                  -> complete current input using the completion bar.
M-down      : complete-bar                  -> complete current input using the completion bar.
M-left      : complete-bar-prev             -> go to the previous possible completion in the completion bar.
M-right     : complete-bar-next             -> go to the next possible completion in the completion bar.
M-home      : complete-bar-first            -> go to the beginning of the completion bar.
M-end       : complete-bar-last             -> go to the end of the completion bar.
M-delete    : kill-prev-word                -> cut the word behind the cursor.
M-backspace : kill-prev-word                -> cut the word behind the cursor.
M-b         : prev-word                     -> move the cursor to the beginning of the previous word.
M-c         : capitalize-word               -> capitalize the first word after the cursor.
M-d         : kill-next-word                -> cut up until the next non-word character.
M-f         : next-word                     -> move the cursor to the end of the next word.
M-l         : lowercase-word                -> convert the first word after the cursor to lowercase.
M-n         : history-next                  -> go to the next entry of the history.
M-p         : history-prev                  -> go to the previous entry of the history.
M-u         : uppercase-word                -> convert the first word after the cursor to uppercase.
M-w         : copy                          -> copy the current region to the clipboard.
C-left      : prev-word                     -> move the cursor to the beginning of the previous word.
C-right     : next-word                     -> move the cursor to the end of the next word.
C-delete    : kill-next-word                -> cut up until the next non-word character.
C-space     : set-mark                      -> set the mark to the current position.
C-_         : undo                          -> revert the last action.
C-a         : goto-bol                      -> move the cursor to the beginning of the current line.
C-b         : prev-char                     -> move the cursor to the previous character.
C-c         : break                         -> cancel edition.
C-d         : interrupt-or-delete-next-char -> interrupt if at the beginning of an empty line, or delete the next character.
C-e         : goto-eol                      -> move the cursor to the end of the current line.
C-f         : next-char                     -> move the cursor to the next character.
C-g         : cancel-macro                  -> cancel the current macro.
C-h         : delete-prev-char              -> delete the character before the cursor.
C-k         : kill-next-line                -> cut everything until the end of the current line.
C-l         : clear-screen                  -> clear the screen.
C-m         : accept                        -> accept the current input.
C-n         : next-line                     -> move the cursor to the next line.
C-p         : prev-line                     -> move the cursor to the previous line.
C-r         : prev-search                   -> search backward in the history.
C-s         : next-search                   -> search forward in the history.
C-u         : kill-prev-line                -> cut everything until the beginning of the current line.
C-w         : kill                          -> cut the current region to the clipboard.
C-x (       : start-macro                   -> start a new macro.
C-x )       : stop-macro                    -> end the current macro.
C-x e       : play-macro                    -> play the last recorded macro.
C-x C-e     : edit-with-external-editor     -> edit input with external editor command.
C-x C-k tab : insert-macro-counter          -> insert the current value of the macro counter.
C-x C-k C-a : add-macro-counter             -> adds a value to the macro counter.
C-x C-k C-c : set-macro-counter             -> sets the value of the macro counter.
C-y         : yank                          -> paste the contents of the clipboard at current position.
C-z         : suspend                       -> suspend edition.
# 

You can also override these in ~/.lambda-term-inputrc. Here’s what I have:

$ cat ~/.lambda-term-inputrc 
[read-line]
C-p: complete-bar-prev
C-n: complete-bar-next

I overrode these to use Ctrl (C) instead of Meta (M) because with my setup (Mac Terminal.app) the meta character (which I would expect to be mapped to the Command key) doesn’t seem to work.

1 Like