Blog

03/06/2019

Fix: VIM Fix UP and Down Keys Inserting A B C D Character On Remote SSH Session on a Linux or Unix Systems



I am login to a remote server using a ssh client. I am annoyed with the arrow key behavior during insert mode in Vim. As soon as I hit arrow keys, vim display A B C D on screen. How do I fix this problem under Linux, FreeBSD, OpenBSD, NetBSD, Apple OS X or Unix-like operating systems? How can I fix arrow keys that display A B C D on remote shell?

[donotprint][/donotprint]
Vim is a text editor that is upwards compatible to Vi text editor. It can be used to edit all kinds of plain text. It is especially useful for editing programs and configuration files located in /etc/ directory. This problem indicates that the global vimrc file is not loaded. The global vimrc file enables many enhancements for users.

When you press the [Left], [Right], [Up], and [Down] arrow keys, vim will show and insert A B C D characters as follows:

Fig.01: Vim Editor Shows A B C D Characters When Press Up, Down, Left, Right Arrows

You can fix this problem using any one of the following solution:

Solution #1: Use vimrc file to fix arrow keys that display A B C D

You can find out the search path for vimrc is as follows:
$ vim --version | egrep -i 'vimrc|exrc|fall-back'
Sample outputs (from my RHEL 6.x system):

   system vimrc file: "/etc/vimrc"
     user vimrc file: "$HOME/.vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/share/vim"

Another sample outputs from vendor supplied embedded Linux based router:

   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/opt/share/vim"

In this example, cd to /opt/share/vim:
# cd /opt/share/vim
Search for vimrc* file, enter:
# find . -name *vimrc* -ls
OR
# find . -name *vimrc*
Sample outputs:

4068967    4 -rw-r--r--   1 root     root         1698 Feb 15  2012 ./gvimrc_example.vim
4069780    4 -rw-r--r--   1 root     root         2938 Feb 15  2012 ./vimrc_example.vim

Use the ln command to make a symbolic link:
# ln -s vimrc_example.vim vimrc
This should fix the problem and you will not annoyed by vim with the arrow key behavior.

Solution #2: Update your $HOME/.vimrc file

Edit $HOME/.vimrc file, enter:
$ vim $HOME/.vimrc
Add the following line at the top of file:

set nocompatible "must be first line

Save and close the file. This should fix the problem for you.

Here is my sample $HOME/.vimrc file for your ready references

set nocompatible " must be the first line
filetype on
filetype indent on
filetype plugin on
syntax on
set laststatus=2
set statusline=%<%f%h%m%r%=%-20.(line=%l  col=%c%V  totlin=%L%)  %h%m%r%=%-40(bytval=0x%B,%n%Y%)%P
set ofu=syntaxcomplete#Complete

Sample vim session:

Fig.02: Vim arrow problem keys fixed

Solution #3

Edit $HOME/.vimrc file, enter:
$ vim $HOME/.vimrc
Append the following lines:

## do not use ^[OA blindly, see note below ## 
set t_ku=^[OA
set t_kd=^[OB
set t_kr=^[OC
set t_kl=^[OD

Please note that escape keys generated as follows:

  1. ^[OA generated by pressing Ctrl-V and [Up] key.
  2. ^[OB generated by pressing Ctrl-V and [Down] key.
  3. ^[OC generated by pressing Ctrl-V and [Right] key.
  4. ^[OD generated by pressing Ctrl-V and [Left] key.

Save and close the vim/vi. Start it again for testing purpose. See below in the comments for more info.

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....
12/08/2019

How to Start and Enable Firewalld on CentOS 7

In this article, we discuss how to start and enable firewalld. It is highly recommended that you have a firewall protecting your server.Pre-Flight CheckThese...