1

What I tried:

 :nnoremap <C-w>n <Enter>

Expected result:

  • When pressing Ctrl+w then n, just an Enter is simulated rather than horizontally splitting a window.

Actual result:

  • The mapping sometimes works but sometimes not.

What I noticed:

  • When lhs of :map is of the form <C-X>Y where X and Y are arbitrary characteres, mapping works only when you press Ctrl+X and then rapidly press Y. If there is a delay (maybe hundreds milliseconds or so) before pressing Y, mapping does not work and original key-binding is used. By "original key-binding", I mean the un-mapped version of Ctrl+X + Y, not just Y. So it seems this strange behavior doesn't come from time-out of Ctrl+X. (If so, it would be natural that un-mapped version of just Y is simulated.)

Environment:

~ $ vi --version
VIM - Vi IMproved 8.2 (2019 Dec 12, compiled Feb 15 2020 13:52:40)
Included patches: 1-259

How can I map to Ctrl+w n? Or does the behavior natural?

  • 2
    Try set notimeout and check it it would work for you. (It did for me) – Maxim Kim Feb 16 at 8:11
  • 1
    @MaximKim It worked like a charm. Thank you. – ynn Feb 16 at 10:18
  • @MaximKim worth an answer; the intricacies and interactions of the various timeout options can be confusing. – D. Ben Knoble Feb 16 at 15:00
  • @D.BenKnoble I wouldn't be able to explain what are the drawbacks of using or not using this settings... – Maxim Kim Feb 16 at 15:11
2

So, first, the breakdown on mappings and "timing out":

'timeout'       boolean (default on)
'ttimeout'      boolean (default off, set in |defaults.vim|)
    These two options together determine the behavior when part of a
    mapped key sequence or keyboard code has been received:

    'timeout'    'ttimeout'     action
       off      off             do not time out
       on       on or off       time out on :mappings and key codes
       off      on              time out on key codes

    [snipped]

Essentially, by default, vim will time out on :mapped sequences and special key codes (like the <Esc>. that is the equivalent of pressing <A-.>).

You can control the length of said timeout with timeoutlen and ttimeoutlen:

'timeoutlen'    number  (default 1000)
'ttimeoutlen'   number  (default -1, set to 100 in |defaults.vim|)
    The time in milliseconds that is waited for a key code or mapped key
    sequence to complete.  Also used for CTRL-\ CTRL-N and CTRL-\ CTRL-G
    when part of a command has been typed.
    Normally only 'timeoutlen' is used and 'ttimeoutlen' is -1.  When a
    different timeout value for key codes is desired set 'ttimeoutlen' to
    a non-negative number.

        ttimeoutlen mapping delay      key code delay
           < 0      'timeoutlen'       'timeoutlen'
          >= 0      'timeoutlen'       'ttimeoutlen'

    The timeout only happens when the 'timeout' and 'ttimeout' options
    tell so.  A useful setting would be
        :set timeout timeoutlen=3000 ttimeoutlen=100
    (time out on mapping after three seconds, time out on key codes after
    a tenth of a second).

So it sounds like by default, you should get at least 1 second for mappings and key-codes alike, unless you're using the line from defaults.vim such that key-codes only get 100s.


Options for a solution

  1. Disable timeouts entirely (set notimeout nottimeout)—personally, I don't recommend this, but YMMV.
  2. Enable timeouts on mappings as longer, and keycodes as shorter (set timeoutlen=3000 ttimeoutlen=100)—this is what I've been using and I've not had any issues. Your mapping works for me with this setup.

Understanding where the issue might be

Try running

verbose set timeout? ttimeout? timeoutlen? ttimeoutlen?

The results should be informative.

| improve this answer | |
  • Thank you. In .vimrc, I set set timeoutlen=300, and this is the cause. I didn't know one can set different delays for mappings and key codes. I'll additionally set ttimeoutlen as you recommend. – ynn Feb 18 at 23:54

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.