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.
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityIf 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. :)
vi
instead of vim
, you have to do the same configuration but in the ~/.exrc
file.
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
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:
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:
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
: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."
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.
: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.
May 27, 2017 at 8:46
There are three modes in vi editor namely:
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.
I had the exact same Problem but not only on my local machine but also on connections via putty on a sles machine in a Win7 VM over a citrix receiver. Both the local host and the remote hosts show after a reinstall of Ubuntu had the exact same problem.
After finding out that
$ localectl list-keymaps
Couldn't find any console keymaps.
Causes this strange VI behaviour I installed the package console-data which solves the issue!
sudo apt-get install console-data
Here is an explanation:
Vi is an editor that's been around a long while, with roots back to the Unix systems. It's a good tool but it has been iterated on and now there is better. Vim was written later and is that iteration. People commonly refer to vi as 'vim-minimal' and to vim is an "improved" version of vi. You can think of it like this: vi is the core and vim expands on it.
Likely how you discovered the behavior:
In order for you to have discovered this problem, you pressed "i" to enter into insert mode and then used your arrow keys to navigate the correct line and edit point. This is what caused your unexpected char issue.
Solution 1 or 2
vi someFile
While using vi, first move to the edit point, then enter into insert mode.
vim someFile
Simply use vim from the start.
I cannot stress enough to read the manual (man pages) or to use -h for help. Once you adjust to the patterns in the shell, man pages become your best friend. Example: man vi
or man vim
Validate that Vim is installed.
Open a console window and execute:
dpkg --list | grep vim
If you get something similar to this, then you can just start using vim.
~ $ dpkg --list | grep vim
ii vim 2:7.4.052-1ubuntu3 amd64 Vi IMproved - enhanced vi editor
ii vim-common 2:7.4.052-1ubuntu3 amd64 Vi IMproved - Common files
ii vim-runtime 2:7.4.052-1ubuntu3 all Vi IMproved - Runtime files
ii vim-tiny 2:7.4.052-1ubuntu3 amd64 Vi IMproved - enhanced vi editor - compact version
If the command prompt returns with nothing, then vim is is not installed. Use the below examples to update your repositories and then install vim.
sudo apt-get update
sudo apt-get install vim
On the off chance that you use vi or vim only when searching on the internet how to fix something and you happen to forget to type vim
, there is one way that you can fix it for the future. I caution you to pay attention. You can use an alias to overwrite vi with a reference to vim so that no matter what you type in the infrequent future, you get vim without that "oops!" moment. You can always delete an alias later.
Type vim .bashrc
. Move down to the empty line with only a ~
(or to any other line that starts with alias
(so they're grouped together) and then press the i key to enter -insert- mode. On its own separate line, enter the following:
alias vi='vim'
Press Esc
and then type :wq
. Now you need to reload the file. Type source .bashrc
Any other shell that opens after you have made these edits will automatically load the .bashrc file.
This can also be due to the TERM
variable; set it for example like this:
export TERM=xterm
All the explanations given are a bit bizarre. I have vi but not vim installed.
Checking alternatives: /etc/alternatives/vi
→ /usr/bin/vim.tiny
So vi
is vim.tiny
, and it uses /etc/vim/vimrc.tiny
.
And in vimrc.tiny
you find the following line:
set compatible
Which is almost the only thing in this file. Note that in /etc/vim/vimrc
it is using nocompatible
.
So either install vim, or change the vimrc.tiny
file, or create your own ~/.vimrc
which should contain the line:
set nocompatible
~/.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.
On some systems, the nocompatible
option is not available, or it may not fix the problem if a plugin breaks arrow key functionality. Here's a workaround...
Edit the .exrc
file to include the following lines:
map! ^[OA ^[ka
map! ^[OB ^[ja
map! ^[OC ^[la
map! ^[OD ^[ha
Or, if the problem only exists in normal mode, you can change to nmap
and skip the following a
, as follows:
nmap! ^[OA ^[k
and so on.
Here's the key combinations needed to produce them (showing first one only):
map! <CTRL-V><Up-Arrow> <CTRL-V><ESC>ka
This escapes edit mode, moves in the wanted direction, and reenters edit mode. This is needed to overcome Vi reading the escape sequence as a sequence of literal <ESC><O><A>, etc.
For VI create or edit the file "~/.exrc" or VIM create or edit the file "~/.vimrc" set or add in the line.
set nocompatible
At the time of this writing there are commends on the second most voted item that are wondering why their entry was not accepted. The reason is there are hosts that do not allow installing other editors. Even then vim sometimes has the same effects, so there is a bunch of usable info from the top two entries. I cannot comment however hopefully this clears up reasons why neither one is 100% correct 100% of the time.
The traditional vi editor doesn't recognize the arrow keys (...afair). There are two solutions:
On this machine I had to make sure
that
set nocompatible
is the last entry in ~/.vimrc
. Or at least one of the last entries, in particular no more plugin is loaded afterwards.
HTH
For some reason, Ubuntu has omitted initialising envvar HOME for user root on my new 18.04 VPS. If that is the case, vi cannnot find the .vimrc file. The problem does not occur for normal users (in /home/).
To check the variable:
echo $HOME
If this results in a blank line, the variable is not set. The problem went away after
export HOME=/root
I moved the command into the /root/.bashrc file (I don't know where this is usually set).
This may happen because Vim received no (or wrong) information about the terminal you are using and therefore can't recognise the key presses correctly.
Since Vim tries to get this information from the TERM
variable, this could be fixed by running
export TERM=linux
before starting Vim.
This is similar to Jasper Nuyens's answer about setting it to xterm
. xterm
, xterm-color
, and xterm-256color
relate to graphical terminal programs. linux
is the terminal type set in virtual consoles and is worth trying if you're not in a graphical terminal, or if xterm
was already set or otherwise not effective.
I would recommend trying out gvim
.
sudo apt install vim-gtk3
Gvim adds a graphical user interface on top of vim, and it does provide a proper cursor by default as well.
askubuntu
won't allow me to answer b/c it's a "highly active question".