I want to run a function every time the user presses any key. I have already looked through the autocommand documentation and there does not seem to be one for this. Is there any function I do not know of that will detect any keypress?

  • Should execute when the user pressed any keys in any special mode or really everytime?
    – TornaxO7
    Jan 26 '21 at 6:52
  • It needs to be for any time a key is pressed in any of the modes (I guess the exception would be command mode/ex mode.)
    – Prismavoid
    Jan 26 '21 at 12:27
  • What do you want to achieve with this function? Jan 26 '21 at 13:31
  • 1
    Can you kindly mention the precise problem(s) you are trying to solve? Otherwise it might lead to the XY problem. Jan 26 '21 at 17:53
  • 1
    I think that the best solution is probably a new ConcealUpdated autocmd, which gets triggered when "concealed" text updates the screen: folds open/close, but also concealed text gets displayed/hidden, and maybe some other scenarios. It might be worth raising this issue on the Vim issue tracker; I don't think it's that much work to implement. Jan 27 '21 at 23:00

There is not a direct way in vim to hook all key presses. If you are open to source modifications, one idea would be to edit the source code of the gtk version of vim.

However, there are a number of events which you can hook into; e.g.,

au CursorMoved,CursorMovedI,TextChanged,TextChangedP,CmdlineEnter,CmdlineLeave,CmdlineChanged * call cmd()

You can come up with key presses that don't raise one of these autocmds, but it would be rare in a normal editing session.

  • While these cover most actions, a big part of what I need to detect is folding/unfolding text, which you cannot do with autocommands.
    – Prismavoid
    Jan 27 '21 at 22:44

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.