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

Posted on in Categories , , last updated February 10, 2015

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?


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
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
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: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

5 comment

  1. The third way…
    The best thing about it is that it works both in vi and vim.

    Compose a .exrc file with the following content:

    map! ^? ^[xi
    map! ^[OA ^[ka
    map! ^[OB ^[ja
    map! ^[OC ^[la
    map! ^[OD ^[ha

    NOTE:
    ^[ == Ctrl-V, Ctrl-[
    ^? == Ctrl-V, Ctrl-BS

    This .exrc file maps the arrow keys and backspace at the original vi editor
    Usage:
    1. Place it at the home folder
    2. Variant 1 – set the EXINIT variable in shell
    export EXINIT=’set exrc’
    invoke vi

    Variant 2 – invoke vi
    source .exrc from within vi
    :so .exrc

  2. Hello,

    in my Ubuntu 12.10 I have there:

    Error detected while processing /home/master/.vimrc:
    line 2:
    E319: Sorry, the command is not available in this version: filetype on
    line 3:
    E319: Sorry, the command is not available in this version: filetype indent on
    line 4:
    E319: Sorry, the command is not available in this version: filetype plugin on
    line 5:
    E319: Sorry, the command is not available in this version: syntax on

    How can I do please?

    Regards,

    Alex

  3. And of course, you could always use the standard for vi:
    H to go left,
    J to go down,
    K to go up and
    L to go right

    That is, when you’re not in editing mode!
    A lot of the above things work on Linux, but when you are working in a mixed environment like me where Linux, AIX, Sun and HP-UX are in use you better not use these tips since it will no work on some UNIX platforms.

    Good article, but I just wanted to point this out.

    Regards,
    Gert-Jan

Leave a Comment