Are there any events similar to
InsertLeave that I could use for visual mode?
Looking through the helpfiles, I did not fine any.
If not, how would I be able to detect if Vim enters or leaves Visual mode?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
September of last year, a new event was added:
commit f1e8876fa2359b572d262772747405d3616db670 (tag: v8.2.3430) Author: =?UTF-8?q?Magnus=20Gro=C3=9F?= <[email protected]> Date: 2021-09-12 patch 8.2.3430: no generic way to trigger an autocommand on mode change Problem: No generic way to trigger an autocommand on mode change. Solution: Add the ModeChanged autocommand event. (Magnus Gross, closes #8856)
ModeChanged After changing the mode. The pattern is matched against `'old_mode:new_mode'`, for example match against `*:c*` to simulate |CmdlineEnter|. The following values of |v:event| are set: old_mode The mode before it changed. new_mode The new mode as also returned by |mode()| called with a non-zero argument.
So you can use the following event pattern:
augroup VisualEvent autocmd! autocmd ModeChanged *:[vV\x16]* :echom 'VisualEnter' autocmd Modechanged [vV\x16]*:* :echom 'VisualLeave' augroup END
[vV\x16] is a regex pattern matching either of v (visual mode), V (line-wise), or ctrl-v (block-wise).
Answer found courtesy of clason.