I think post-command-hook
is what you wish to use.
It is run every time a command has been run. And a command is basically any interactive action, such as clicking a mouse button, typing a letter (which runs self-insert-command
, or running a command using M-x
.
You may wish to make sure your hook is not run while you are typing in the minibuffer. I think wrapping the body in (unless (window-minibuffer-p) …)
will care of that for you.
And possibly, you want your hook only to be active when you are editing certain files. In which case, you might consider making post-command-hook
buffer local: Using
(add-hook 'post-command-hook #'your-hook nil :local)
will take care of that for you. (Edited to add this last point.)