1

Say I want to copy what f does. f takes an "argument" keystroke and moves the cursor there. How do I create such a mapping?

I want to have a functionality that would require me to have a character as an argument. I'm not looking for the exact, or even close, functionality of f

  • 1
    Use y/ + the desired keystroke? :) (so create a map for y/) - it has the advantage that it works on multiple lines, can search for more than one char, but you have to hit Enter. – VanLaser Sep 4 '16 at 11:40
  • @VanLaser Sorry, I wasn't clear. I'm not looking for that exact functionality. – 0fnt Sep 4 '16 at 12:27
  • Ok, you probably want to use :h getchar() in a function. – VanLaser Sep 4 '16 at 12:28
  • Also, yf + <a char> "to copy what f does" (so - map yf) – VanLaser Sep 4 '16 at 13:31
  • What problem are you actually trying to solve? f doesn't take arguments, it's a motion (see :h left-right-motions) that can be used with operators (see :h operator; in fact read the entire :h motion.txt, about everything there is very useful). So, what are you trying to do? – Sato Katsura Sep 4 '16 at 14:12
2

You're looking for getchar(). Since that usually returns a number, convert the result back to a String:

let char = nr2char(getchar())

This is not a full equivalent of what the built-in f does; for example, it doesn't handle digraphs (i.e. <C-K>xy combinations).

If you use this in a mapping, ensure that all keys from the mapping have been consumed, or wrap the getchar() around inputsave() / inputrestore().

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.