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

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
    
You should not ask something like this on AU but try to find a how-to on vi with google. There are plenty of how-to's on the web. lowfatlinux.com/linux-editor-vi.html computerhope.com/unix/uvi.htm and not only in English but also in your language. – Rinzwind Nov 21 '12 at 8:44
    
Is there any specific reason for wanting to use vi as editor? I find using gedit (for simple text files) or jedit or geany (for html, php, css files) much easier and user-friendly. – To Do Nov 21 '12 at 10:10
3  
@Rinzwind I don't understand how this question is off topic here? Google sent me here, from here I should go back to Google. Sounds like a vicious circle. – Radu Rădeanu Oct 5 '13 at 7:55
    
@RaduRădeanu is has nothing to do with Ubuntu :+ – Rinzwind Oct 5 '13 at 8:27
3  
@Rinzwind As far as I know, Ubuntu comes with vi preinstalled. Now these questions about default behavior of Ubuntu programs are off topic here? – Radu Rădeanu Oct 5 '13 at 8:47

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

vim ~/.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
2  
@bvb Can you please help us with what message vim $HOME/.vimrc displayed? – Gaurav Manchanda Oct 5 '13 at 15:09
4  
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
2  
Worked. But why this fix is required? Why by default it does not handle arrows? – Mohit Jan 23 at 12:41
1  
By default, vi/vim is programmed to replace arrow keys with the keys h, j, k, l. This is done so you dont have to move from the major key area of the keyboard. – Gaurav Manchanda Jan 24 at 19:34

There are three modes in vi editor namely:

  • command mode
  • input mode
  • default mode.

When youu open a file, you are in default mode. Now if you want to go to a specific position in your text, just use arrow keys or use h, j, k, l keys. Note that this would work only when you have not pressed i (or any other input mode entering command like a, A, I).

The reason for 'B' may be because the arrow keys in input mode don't function as arrow keys, so just press Esc to go into default mode any time. When to shift to input mode press i or a, and to navigate just press i key and use arrow keys or h, j, k, l.

share|improve this answer

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
7  
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

Also try vim.

$ sudo apt-get install vim

There are many good vim/vi tutorials on youtube, or the web generally. For your problem, see here

share|improve this answer

To disable printing letters on pressing arrows in edit mode you can do following

vi $HOME/.exrc 

(create file if it does not exist) and then add line set nocompatible to it and save.

share|improve this answer
    
This should be the accepted answer. Thank you! – eis Nov 18 '15 at 10:10

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

Just do two things to connect your computer to the Internet.

  1. open terminal and type:

    sudo apt-get update
    

    (it will ask you your password and then hit enter)

  2. then type:

    sudo apt-get install vim  
    

It worked for me, hope it will also work for you.

share|improve this answer
    
Posted years ago: askubuntu.com/a/220072/158442 – muru Apr 12 at 7:52
    
Connect your computer to the internet? How? Also that doesn't answer the question. – Star OS Apr 12 at 11:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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