3

I use a comma as my leader key. I would like to be able to press the comma twice rapidly (i.e., <leader>,) to achieve the native functionality of the comma (i.e., jump back after f/F and ;).

To that end, my .vimrc begins

let mapleader=","
map <leader>, ,

But when I press ,, it does nothing. What should I change to make it work?

Alex Roberts
  • 307
  • 1
  • 8

1 Answers1

4

It works for me, but there is a 1 second delay before the cursor moves to the left. This is because when ,, is pressed, vim translates it to ,, which further translates to the <leader> key. So, vim is waiting for additional input as there may be additional keystrokes after <leader>. This explains the 1 second delay.

The remedy is to use noremap instead of map. This tells vim to use a non-recursive mapping. So, if we use

let mapleader=","
noremap <leader>, ,

,, is translated to ,, and is not further translated to the <leader>. The cursor moves immediately to the left, as intended.

husB
  • 2,068
  • 5
  • 22
  • 2
    To add a few details to this answer: husB analyzed correctly the issue with the comma being interpreted as a leader and creating a delay, once possible solution would also be to tweak :h 'ttimeoutlen' (but that would impact all mappings). Using a non recursive mapping is the right solution, actually by default you should always be using non recusive mapping unless you have a very specific need and you know what you are doing. And furthermore it would be better to use mode specific mappings like nnoremap and xnoremap. – statox Mar 01 '22 at 14:39
  • Thank you. I just tested it with those two lines, and I found that you're right, it works. The trouble is that it doesn't work with my big, bloated .vimrc. So I will do some digging to see if I can find what is interfering. – Alex Roberts Mar 02 '22 at 04:49
  • @husB, a follow-up question for you or anyone else who knows: Do you have any suggestions for how to find what the problem is in my .vimrc that is preventing your answer from working with my setup? Are there any particular types of settings that might be expected to mess with <leader>,, or is there a way to list such mappings and find what <leader>, is mapped to? – Alex Roberts Sep 27 '22 at 18:31
  • When I type :map, the list includes the line ,, ,. Shouldn't that mean that it would work? – Alex Roberts Sep 27 '22 at 18:33
  • 1
    @AlexRoberts that should work, but there may be other mappings that take priority over your mapping... a more specific command would be :verbose filter /,/ map, which filters out mappings that contain ,. – husB Sep 28 '22 at 03:34
  • 1
    Regarding your first question, you could do a bisection search (as taken from here)-- commenting out half your vimrc and see if the problem persists. Then you would know which if the issue is in the former or latter half. Further comment out half of that (a quarter), ...until the problem is isolated :) – husB Sep 28 '22 at 03:37
  • Thank you, @husB. I began to try your suggestion for working my way through my .vimrc, but in the process I stumbled across a curious solution that I don't understand: when I press \, I get the old , functionality, even though I have set let mapleader=",". Even more strangely, \, works even when I delete noremap <leader>, , from my .vimrc. So though I have no idea why this works, it is my solution for now. – Alex Roberts Sep 29 '22 at 16:31
  • Ah, maybe it is from the plugin sneak. From running :map: x \ <Plug>Sneak_, o \ <Plug>Sneak_, n \ <Plug>Sneak_, – Alex Roberts Sep 29 '22 at 16:33
  • It seems that sneak has somehow replaced the usual , functionality with <Plug>Sneak_,, so here is the solution when using vim-sneak: noremap <leader>, <Plug>Sneak_,. Now ,, works. – Alex Roberts Sep 29 '22 at 18:59
  • In case anyone stumbles across my long series of comments/thinking aloud: ,, is too slow when I do noremap <leader>, <Plug>Sneak_,, and \ is inconvenient, so I've now added nnoremap '; <Plug>SneakPrevious to my .vimrc. There is no delay, and it's easy to type '; right after typing ;. – Alex Roberts Sep 29 '22 at 20:28