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?

Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements vim editor
Time 2m
[/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
# 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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 5 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
5 comments… add one
  • Jalal Hajigholamali Nov 29, 2012 @ 3:48


    Thanks, Nice article…

  • Vladimir Nov 30, 2012 @ 12:02

    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

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

    This .exrc file maps the arrow keys and backspace at the original vi editor
    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

  • Alex Sep 3, 2013 @ 20:57


    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?



    • SK Jun 26, 2015 @ 8:23

      Hi Alex,

      I faced the same problem. I did the following:

      # sudo apt-get install vim

      vi is not able to handle much of these extras. vim does.


  • Gert-Jan Feb 12, 2015 @ 16:13

    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.


Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @