WSL: Arrow keys don't work in vim while using bash from Cmder.

I discovered an issue when using Vim. It does not allow me to use the arrow keys at all and gives me an error in vim. This issue only occurs when you use bash from within Cmder. I’m using Cmder which uses ConEmu version 160914 [64]. capture

Reproduce using the following steps:

  1. Start Cmder.
  2. Type in bash (unless it’s your default shell).
  3. Open a file in vim with vim <filename>.
  4. Type in anything and try to move around with the arrows.

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Reactions: 16
  • Comments: 18 (3 by maintainers)

Most upvoted comments

This worked for me echo ':set term=builtin_ansi' >> ~/.vimrc

It took me some time and many unsuccessful attempts, but finally I managed to fix the issue while not breaking colors or any other things!

Instead of changing terminals and modes, I focused on key codes generated by the cursor keys and what Vim has mapped to the cursor movement.

TL;DR

To fix the cursor movement using the arrow keys, add the following commands into the .vimrc file:

set <Up>=^[[A
set <Down>=^[[B
set <Right>=^[[C
set <Left>=^[[D

Tested in Vim 8.0 under Ubuntu 18.04 bash executed in ConEmu 191012 on Windows 10 Enterprise version 1809.

Note

Unfortunately, the key codes generated by the keyboard are different under Windows Console (e.g. ^[OA) and ConEmu (e.g. ^[[A) so it may not work in both at the same time.

Explanation

My goal was to find out what actual key codes the arrow keys are generating on my system and what key codes has Vim mapped to its cursor movement actions.

Key codes assigned to cursor movement actions

In Vim, execute :set <key-name>? to print a key code currently mapped to a specific key name. You can find the key names in this list or by switching Vim to the Insert mode and pressing <kbd>Ctrl</kbd>+<kbd>K</kbd> followed by the actual key. For example for the <kbd>Left</kbd> arrow key, the name is <Left>, so I executed the command :set <Left>? to get the following output:

t_kl <Left>      ^[O*D

That means, the key named <Left>, which means cursor-left, is mapped to the terminal option t_kl and has assigned the key code ^[O*D.

Key codes generated by keyboard

To get the actual key codes generated by the keyboard, switch to the Insert mode, press <kbd>Ctrl</kbd>+<kbd>V</kbd> and then the key. For example, for the <kbd>Left</kbd> arrow key, it prints the code ^[[D on my system, which is clearly different from the key code ^[O*D assigned to the key name <Left>.

Fix

So what’s needed is simply to map Vim’s key names to the same key codes as generated by the keyboard:

:set <Left>=^[[D

The easiest way to configure your keys is:

  1. Open .vimrc in Vim
  2. Switch to the Insert mode
  3. type set <Up>=
  4. Press <kbd>Ctrl</kbd>+<kbd>V</kbd> followed by the <kbd>Up</kbd> arrow key, Vim inserts its key code
  5. Press <kbd>Enter</kbd> and repeat the process for rest of the keys: <kbd>Down</kbd>, <kbd>Right</kbd> and <kbd>Left</kbd>

The final product should look similar to the following:

set <Up>=^[[A
set <Down>=^[[B
set <Right>=^[[C
set <Left>=^[[D

Now, the cursor keys work in all Vim modes. At least for me 😃

Credits

It was this answer on the Stack Exchange’s Q&A site Vi and Vim which greatly helped me to figure the things out.

This worked for me echo ':set term=builtin_ansi' >> ~/vimrc

worked for me too. Just has to be ~/.vimrc

I’m going to close this, as it’s a conemu/cmder problem, and not a WSL/conhost one

This worked for me echo ':set term=builtin_ansi' >> ~/.vimrc

This worked for me as well. I use ConEmu. set nocompatible on both .vimrc and .xhrc did not work. I only added :set term=builtin_ansi in .vimrc and now the arrow key works as I expected.

I wouldn’t expect any more improvements in the coming release in terms of supporting conemu/cmder. It’s on our radar, but it’s not going to make it for this release.

Yes you need -cur_console:p

@dsouzadyn The arrow keys are bound to diff escape sequences. You need to use a special mode in cmder or conemu. This is a bug in their software (not detecting that terminal processing is enabled).