Linux Vi Arrow Keys Broken in Insert Mode

Linux vi arrow keys broken in insert mode

I presume you're using vim as this is tagged as Linux. Try:

:set nocompatible

(You may want to configure your .vimrc with this by adding this command to it. Create a new .vimrc file if not already present in your home directory, run echo $HOME to check home directory path.)

Arrow keys not working in vi/vim in remote ubuntu terminal

Arrow keys are useful when in insert mode. It may be that your vim installation is not a full one per Linux vi arrow keys broken in insert mode in which case follow the instructions in the answer by Han.

Arrow keys in vim (linux) in insert mode broken for me

If these keys work fine in normal mode, but do not in insert then you must have some mappings to the first one or two characters (normally <Up> is either <Esc>[A (terminals that use CSI) or <Esc>OA (xterm)). Try checking out output of

verbose imap <Esc>

, there should be not much mappings starting with <Esc> in insert mode (I have none, for example). I can say, that with arrow keys working normally in insert mode, using

inoremap <Esc> <Esc>

produces just the same behavior as if you had problems with terminal recognition or had 'compatible' set.

Cannot use arrows in VI on some consoles

Try using Vim instead of vi. I was forced to use vi at uni. It's crap.

Why arrow keys are not recommended in Vim

Using the arrow keys is considered a bad habit, because if you're using the arrow keys you're probably missing out on many of vim's lovely features.

When people first start to use vim, they tend to stay in insert mode since this is more like conventional text editing. To be effective in vim however, you should only be in insert mode when you're actually entering text. If you want to move the cursor around, you should be in normal mode. You should pretty much be in normal mode by default.

In normal mode there are a million shortcuts for moving around. You can use hjkl to move around one space at a time, or you can move by words, paragraphs and so on. If you're in normal mode, there's no point in using the arrow keys instead of hjkl since they're further away.

There's the attitude that if you're using the arrow keys, you're using vim "wrong". The truth is that vim has a really really steep learning curve, so while you're learning do whatever keeps you sane. When I first started using vim I did everything the "wrong" way and I didn't have any problems breaking the habits once I learned more of vim's commands.

As an example, when I first started, say I wanted to change the text in quotes:

String mystring = "I want to change this";

I would go into insert mode, move to the end of the string using the arrow keys, press backspace until the string was gone, and enter the new text.

A much better way is to put your cursor anywhere in the string (normal mode), then press ci". This will change in ". It will delete everything between the quotes and put you into insert mode so that I can enter the new text.

Traversing text in Insert mode

While it may make sense that you should be able to use the h j k l keys to traverse the editor in insert mode, but that is actually not the way Vim is intended to be used! There are many commands that Vim provides to make editing faster and easier.

The right way is to press Esc, go where you want to do a small correction, fix it, go back and keep editing. It is effective because Vim has much more movements than usual character forward/backward/up/down. After you learn more of them, this will happen to be more productive.

Here are a couple of use-cases:

  • You accidentally typed "accifentally". No problem, the sequence EscFfrdA will correct the mistake and bring you back to where you were editing. The Ff movement will move your cursor backwards to the first encountered "f" character. Compare that with Ctrl+DeldEnd, which does virtually the same in a casual editor, but takes more keystrokes and makes you move your hand out of the alphanumeric area of the keyboard.
  • You accidentally typed "you accidentally typed", but want to correct it to "you intentionally typed". Then Esc2bcw will erase the word you want to fix and bring you to insert mode, so you can immediately retype it. To get back to editing, just press A instead of End, so you don't have to move your hand to reach the End key.
  • You accidentally typed "mouse" instead of "mice". No problem - the good old Ctrl+w will delete the previous word without leaving insert mode. And it happens to be much faster to erase a small word than to fix errors within it. I'm so used to it that I had closed the browser page when I was typing this message...!
  • Repetition count is largely underused. Before making a movement, you can type a number; and the movement will be repeated this number of times. For example, 15h will bring your cursor 15 characters back and 4j will move your cursor 4 lines down. Start using them and you'll get used to it soon. If you made a mistake ten characters back from your cursor, you'll find out that pressing the key 10 times is much slower than the iterative approach to moving the cursor. So you can instead quickly type the keys 12h (as a rough of guess how many characters back that you need to move your cursor), and immediately move forward twice with ll to quickly correct the error.

But, if you still want to do small text traversals without leaving insert mode, follow rson's advice and use Ctrl+O. Taking the first example that I mentioned above, Ctrl+OFf will move you to a previous "f" character and leave you in insert mode.

Vim - How do I show a helpful error message when using the arrow keys in insert mode?


:inoremap <up> <Esc>:echoerr 'MSG HERE'<CR>

Prevent arrow keys from breaking down dot command

The only way to support redo, consists in using <C-G>U before <right> and <left>. This requires has('patch-7.4.849')

If you need to move to the end of the line, you'll need to count. If you need to move to the next line, you'll loose redo.



Related Topics



Leave a reply



Submit