0

In a linux terminal I use "cat > file.txt" to make a file one line at a time.

What I find is that per-line the delete key works but the arrow keys insert escape codes rather than moving left and right in the line.

Why is this? Is there a reason that the terminals basic action cannot do left and right arrow keys? I know there are things like GNU readline which do it but my question is not about those.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
river
  • 103
  • 2
    Same reason as https://unix.stackexchange.com/questions/103608/is-there-any-reason-why-i-get-a-when-i-press-up-arrow-at-the-console-login-sc – muru Jun 12 '19 at 17:01
  • 1
    Because arrow keys are mapped to escape sequences, while the delete key is not (exercise: find out what it is mapped to. man stty may be interesting.). – dirkt Jun 12 '19 at 17:04

1 Answers1

1

Because the line editing capabilities of the terminal driver are quite primitive and do not include things like moving the insertion point left and right.

What they do include:

  • deleting the last char (VERASE / erase, BackSpace)
  • deleting the last word (VWERASE / werase, Control-W)
  • deleting the whole line (VKILL / kill, Control-U)

They're also not able to bind multiple key sequences (as the escapes sent by the left- and right-arrow keys usually are) to its special actions.

And of course, VERASE and VWERASE are not Unicode-aware; Linux has a perfunctory IUTF8 flag (which works with simple data and is better than nothing), but it doesn't know about zero-width modifiers, directional marks, etc.

Look into the stty(1) and termios(3) manpages for more details.