1

I need to programmatically input a motion in operation-pending mode, kinda like using :normal!, but without switching modes, so the operator still takes effect.

Context

I'm writing a script that changes the behaviour of a key depending on how many times it has been pressed in a row during a given time. Think of vim-hardtime, but instead of blocking the key, I want it to do something else, e.g., if I press l three consecutive times its behavior should switch to fs.

vim-hardtime solves this by creating non-recursive <expr> mappings on the restricted keys. When the key is allowed, the string representing it is returned, so its default command is executed. When the key is blocked, an empty string is returned, and nothing happens. I tried this technique, but it doesn't suffice, because <expr>-mappings can't do the following:

  • Changing the buffer text.
  • Editing another buffer.
  • Use the :normal command.

Note

Tho programmatically feed a motion is the only solution I can think of, so using <expr> is not necessary, but maybe there's a workaround <expr>-mappings limitations that I'm not aware of

1
  • Possibly feedkeys()? Read its doc carefully though. It’s tricky to get right
    – D. Ben Knoble
    Commented Jul 31, 2023 at 15:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.