Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create vim function that reads a single key

How can I make a vim function wait for a single key press?

Example:

  • create a function NextOccur that is mapped to execute when key f is pressed
  • when f is pressed nothing should help right away
  • after the next key (let's say x) is pressed, the cursor should move to the next ocurrence of x

PS: Yes, I know that's exactly what f does by default (although its only linewise), but I was wondering if WE could implement functionality like this, ourselves

What if I want to wait for the next two keypresses, is that also possible?

like image 617
VimQuestion Avatar asked Jun 26 '26 21:06

VimQuestion


1 Answers

You can use the built-in getchar() function. It waits for a single keypress and then returns it. For an ordinary 8-bit character, you have to use nr2char(getchar()) to convert it to a String.

It seems you want to extend the f / t commands to cover multiple lines, too. Be aware that there are already plugins that do that, for example ft_improved.

like image 188
Ingo Karkat Avatar answered Jun 30 '26 03:06

Ingo Karkat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!