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.

marked as duplicate by muru, terdon Jun 13 at 0:11

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Same reason as unix.stackexchange.com/questions/103608/… – muru Jun 12 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 at 17:04

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.