Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

When I tried to use arrow keys in insert mode in vi editor the following characters are being inserted in the editor:

  • for I get B,
  • for I get A,
  • for I get D,
  • for I get C.

Please help me in resolve this problem.

share|improve this question

marked as duplicate by Radu Rădeanu, Mitch, guntbert, Warren Hill, aquaherd Oct 5 '13 at 21:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

If you dont already have a .vimrc file in your home directory, create one using this:

vim $HOME/.vimrc

Add this line to the top of the file:

set nocompatible

save the file and this should fix the problem for you :)

share|improve this answer
I tried the vim $HOME/.vimrc command but it has displayed the following message. – bvb Oct 5 '13 at 14:42
@bvb Can you please help us with what message vim $HOME/.vimrc displayed? – Gaurav Manchanda Oct 5 '13 at 15:09
I had to create a .exrc file for vi, but set nocompatible worked great. – tgun926 Jan 30 '15 at 23:38
There is a trick if you use vi instead of vim, you have to do the same configuration but in the ~/.exrc file. – tzi Jul 22 '15 at 14:47
Worked. But why this fix is required? Why by default it does not handle arrows? – Mohit Jan 23 at 12:41

With vi, when clicking on i you activate the command to Insert text.
This command allows you to insert text in your file.
And right, when clicking :

  • on it will insert a "A",
  • on it will insert a "B",
  • ...

Till you deactivate this command.
To deactivate a command in vi : just click on Esc
And then you will get back normal use of your arrow keys:

  • to go up,
  • to go down,
  • ...

FYI, here are some vi commands:
From this source.

:xReturn quit vi, writing out modified file to file named in original invocation
:wqReturn quit vi, writing out modified file to file named in original invocation
:qReturn quit (or exit) vi
:q!Return quit vi even though latest changes have not been saved for this vi call

move cursor down one line
move cursor up one line
move cursor left one character
move cursor right one character

u undo whatever you just did; a simple toggle
. redo whatever you just did

i insert text before cursor, until Esc hit
I insert text at beginning of current line, until Esc hit
a append text after cursor, until Esc hit
A append text to end of current line, until Esc hit
o open and put text in a new line below current line, until Esc hit
O open and put text in a new line above current line, until Esc hit

r replace single character under cursor (no Esc needed)
cw change the current word with new text,starting with the character under cursor, until Esc hit
x delete single character under cursor
Nx delete N characters, starting with character under cursor
dw delete the single word beginning with character under cursor
C change (replace) the characters in the current line, until Esc hit
D delete the remainder of the line, starting with current cursor position

dd delete entire current line
Ndd delete N lines, beginning with the current line; e.g., 5dd deletes 5 lines
yy copy (yank, cut) the current line into the buffer
Nyy copy (yank, cut) the next N lines, including the current line, into the buffer
p paste the line(s) in the buffer into the text after the current line

0 (zero) move cursor to start of current line (the one with the cursor)
$ move cursor to end of current line
w move cursor to beginning of next word
b move cursor back to beginning of preceding word
:0Return or 1G move cursor to first line in file
:nReturn or nG move cursor to line n
:$Return or G move cursor to last line in file

/string search forward for occurrence of string in text
?string search backward for occurrence of string in text
n move to next occurrence of search string
N move to next occurrence of search string in opposite direction

share|improve this answer
Arrow key down inserting "B" is not an expected behavior. Arrow keys should be moving cursors up and down/ left and right regardless of the insert mode. – James Wong Sep 6 '15 at 7:49

The traditional vi editor doesn't recognize the arrow keys (...afair). There are two solutions:

  1. use vim instead of vi
  2. use h, j, k and l to navigate (you can't navigate while editing on the traditional vi btw.)
share|improve this answer
This isn't true. Look for terminfo/termcap and libcurses based apps. I use vi since 1990 in Xenix, SCO Unix, AIX, old hpux, and others, and pure-vi from 80s already have support to terminfo/termcap mappings. – Luciano Dec 14 '15 at 11:42
@james-wong well then I should rephrase my answer. To "pure vi under Ubuntu doesn't..." – MadMike Dec 14 '15 at 14:52
... and this was meant as a comment to @luciano – MadMike Dec 14 '15 at 14:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.