1

In Vim there are find commands ('f', 't', 'F', 'T'), those commands can be repeated by using either ';' or ','. I want to be able to repeat those commands by using 'n' and 'N'. Basically as if I used word search ('/').

For example when I type 'fa', I want vim to act like I executed the command '/a'.

How can I make this happen?

DasOhmoff San
  • 865
  • 6
  • 19
  • 1
    But why not actually use `/a`? You can, of course, remap `n` and `N` to mean `;` and `,`, or `f` to mean `/`, but _why_? – Michail Apr 26 '19 at 19:13
  • 1
    Using f is way more convinient for me. '/a' are 4 keystrokes on my keyboard layout (I live in europe), while 'fa' are only 2, and I also do not have to use the shift key for that (for my keyboard layout) and therefore my hands do not have to move. I would remap my keys but 'n' and 'N' does not work when you use the find command, they only work when you actually search via '/'. – DasOhmoff San Apr 26 '19 at 19:19
  • 1
    Ouch. You need `nnoremap` for this. There's some info on it [here](https://stackoverflow.com/questions/3776117/what-is-the-difference-between-the-remap-noremap-nnoremap-and-vnoremap-mapping) (and also differences with other mapping commands); it might also be worth looking into `keymap` and associated options (saves me a lot of pain while typing Russian text), if you wish to use a different keyboard layout for normal and insert modes. – Michail Apr 26 '19 at 19:29

1 Answers1

3

You can do it by adding the following in your vim configuration:

function! MyFind(c)
       execute "let @/ = '" . a:c . "'"
       return 'n'
endfunc

function! MyBackfind(c)
       execute "let @/ = '" . a:c . "'"
       return 'N'
endfunc

nnoremap <expr> f MyFind(nr2char(getchar()))
nnoremap <expr> F MyBackfind(nr2char(getchar()))

Caveat:

  • Does not work for t/T, could be implemented though, I'll leave this task to you
  • with F, it goes to the previous occurrence of the letter, but pressing n will still go forward, it will do a / search, not a ? search (both use the same register and I haven't found a way to create a ? search, same, I leave that as as exercise for you :-) )

For more info, read :help <expr> mostly.

padawin
  • 4,230
  • 15
  • 19