1

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?

Prismavoid
  • 83
  • 4
  • Should execute when the user pressed any keys in any special mode or really everytime? – TornaxO7 Jan 26 '21 at 06: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? – Jürgen Krämer 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. – mahbubweb Jan 26 '21 at 17:53
  • You can build a list of keys and remap them (like in this joke answer, not a complete list though), but I don't think there's any autocmd or easy way to do this. As others have mentioned, it would be helpful if you could describe what your goal is, because there may be another way to solve it. – Martin Tournoij Jan 26 '21 at 20:04
  • @MahbubAlam You have a good point. Exactly what I am working on is a plugin using Ueberzug to display images in Vim (more info). The reason I want to detect keypresses is because almost any action, notably folding (which there is not an autocommand for), could affect exactly where the image is placed. In any of these cases, I would want to make plugin check whether any of the images are displayed in a new place, and if so update them accordingly. Currently I am using a timer to update it, but that is not ideal. – Prismavoid Jan 27 '21 at 03:12
  • 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. – Martin Tournoij Jan 27 '21 at 23:00
  • I couldn't find any results for ConcealUpdated using :help or grepping the autocmd documentation. Also, when I tried to run it, I got an error saying that it was not an event. I am using neovim, so is this something that only works in vim? – Prismavoid Jan 27 '21 at 23:36

1 Answers1

1

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.

Mass
  • 14,080
  • 1
  • 22
  • 47
  • 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