4

I am using Ocaml from a Linux terminal. Sometimes it gets stuck in a weird mode where it does not respond to my keyboard as expected. For example, if I press the arrows up, down, right, and left, it generates ^[[A^[[B^[[C^[[D in input. Alternatively, sometimes if I type a letter only once it might repeat that same letter three times in a row and/or if I type the delete button it types "^H" instead.

Does anyone know what is going on here? I assume that I am inadvertently doing something to switch the mode but I don't how to switch it back or why it is switching in the first place.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
user2174
  • 41
  • 1
  • 1
    I can't tell you _why_ you're seeing that, but _what_ you're seeing are the control codes which represent the keys on your keyboard. It's a fairly labyrinthine topic, but you could start here: http://en.wikipedia.org/wiki/C0_and_C1_control_codes#C0_.28ASCII_and_derivatives.29 . I'm not familiar with OCAML, but perhaps armed with this new vocabulary (escape sequence, control code etc) your Google results will improve. – Ben Graham Nov 14 '12 at 03:29
  • You could also modify your program to use the `readline` library, see http://programmer-monk.net/trac/ocaml-misc/wiki/ReadlineDocumentation and if using `ocaml` toplevel you could run it inside `emacs` – Basile Starynkevitch Nov 14 '12 at 06:04

2 Answers2

6

I believe this is essentially a duplicate of this other Stack Overflow question:

Is it possible to use arrow keys in OCaml interpreter?

The stock version of the OCaml interpreter doesn't interpret special keys like arrow keys. So it will just echo their control codes (as Ben Graham points out). To get the kind of behavior you probably want (editing the input, going back to previous lines, etc.) you need to wrap the OCaml interpreter with a line editing facility. See the other question linked above for some suggestions.

This doesn't explain why you see different "modes" of behavior, but I still think this is how you want to think about your problem.

Community
  • 1
  • 1
Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
2

You should use Utop. Utop is a OCaml interpreter which offers auto-completion (like bash) and a history of command. Of course, all problems with arrow keys disappears.

You'll need to compile Zed and Lambda-Term to compile Utop.

Ontologiae
  • 595
  • 4
  • 11