This question already has an answer here:

I am using Ubuntu 16.04 LTS. I have a file I open in the terminal with vi abc.txt, and to edit/insert some text into it I press the insert button.

When I press the arrow keys /// it inserts the letters A/C/B/D respectively instead of moving the cursor. Even if I type backspace to delete the inserted letter it doesn't get deleted.

Why is this happening? Why are these letter added instead and why are they not getting deleted when I press backspace?

enter image description here

marked as duplicate by dessert, muru command-line May 15 '18 at 9:40

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
    This Vi and Vim question addresses the very same issue: arrow keys don't work in insert mode – dessert May 1 '18 at 7:12
  • I softly disagree with the linked duplicate. The other question asks the main thing OP was likely interested in, but I came here from the marked "duplicate" question specifically because it doesn't ask, nor do any of its answers address the question of why does this happen, only "how do I fix it so it doesn't". The closest any of those answers seems to get is "because vi is old and doesn't understand arrow keys". I, for one, would like to know the mechanism behind the observed results. Why does pressing up give you an "A" and not, say, an ampersand (or any other arbitrary character). – jmbpiano May 15 '18 at 21:33
  • Fortunately, the first answer on the question @dessert linked, actually does deal with the "why", so I at least have my answer even if that question doesn't actually ask for it. Makes me almost wish answers could be migrated like questions, since @theonlygusti's answer actually seems to fit better here than the question it was given on. – jmbpiano May 15 '18 at 21:37
  • @jmbpiano I mildly agree – would you write an answer here if the question would be reopened? – dessert May 15 '18 at 22:19
  • I would be willing to do so, (though not tonight). It would largely be a (cited) copy of what @Rich wrote on the other question, so perhaps he should be given first opportunity? – jmbpiano May 15 '18 at 22:26

That is because of wrong terminal settings. Check your TERM variable and make sure it is set correctly.

Most common to do first is a ...

:set term=builtin_ansi

inside vim and see if that works.

Also have a look at this vim wikia page. It has a 24 different approaches to this problem.

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