4

I don't actually want f itself to wrap across lines, but I would like to know how I can create a single key mapping which will jump to the next instance of the letter which follows it.

The mapping could be f or something else; perhaps I would use <C-/> which seems unbound.

The intention is that it would operate sort of like a function, and would take the next letter as its argument and perform:

/<argument><CR>

Is this possible, or would there have to be a separate key mapping for every letter, e.g.

nnoremap <C-/>a /a<CR>
nnoremap <C-/>b /b<CR>
...

?

statox
  • 49,782
  • 19
  • 148
  • 225
Wildcard
  • 4,409
  • 26
  • 49

1 Answers1

2

You could do something like this:

nnoremap <expr> <c-f> '/' . nr2char(getchar()) . '<cr>'

See :h getchar and :h nr2char for more info.

Karl Yngve Lervåg
  • 9,494
  • 25
  • 35
  • I can't seem to get this to work. I tried typing it in as an ex command (i.e. prefixing it with : starting from normal mode) but when I then tried <c-/> nothing happened. – Wildcard Mar 19 '16 at 08:39
  • Strange. Could you try to use f instead of <c-/>? – Karl Yngve Lervåg Mar 19 '16 at 12:35
  • Just tried it; still doesn't do anything. – Wildcard Mar 19 '16 at 12:40
  • Ok, that's very strange. This works as expected at my end. Did you remember the <expr> part? – Karl Yngve Lervåg Mar 19 '16 at 13:26
  • And: Could you try to copy the command and put it in your vimrc? – Karl Yngve Lervåg Mar 19 '16 at 13:27
  • I tried that too. :) I'm on Mac OS X 10.7.4 using Vim 7.3 if that helps. – Wildcard Mar 19 '16 at 13:43
  • I copied and pasted it and it didn't work because of <C-/>. But It did work for me when I substituted <C-/> for another key (e.g. <F2>). – Jair López Mar 19 '16 at 14:02
  • 1
    Same thing for me: it works with F2 but not <c-/>. – statox Mar 19 '16 at 15:43
  • I think there is a problem mapping <c-/>. One could perhaps use <c-v> to write the actual key code generated by <c-/>, but on my keyboard it seems this only returns /, and so it seems <c-/> is undefined for me. I am not sure if this is the case for everyone else. In the meantime, I propose that one instead uses a different key, e.g. <c-f>. – Karl Yngve Lervåg Mar 19 '16 at 19:11
  • Maybe you could add it to your answer, something like “If <C-/> doesn't work, press CTRL-V + CTRL-/ instead, or use another key, e.g., <C-F>” To make it work I actually followed these steps. Note that <C-F> is used for Vim (see :h CTRL-F). This answer provides some guidance regarding how to find a good key sequence. – Jair López Mar 19 '16 at 20:33