3

If I'm in insert mode in Vim, and I press Control + W, it waits a moment, then deletes the previous word. If I start typing immediately after pressing it, it deletes the previous word instantly and replaces it with what I type.

In either case, the previous word is deleted and whatever I type next replaces it. So why the pause? Is there something else I can do at that point?

Update

This is on MacVim 7.3 (55) on OSX Snow Leopard. I just logged into a server with a vanilla config and tried it, and the deletion is instant. So maybe it's something with my config?

2
  • 1
    I can't reproduce the delay. What version of Vim and what operating system?
    – Mikel
    Mar 2, 2011 at 20:22
  • @Mikel - updated question. Mar 2, 2011 at 20:33

2 Answers 2

5

You might have a mapping that starts with Ctrl-W and that's why Vim waits for a while to see if you want to use it, check with :verbose imap <C-W>

3
  • Yep! This was in my .vimrc, which I intially got from a friend and have been tweaking since: imap <C-w><C-w> <esc><C-w><C-w> So he specifically mapped hitting "control + w" twice in insert mode to moving to the next window. Mar 2, 2011 at 21:43
  • Using imap for <C-w> in insert mode is really useful, I find; but in your case, the ambiguous key mapping is what's causing the weird delay. (I'll post an answer to elaborate)
    – Randall
    Apr 13, 2022 at 15:36
  • This is also handy for :verbose map [insertKeynames] and :verbose cmap [insertKeynames]. Feb 12 at 16:23
0

As Raimondi alluded to, your .vimrc has an ambiguous mapping for <C-w>.

What I find more useful is to just completely remap Ctrl-w to get the "change window" behavior I want:

imap <C-w> <esc><C-w>

Now, that allows me to still use <C-w><C-w> to go to the next window as your mapping did, but it allow allows me to use all of the window movements (see :help window-moving in Vim). To me, that's way more powerful, even though I lose the default <C-w> behavior.

Back to your delay, and ambiguous mapping. You have one defined in your .vimrc, and one mapping built-in to Vim, so you have:

imap <C-w> [built-in behavior: delete previous word] (not real syntax, just for illustration)
imap <C-w><C-w> <esc><C-w><C-w>

While Vi did not allow such mappings at all, Vim can deal with ambiguous mappings; The description in :help map-ambiguous in Vim explains your situation well:

                                                        map-ambiguous
When two mappings start with the same sequence of characters, they are
ambiguous.  Example:
    :imap aa foo
    :imap aaa bar
When Vim has read "aa", it will need to get another character to be able to
decide if "aa" or "aaa" should be mapped.  This means that after typing > "aa"
that mapping won't get expanded yet, Vim is waiting for another character.
If you type a space, then "foo" will get inserted, plus the space.  If you
type "a", then "bar" will get inserted. 
{Vi does not allow ambiguous mappings}

If you want to keep the <C-w> behavior available, your best bet is to probably map it to Ctrl-Backspace. It's not intuitive to do so, but per https://vim.fandom.com/wiki/Map_Ctrl-Backspace_to_delete_previous_word it is possible:

Mapping Ctrl-Backspace does not work in terminal Vim. Following is a workaround.

noremap! <C-BS> <C-w>
noremap! <C-h> <C-w>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .