1

I like to open Vim with many buffers open, and switch between them using plugins. Because I work a lot in C++, I like to have a way to quickly switch between header files and their corresponding source files and vice-versa. With some work, I have created these two bindings which essentially do the same thing: find a buffer with the same name as the currently opened buffer, but with a .c* extension if my current buffer is a header file, and with a .h* extension if my current buffer is a source.

My puzzlement derives from the fact that the binding that opens the other file using a split generally executes instantly, while the one that replaces the current buffer takes a second or so before it terminates. However, running the :buffer command manually is instant. Why does this happen?

This question seems similar to this one, but as far as I can see my bindings don't really shadow any control codes.

Here are the two bindings (the top one opens a split, the second one replaces):

nmap <silent><leader>v :execute 'vert sbuffer' '\<'.expand("%:t:s,\\v\\.h(..)?$,.X123X,:s,\\v\\.c(..)?$,.h,:s,.X123X$,.c,")<CR>
nmap <silent><leader>c :execute 'buffer' '\<'.expand("%:t:s,\\v\\.h(..)?$,.X123X,:s,\\v\\.c(..)?$,.h,:s,.X123X$,.c,")<CR>
3
  • 1
    Could you tell us what is the result of the nmap <leader>c command on your system? Commented Sep 10 at 10:54
  • 1
    @VivianDeSmedt And I think you solved my problem! I had a command for <leader>cd so I guess vim was trying to wait to see if I was going to type that one. Feel free to put that as an answer (or just the method to verify whether a map maps to more things) and I'll gladly mark it accepted. Thanks!!
    – Svalorzen
    Commented Sep 10 at 12:57
  • Thanks for the feedback. I wrote an answer with some additional information. Feel free to comment it. I'll be glad to improve it :-) Commented Sep 10 at 13:29

1 Answer 1

2

Very often the delay are due to the interval ('timeoutlen') that Vim wait before deciding which mapping to trigger when you have overlapping mappings (in your case: <leader>c and <leader>cd)

After hitting <leader>c Vim waits timeoutlen microseconds for an additional d before deciding that your intention was <leader>c.

Very often there are numerous mappings that could conflict. A good way to detect the problem is to use the nmap command that allow you to list all the normal mode mappings that share a given prefix.

In your case:

nmap <leader>c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.