Hitting arrow keys adds characters in vi editor
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.
:x
Return quit vi, writing out modified file to file named in original invocation
:wq
Return quit vi, writing out modified file to file named in original invocation
:q
Return 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
:0
Return or 1G
move cursor to first line in file
:n
Return 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.
Related videos on Youtube
bvb
Updated on September 18, 2022Comments
-
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 almost 9 yearsI 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 almost 5 yearsIt is very very scary ;)
-
OMA almost 3 yearsIt 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 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 over 9 yearsI had to create a .exrc file for vi, but set nocompatible worked great.
-
tzi almost 9 yearsThere is a trick if you use
vi
instead ofvim
, you have to do the same configuration but in the~/.exrc
file. -
Luciano over 8 yearsThis 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 over 8 years@james-wong well then I should rephrase my answer. To "pure vi under Ubuntu doesn't..."
-
MadMike over 8 years... and this was meant as a comment to @luciano
-
Mohit over 8 yearsWorked. But why this fix is required? Why by default it does not handle arrows?
-
Gaurav Manchanda over 8 yearsBy 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 over 7 yearsnext problem is backspace, solution: askubuntu.com/questions/296385/…
-
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 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 over 7 yearsworked for me -- though not sure why this is different from the vim I was launching previously
-
muru over 7 yearsI'm not sure what's so bizarre. You end up suggesting 1. install Vim - askubuntu.com/a/220072/158442, 2. creating
~/.vimrc
and settingnocompatible
: 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 about 7 yearsI put that in .vimrc. Why do you put it in .exrc?
-
William Robertson almost 7 yearsVim 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 almost 7 yearsThis 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 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 over 6 yearsThis 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 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 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 about 6 years@tzi I use vi, not vim, but this ~/.vimrc change worked for me, too.
-
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 almost 6 yearsThis solved the problem for me where the other answers didn't. Thanks!
-
Freedo about 5 yearsThis did not work for me on ubuntu 18.04
-
Freedo about 5 yearsNone of the answers here works for me on ubuntu 18.04
-
frakman1 almost 5 yearsI 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 over 4 yearsThis answer could be improved by including an explanation of why
set nocompatible
works or what it does. -
seeker_of_bacon over 4 yearsAccording to
:help compatible
set nocompatible
is already implicitly applied if you have~/.vimrc
. -
David I. Samudio about 4 yearsDon't forget to restart your terminal for the changes to take effect.
-
aroth about 4 yearsDidn'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 about 4 yearstried some of the other answers here but for me (Ubuntu 19.10), editing the ~/.exrc file and adding the nocompatible worked perfectly.
-
kev about 4 yearsTHIS IS THE ANSWER
-
pauljohn32 almost 4 yearsI 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 over 3 yearsFixed, worked for me on RaspberryPI while connected using putty. Thanks!
-
Andrea Girardi over 3 yearsThis solves my issue also on Debian
-
takanuva15 over 3 yearsWish 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 about 3 yearsThe 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 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 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 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 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 almost 3 yearsI 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 about 2 yearsIf running
vi
withsudo
, the line should be added to/root/.exrc
.