3

Usually when I want to run vim in a desktop konsole session, I make sure the TERM variable is set to linux as the arrow keys don't work in insert mode with TERM=xterm. They show capital A..D instead of moving the cursor.

Now I think this is the wrong way of doing thingsas konsole or xterm is actually of type xterm.

Is there a better way for making arrow keys work under vi / vim?

OS: Kubuntu 13.04 (Clean install)

  • Are you sure you've not changed the default Key bindings in the current profile? – Stéphane Chazelas Aug 14 '13 at 17:45
  • Clean install on Ubuntu. – jippie Aug 14 '13 at 17:50
  • That's trying to use the arrow keys in insert mode without having done set nocp, right? – Stéphane Chazelas Aug 14 '13 at 19:33
  • @StephaneChazelas Insert mode and before I do set nocp, yes. – jippie Aug 14 '13 at 19:57
  • maybe worth an answer ... – jippie Aug 14 '13 at 19:58
5

I think I know what's going on.

You've not configured vim to disable vi compatibility mode.

In vi, you can only press the arrow keys in command mode. When in insert mode, the character sequences sent upon pressing the arrow keys are treated as if the characters were entered individually.

Now, upon startup, vi sets the terminal in keypad transmit mode (kx). In that mode, the terminals usually send a different character sequence upon pressing the arrow keys than when not.

For instance, the Up key sends \eOA in kx mode and \e[A when not.

To set the terminal in kx mode, it looks up the smkx (Set Mode Keypad Transmit) capability in the terminfo database for the current value of $TERM.

If you look at the smkx capability for xterm, konsole and linux, you see:

$ infocmp -1 linux | grep smkx
$ infocmp -1 xterm | grep smkx
    smkx=\E[?1h\E=,
$ infocmp -1 konsole | grep smkx
    smkx=\E[?1h\E=,

That is smkx is not defined in the linux entry.

What that means is that when you start vim with TERM=linux, vim doesn't set the terminal in kx mode, that means it sends \e[A instead of \eOA when you press the Up key for instance.

Now, what does \eOA do in vim when you're in insert mode? \e (ESC), leaves insert mode and returns to command mode. O opens a new line and enters insert mode again, A inserts the A character.

While \e[A, \e (ESC) leaves insert mode, [A is an unknown combination (in vim, [ is a prefix and is always followed by another character), so does nothing.

So what you need to do is: keep $TERM as it is, don't use arrow keys in insert mode when in vi compatibility or add a set nocp to your ~/.vimrc so you can use arrow keys in insert mode.

  • Yeah I figured out the "don't use arrow keys in insert mode when in vi compatibility"-option myself, but I didn't like that ;o) – jippie Aug 15 '13 at 4:54
-2

The fast way to fix this is:

echo "set nocp" > ~/.vimrc

This should fix it :)

  • 2
    You should improve your answer and include why this solves the problem. – countermode Nov 30 '16 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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