The reason for this happening is most likely how certain keys are treated in terminal.
From :h xterm-cursor-keys
:
*vt100-cursor-keys* *xterm-cursor-keys*
Other terminals (e.g., vt100 and xterm) have cursor keys that send <Esc>OA,
<Esc>OB, etc. Unfortunately these are valid commands in insert mode: Stop
insert, Open a new line above the new one, start inserting 'A', 'B', etc.
Try, for example, typing CTRL-v
then <left>
in insert mode. I believe you have ^[OD
inserted, which is the same as <Esc>OD
. In short, when we press the left key, our terminal sends <Esc>
+O
+D
, which is then interpreted as <Left>
.
This topic is rather complicated, and I myself don't fully understand. Options that might take part in this situation includes timeout
, ttimeout
, mapping for Esc
, etc. Searching a bit also suggests the value of TERM
can sometimes affect the situation.
However, looking at a similar question on Unix&Linux stack exchange and your reporting Backspace not working, I suspect you don't have a .vimrc
at all (that's not a bad thing!), making your vim vi-compatible.
I want you to try:
- Open vim, then type
:set compatible?
then Enter.
- if
compatible
is shown in the bottom of the terminal, then my guess is right, yay.
- Type
:set nocompatible
, then Enter. Get into the insert mode, and try pressing cursor keys.
If the :set nocompatible
works, then you should create your vimrc
file (search for relevant informations, there are plenty of them). You don't even have to write set nocompatible
in the .vimrc
, because having .vimrc
automatically does that.
EDIT:
If this is the case, the specific option to look into is esckeys
. This is set to off when nocompatible
. Its description from the help file is as follows:
Function keys that start with an <Esc> are recognized in Insert
mode. When this option is off, the cursor and function keys cannot be
used in Insert mode if they start with an <Esc>. The advantage of
this is that the single <Esc> is recognized immediately, instead of
after one second.
Anyway, I suggest using vim in nocompatible mode.