Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrow keys don't work in vim while using bash from Cmder. #1154

Closed
dsouzadyn opened this issue Oct 2, 2016 · 18 comments
Closed

Arrow keys don't work in vim while using bash from Cmder. #1154

dsouzadyn opened this issue Oct 2, 2016 · 18 comments

Comments

@dsouzadyn
Copy link

dsouzadyn commented Oct 2, 2016

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.
@fpqc
Copy link

fpqc commented Oct 2, 2016

@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).

@rmobis
Copy link

rmobis commented Oct 3, 2016

See cmderdev/cmder#901 and this ConEmu release for a solution.

@fpqc
Copy link

fpqc commented Oct 3, 2016

Yes you need -cur_console:p

@zadjii-msft
Copy link
Member

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

@AuthorProxy
Copy link

AuthorProxy commented Oct 18, 2016

Guys, how can I echo something or run any other command after zsh has loaded?
I was tried:
%windir%\System32\bash.exe ~ -c zsh & echo test -new_console:pad:C:\ProxyDocuments\Projects
but this doesn't work

Also why zsh git is so slow and old (1.9.1) compared with my default portable version (2.10.1)..

@zadjii-msft
Copy link
Member

@AuthorProxy Could you open a new issue for this? I'd rather not pollute an old, closed thread.

@fpqc
Copy link

fpqc commented Oct 18, 2016

@zadjii-msft Also his question should go to the ConEmu issues page or go to @Maximus5 on twitter.

@Maximus5
Copy link

This is not a bug in ConEmu. This is a bug of conhost/BashOnWindows, mentioned here long ago
#111

ConEmu tries to provide workaround for this conhost bug, but due to the lack of API its impossible for every possible case.

@Maximus5
Copy link

It also depends on another bug: #406

@fpqc
Copy link

fpqc commented Oct 18, 2016

@Maximus5 Yeah, but they said they're working on a proper new console API now. So in the meantime, since you have workarounds, there is no point sending people here.

Look at miniksa's comments if you doubt it:

#111 (comment)

@DanielGGordon
Copy link

What is the current status of the latest Windows build working with Cmder?

  1. Colors
  2. Arrow keys
  3. Zsh?

I'm very excited to one day have a Windows machine with the new Bash on Windows - inside Cmder, with working keys and colors - and oh-my-zsh. That would just be so awesome.

@zadjii-msft
Copy link
Member

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.

@adorkablue
Copy link

adorkablue commented Oct 27, 2017

@DanielGGordon
emmm Seems it works very well on Version 1709 (with some small bug fix tricks)

Colors
snipaste_2017-10-28_02-46-05

Arrow keys
Temporary fix this by adding

function zsh () {
    bash -cur_console:p
}

to %CmderDIR%/vendor/profile.ps1 and "zsh" in Cmder (Powershell, for cmd it is the clink.lua)
Note: This first start bash, and I've modified the .bashrc to let it switch to zsh automatically.
Add this to $Home/.bashrc:

if test -t 1; then
exec zsh
fi

(oh-my-)zsh
As the picture above : )

@gsusI
Copy link

gsusI commented Feb 13, 2020

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

@sarlaccpit
Copy link

sarlaccpit commented Feb 13, 2020

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

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

@ferenczy
Copy link

ferenczy commented Mar 6, 2020

echo ':set term=builtin_ansi'

Interesting. In my case, it affected syntax highlighting colors but mainly it hid the text cursor in Vim so it's not usable for me, unfortunately. You can't really edit a text if you don't see where the cursor is.

Ubuntu 18.04 on Windows 10 Enterprise version 1809

@ferenczy
Copy link

ferenczy commented Mar 6, 2020

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 Ctrl+K followed by the actual key. For example for the Left 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 Ctrl+V and then the key. For example, for the Left 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 Ctrl+V followed by the Up arrow key, Vim inserts its key code
  5. Press Enter and repeat the process for rest of the keys: Down, Right and Left

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.

@satriajanaka09
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests