2

While doing TDD I switch back and forth between windows often. I would like to reduce the number of keystrokes for this. I have tried both:

inoremap <C-w> <ESC><C-w>

and

imap <C-w> <ESC><C-w>

I was thinking with this mapping I could do <C-w>h and switch directly from the right window to the left window, but it doesn't work, it gets to normal mode but leaves me in the right window. Can anyone explain why this doesn't work and how I might make it work?

1
  • 4
    Welcome to Vi and Vim! I just tested your mapping (the first one) and it seems to work for me... so I wonder why it doesn't seem to be working in your case... What O.S./platform are you on? Are you using Vim on the terminal or a graphical version (GVim, MacVim, etc.)? I'm thinking it might be related to the terminal interpretation of some of these keystrokes (such as the "Esc" key...)
    – filbranden
    Jul 13 at 23:44
1

It should be:

inoremap <c-w> <esc><c-w>w

because <c-w>w is what switches windows

3
  • 1
    OP wants to have C-w available in insert mode the same as in normal mode -- providing <C-w>h, <C-w>l and other window mappings in insert mode.
    – Maxim Kim
    Jul 14 at 6:28
  • Oh, this changes things.. Then I'd say OP's best bet is to have multiple mappings for the commands he needs using <c-o>. inoremap <c-w>w <c-o><c-w>w inoremap <c-w>h <c-o><c-w>h and so on... Jul 14 at 10:19
  • 1
    well, the mapping in the question works for me...
    – Maxim Kim
    Jul 14 at 11:15
0

Type faster. Your first mapping (inoremap <C-w> <ESC><C-w>) works for me, but only if I type quickly. The <C-w> within the mapping times out if I'm too slow. While a <C-w> in normal mode without a mapping does not timeout.

You can see the timeout happen with :set showcmd. In the bottom right of Vim you'll see the current command you're typing. In normal mode typing <C-w> will show ^W and it will remain there until you type something else to complete the command. In insert mode with your mapping typing <C-w> shows ^W, but then it quickly disappears. This indicates your command timed out.

I wish I had a better answer. I tried modifying timeout, ttimeout, timeoutlen, and ttimeoutlen to give more time, but it didn't have any effect on the timeout.

2
  • I would love to know if there is a limitation with changing the timeout in this case, or I just failed to change it correctly.
    – Rob Mosher
    Jul 14 at 15:55
  • Changing timeoutlen should affect mappings, according to a comment in my vimrc 😅
    – D. Ben Knoble
    Jul 14 at 16:58

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.