antonio
antonio

Reputation: 9

cursor keys not functioning properly in vim

I open my terminal and use Vim to open a new or old program, but after I click i to go into insert mode, my cursor keys and backspace key are not functioning properly. The backspace key just moves the pointer. The cursor keys spawn A and B, or just cause other chaos.

This just happened out of nowhere and it just does crazy things. Does anybody know what’s wrong, or if my terminal is just on the fritz? I tried completely removing Cygwin from my computer and re-installed it, but that did not correct the issue.

Upvotes: 0

Views: 1834

Answers (2)

Dick Seabrook
Dick Seabrook

Reputation: 1

To solve backspace/delete and cursor control key problems under Ubuntu 20.04 I created a .vimrc file in my home directory and put in the single line:

set nocompatible

Seems to work so far.

Upvotes: -1

Yosh
Yosh

Reputation: 2732

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.

Upvotes: 2

Related Questions