5

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

dessert
  • 39,982
D.H.N
  • 61
  • 2
    This [vi.SE] question addresses the very same issue: arrow keys don't work in insert mode – dessert May 01 '18 at 07: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
  • @muru Please vote to reopen the question, see comments above – the question actually doesn’t ask for a solution to the problem, but solely for the Why. An answer can and should contain links to the questions holding possible solutions, but merely concentrate on the actual question. – dessert May 15 '18 at 22:29
  • @jmbpiano I'd suggest posting that answer on the dupe because (a) it's not unusual to post an answer explaining "why" something happens, especially if the main problem has been solved already (note that Rich's answer is an example of this), (b) the other question is far more visible and visitors to it would likely be interested in the why, and (c) this question did originally ask "How do i solve this issue..??". – muru May 16 '18 at 04:51
  • @dessert see above – muru May 16 '18 at 04:53

1 Answers1

4

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.

Rinzwind
  • 299,756