0

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
antonio
  • 9
  • 5
  • Can you confirm that this happens when you run Vim without any init files or plugins (`vim -u NONE`)? – Ben Klein Dec 20 '14 at 20:32
  • im not understanding your question. im kinda new so i apologize if you feel like you are teaching a simpleton – antonio Dec 20 '14 at 23:56
  • basically i cant type in insert mode using the terminal. i have to type it in notepad or textpad and compile using g++, then do the run around to run it in cygwin to check for errors, and execute – antonio Dec 21 '14 at 00:03
  • :) Sorry. When Vim starts, it loads certain configuration files and (if there are plugins installed) plugin files. If you start Vim by running `vim -u NONE`, it will skip all of that. If that makes the problem go away, then you might have a messed-up `.vimrc` or something else causing trouble (rather than just Vim itself being weird). – Ben Klein Dec 21 '14 at 00:04

2 Answers2

2

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.

Community
  • 1
  • 1
Yosh
  • 2,512
  • 3
  • 24
  • 30
  • ok, when u say open vim, do you mean just type vim in the terminal when it opens and hit enter? – antonio Dec 21 '14 at 21:01
  • Yes, that's what I mean. (I'm not sure what is the proper verb for this. "Start" might have been far better?) – Yosh Dec 22 '14 at 02:28
  • ok, i was just making sure, it said compatible, but the cursor keys still displayed letters, so im sure the backspace key is not working either – antonio Dec 22 '14 at 13:46
  • `:set compatible?` said `compatible`, and `:set nocomopatible` did not solve the issue? – Yosh Dec 22 '14 at 14:19
  • Could you try invoking vim with the command `vim -N`? If that didn't help either, that probably means my guess was wrong. [Vim Tips wiki](http://vim.wikia.com/wiki/Fix_arrow_keys_that_display_A_B_C_D_on_remote_shell) has some related information, so please try them alongside. – Yosh Dec 22 '14 at 14:56
-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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61