-1

So you're in Normal mode, then you press f followed by a character, then vim takes you to the next occurrence of that character. When you land on that character you are in Normal mode.

My question is how can we make it so that when you land it automatically switches you to Insert mode ? at the character landed itself, i.e with i.

1
  • The problem with such an approach is that you make it harder to use f and t as part of other Normal mode sequences and commands. Also, for example, commands such as dt) or ct) to delete or change until the next ). All that just to save a single i or a key press? Not to mention by bundling these two together you also lose the ability to pick between i and a...
    – filbranden
    Nov 22 '20 at 20:05
2

An idea:

nnoremap <expr> f FindInsert('f', 'i')

function FindInsert(find, insert) abort
  const l:char = getchar()->nr2char()
  return printf('%s%s%s', a:find, l:char, a:insert)
endfunction

It would then be easy to add F/t/T, or to use a instead of i, or what have you.

But be aware that when you press ; or ,, you won't get this behavior.

Also, I haven't handled counts.

2
  • Actually another one of my mappings threw me off. I infact get this: Error detected while processing function FindInsert: line 1: E15: Invalid expression: >nr2char()
    – Akharrou
    Nov 22 '20 at 19:59
  • 2
    Functional -> syntax requires a fairly recent Vim version... The equivalent statement without using it is let l:char = nr2char(getchar()) (const is also a recent addition.)
    – filbranden
    Nov 22 '20 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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