Hitting arrow keys adds characters in vi editor

325,172

Solution 1

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. :)

Solution 2

Installing the vim package will fix the problem:

sudo apt-get install vim

There are many good vim/vi tutorials on YouTube, or the web generally. For your problem, see the article 8 Essential Vim Editor Navigation Fundamentals.

Then continue to open files as usual:

vi desired-file

Solution 3

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

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

Till you deactivate this command.
To deactivate a command in vi: just press 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

Solution 4

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.

Solution 5

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:
325,172

Related videos on Youtube

bvb
Author by

bvb

Updated on September 18, 2022

Comments

  • bvb
    bvb over 1 year

    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.

    • vyi
      vyi almost 9 years
      I had a good laugh reading the title ;) I'm sure there is an XKCD for the spooky feeling that comes (to novice users) when arrow key prints letters.
    • Kyle Bridenstine
      Kyle Bridenstine almost 5 years
      It is very very scary ;)
    • OMA
      OMA almost 3 years
      It seems it's done on purpose in vim's code to mimic the behaviour of old vi, so my question would be: What was actually the point of the old vi having the arrow keys writing the letters ABCD on the document you're trying to insert text in instead of just moving the cursor? What's the actual "useful" use case for that?! I just can't fathom the reason for that.
    • Nathan Beach
      Nathan Beach almost 3 years
      @OMA every time I set up a new Linux instance I wonder the same thing... and I have to Google to figure out how to fix it. why on earth is this still the default setting.
  • tgun926
    tgun926 over 9 years
    I had to create a .exrc file for vi, but set nocompatible worked great.
  • tzi
    tzi almost 9 years
    There is a trick if you use vi instead of vim, you have to do the same configuration but in the ~/.exrc file.
  • Luciano
    Luciano over 8 years
    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.
  • MadMike
    MadMike over 8 years
    @james-wong well then I should rephrase my answer. To "pure vi under Ubuntu doesn't..."
  • MadMike
    MadMike over 8 years
    ... and this was meant as a comment to @luciano
  • Mohit
    Mohit over 8 years
    Worked. But why this fix is required? Why by default it does not handle arrows?
  • Gaurav Manchanda
    Gaurav Manchanda over 8 years
    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.
  • daVe
    daVe over 7 years
    next problem is backspace, solution: askubuntu.com/questions/296385/…
  • villapx
    villapx over 7 years
    @JamesWong Actually, it is expected behavior, as that is how Vi was originally programmed. VIM, (unofficially) the successor to Vi, is in Vi-compatible mode by default, which includes this behavior for the arrow keys. So the word "should" here is merely subjective, as VIM is actually doing what it was purposefully programmed to do.
  • villapx
    villapx over 7 years
    @Locane This isn't a Solaris thing, it's a VIM thing. It doesn't matter if you're on Solaris, Linux, UNIX, Windows, BSD--VIM always starts in Vi-compatible mode by default.
  • Amos Folarin
    Amos Folarin over 7 years
    worked for me -- though not sure why this is different from the vim I was launching previously
  • muru
    muru over 7 years
    I'm not sure what's so bizarre. You end up suggesting 1. install Vim - askubuntu.com/a/220072/158442, 2. creating ~/.vimrc and setting nocompatible: askubuntu.com/a/353944/158442, or 3. editing a system file. Only the suggestion to edit /etc/vim/vimrc.tiny is new, and yet you call answers bizarre while suggesting the same thing as them.
  • pauljohn32
    pauljohn32 about 7 years
    I put that in .vimrc. Why do you put it in .exrc?
  • William Robertson
    William Robertson almost 7 years
    Vim sources both - see output of :version for load order (also :h .exrc or :h init). I suppose someone might want a setting to apply in both vi and vim, or could be sheer force of habit.
  • Lockszmith
    Lockszmith almost 7 years
    This should be the accepted answer (but seems like no one is accepting). it's the simplest/cleanest way to get vim fully 'modernized' to a computer console (instead of tty/ANSI era terminals.
  • ArtOfWarfare
    ArtOfWarfare over 6 years
    @villapx - No, it isn't expected behavior. Expected behavior is defined by what your typical user expects. The typical person using vi/vim is using it because that's what's installed on a server that they need to quickly configure and they won't be doing enough to bother with installing something else. Few actually choose to use vim when given an option, and fewer actually know all these features or quirks. The typical user expects arrows to move the cursor - that is the expected behavior. Anything else is unexpected behavior, and should be written up as a bug for how unexpected it is.
  • ArtOfWarfare
    ArtOfWarfare over 6 years
    This worked for me. I'm surprised that raspian comes with the version of vi/vim that it does... although all the tutorials for it tell you to use nano, which it's hard to argue that vim is really better than it. Probably the only reason I use vim rather than nano is because vim is always on every machine and it seems like a 50/50 chance whether nano is.
  • villapx
    villapx over 6 years
    @ArtOfWarfare You're free to define "expected" however you choose. I'm defining it as what's expected by the author of the code, as they were quite clear in the VIM manual how this is supposed to behave...from :help compatible: "By default this option is on and the Vi defaults are used for the options. This default was chosen for those people who want to use Vim just like Vi, and don't even (want to) know about the 'compatible' option."
  • villapx
    villapx over 6 years
    @ArtOfWarfare Basically, the VIM author clearly had a specific audience in mind when writing this portion of the code, and just because that audience is different from what your audience would be, doesn't mean it's a bug. You can file it as a bug if you wish, but be prepared to be berated by VIM fundamentalists (and really anyone who respects the 'manual', a la RTFM aficionados).
  • Max Williams
    Max Williams about 6 years
    @tzi I use vi, not vim, but this ~/.vimrc change worked for me, too.
  • Steve Cohen
    Steve Cohen almost 6 years
    @Max_Williams It is possible that vi is aliased on your system to invoke vim. I have seen that before.
  • greg
    greg almost 6 years
    This solved the problem for me where the other answers didn't. Thanks!
  • Freedo
    Freedo about 5 years
    This did not work for me on ubuntu 18.04
  • Freedo
    Freedo about 5 years
    None of the answers here works for me on ubuntu 18.04
  • frakman1
    frakman1 almost 5 years
    I get this error when I start vi after using both the .exrc fix: compatible: No such option - 'set all' gives all option values Error detected in .exrc.[Hit return to continue] I am using: Version SVR4.0, Solaris 2.5.0
  • Wyck
    Wyck over 4 years
    This answer could be improved by including an explanation of why set nocompatible works or what it does.
  • seeker_of_bacon
    seeker_of_bacon over 4 years
    According to :help compatible set nocompatible is already implicitly applied if you have ~/.vimrc.
  • David I. Samudio
    David I. Samudio about 4 years
    Don't forget to restart your terminal for the changes to take effect.
  • aroth
    aroth about 4 years
    Didn't work for me, though my problem was slightly different. In my case hitting the arrow keys did nothing at all. Had to set term=builtin_ansi to fix it.
  • bluppfisk
    bluppfisk about 4 years
    tried some of the other answers here but for me (Ubuntu 19.10), editing the ~/.exrc file and adding the nocompatible worked perfectly.
  • kev
    kev about 4 years
    THIS IS THE ANSWER
  • pauljohn32
    pauljohn32 almost 4 years
    I agree with Gunstick. Other answers go off on how they use vi, without directly telling user what to do to make arrow keys work. Other bizarre thing is that the suggestion to "set nocompatible" is never accompanied by much concrete explanation of what the original setting does, or the dangers associated with changing it to"set nocompatible" (github.com/vim/vim/issues/1131)
  • kramfs
    kramfs over 3 years
    Fixed, worked for me on RaspberryPI while connected using putty. Thanks!
  • Andrea Girardi
    Andrea Girardi over 3 years
    This solves my issue also on Debian
  • takanuva15
    takanuva15 over 3 years
    Wish I'd done this earlier. Installing vim also automatically gives syntax highlighting to most standard code files that you open with vi xxx (which is a huge plus).
  • 7ochem
    7ochem about 3 years
    The answer states creating a ~/.vimrc. But when you are within a vi/vim session already, you can also just press Esc and type :set nocompatible or any of the other suggested options in the comments
  • OMA
    OMA almost 3 years
    @villapx : OK, so it's done on purpose to mimic the behaviour of old "vi", so my question would be: What was actually the point of the old "vi" having the arrow keys writing the letters ABCD on the document you're trying to insert text in instead of just moving the cursor? What's the actual "useful" use case for that?!
  • villapx
    villapx almost 3 years
    @OMA it's not "writing the letters ABCD"; every key on your keyboard sends a specific sequence of binary bits to the computer, which your terminal emulator forwards to the program running within it. It's then the program's job to interpret those bits into something. Vi is old (literally came out in 1976), and computers/terminals back then used totally different sequences of bits for the different non-alpha keys than today's keyboards do. The original Commodore 64 (1982) didn't even have arrow keys, for example. So one can imagine why Vi doesn't properly support today's arrow keys.
  • OMA
    OMA almost 3 years
    @villapx : Thanks for the explanation. My next question would then be: What's the point of modern vi/vim in perfectly emulating such old behaviour which doesn't make sense in nowadays computers? Modern Linux distros are usually used with modern computers (at least computers from THIS CENTURY!), so why do some Linux distros write ABCD in vi instead of moving the cursor, which is what any sensible person would expect of the arrow keys?
  • villapx
    villapx almost 3 years
    @OMA No problem. VIM is also old: it was first released in 1991. Perhaps the most impressive thing about VIM is its backwards compatibility, meaning if you wrote some .vimrc entries way back in 1997, they'd still work today. Hence the fact that compatible mode is still defaulted to "on". For those that want a powerful VIM-like editor but more modern and with much less emphasis on compatibility with things from the '70s, there's Neovim. Bonus, Neovim defaults to non-Vi-compatible mode
  • OMA
    OMA almost 3 years
    I didn't know about that editor, but I'm sure it's very competent and powerful. The problem is, it's not included stock with Linux distros! The sole reason I might use vi is because it's the only editor available when I'm connecting to a remote Linux server, so if I have to install another third party editor (which I might not have permissions for) then it's useless to me no matter how good it actually is. I still can't comprehend why an ANCIENT editor from 1976 is the only text editor that comes stock with all Linux distros, server or desktop, instead of a more modern text-only alternative.
  • Farshid.T
    Farshid.T about 2 years
    If running vi with sudo, the line should be added to /root/.exrc.