I am trying to create some keybinds (in my .vimrc) for vim, used from Cygwin's mintty.

I have found and extended some code for the .vimrc that translates the keycodes used by mintty (https://github.com/mintty/mintty/wiki/Keycodes) back into the correct input.

for [key, code] in [
        \["<F1>", "\eOP"],
        \["<F2>", "\eOQ"],
        \["<F3>", "\eOR"],
        \["<F4>", "\eOS"],
        \["<F5>", "\e[15~"],
        \["<Home>", "\e[H"],
        \["<End>", "\e[F"],
        \["<S-Up>", "\e[1;2A"],
        \["<S-Down>", "\e[1;2B"],
        \["<S-Right>", "\e[1;2C"],
        \["<S-Left>", "\e[1;2D"],
    execute "set" key."=".code

Most of these work fine, but when I press shift and the down-arrow in insert mode, vim simply prints [1;2A, similar for Shift-Down. What is the problem?

  • 2
    Welcome to Vi and Vim! Are you pressing shift-down in insert mode? normal mode? other? Please edit to clarify. If you're new to stackexchange, I also recommend the tour and the help center. – D. Ben Knoble Mar 12 '20 at 2:16
  • In insert mode, clarified this in my question. (Actually, I'm trying to set vim up to be modeless, so everything happens in insert mode ...) – LaReine Mar 12 '20 at 4:17

I'm not familiar with mintty, but a quick google search returned the following:

1. mintty tips:

Unexpected behaviour with certain applications (e.g. vim)

If for example the PgUp and PgDn keys do not work in your editor, the reason may be that in the mintty Options, the Terminal Type was set to "vt100" and based on the resulting setting of the environment variable TERM, the application expects other key sequences than mintty sends. (While mintty could be changed to send VT100 application keypad codes in that case, the current behaviour is compatible with xterm.)


Avoiding escape timeout issues in vim

It’s a historical flaw of Unix terminals that the keycode of the escape key, i.e. the escape character, also appears at the start of many other keycodes. This means that on seeing an escape character, an application cannot be sure whether to treat it as an escape key press or whether to expect more characters to complete a longer keycode.

Therefore they tend to employ a timeout to decide. The delay on the escape key can be annoying though, particularly with the mode-dependent cursor above enabled. The timeout approach can also fail on slow connections or a heavily loaded machine.

Mintty’s “application escape key mode” can be used to avoid this by switching the escape key to an unambiguous keycode. Add the following to ~/.vimrc to employ it in vim:

let &t_ti.="\e[?7727h"
let &t_te.="\e[?7727l"
noremap <Esc>O[ <Esc>
noremap! <Esc>O[ <Esc>

2. possibly related question in SO: vim mintty/putty mapping

You can try to set the terminal's keycodes to vim's keycodes like this:

set <c-tab>=^[[1;5I

Similar to the description of that question, you can verify what Vim receives when you press the code following the steps at Vim faq-20.4.

  • Thanks, reading again through the mintty tips solved the problem. The TERM variable was set to cygwin, which is needed to solve some other problems, but leads to vim not recognizing the keysequences. Startying vim with -T xterm solves the problem. Regarding 2.: If you look into the code in the original post, you will find that it does exactly what your code also does. Problem is that it does not work for some of the keys. – LaReine Mar 12 '20 at 16:26
  • @LaReine great, I'm glad you figured it out! – mMontu Mar 16 '20 at 18:43

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.