In Ubuntu/gnome-terminal, if I run:
$ stty -icrnl
Then launch the GHC interactive environment (a Haskell console):
$ ghci
Then pressing Return does not submit the line; however, Enter does.
However, with:
$ stty icrnl
Both Return and Enter submit the line.
I don’t really understand the behaviour; surely Return will be submitting the newline character in both cases?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
The first step in understanding what’s going on is to be aware that there are in fact two “newline” characters. There’s carriage return (CR, Ctrl+M) and line feed (LF, Ctrl+J). On a teletype, CR moves the printer head to the beginning of the line while LF moves the paper down by one line. For user input, there’s only one relevant concept, which is “the user has finished entering a line”, but unfortunately there’s been some divergence: Unix systems, as well as the very popular C language, use line feed to represent line breaks; but terminals send a carriage return when the user presses the Return or Enter key.
The icrnl setting tells the terminal driver in the kernel to convert the CR character to LF on input. This way, applications only need to worry about one newline character; the same newline character that ends lines in files also ends lines of user input on the terminal, so the application doesn’t need to have a special case for that.
By default, ghci, or rather the haskeline library that it uses, has a key binding for Ctrl+J, i.e. LF, to stop accumulating input and start processing it. It has no binding for Ctrl+M i.e. CR. So if the terminal isn’t converting CR to LF, ghci doesn’t know what to do with that character.
Haskeline instructs the terminal to report keypad keys with escape sequences. It queries the terminal’s terminfo settings to know what those escape sequences are (kent entry in the terminfo database). (The terminfo database is also how it knows how to enable keypad escapes: it sends the smkx escape sequence, and it sends rmkx on exit to restore the default keypad character mode.) When you press the Enter key on the keypad in ghci, that sends the escape sequence eOM, which haskeline recognizes as a binding to stop accumulating input and start processing it.
Method 2
Referring to both Return and Enter sounds as if you are describing the keys respectively on the main part of the keyboard and on the numeric keypad often on the right-side of the main part:
However, only the first of those corresponds to the key which (unmodified) sends controlM. The key on the numeric keypad may send a carriage return or whatever, but may actually send an escape sequence, e.g., escapeOM, for VT100-like terminals.
You can see the different key values by echoing them, e.g., using cat -v.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
