7

I want to test in a script if this version of vim supports the "CmdlineEnter" event.

This is a relatively new addition in vim and neovim doesn't support it in it's current 0.2.2 version (although there is a merged PR that adds that event)

How can I test if this event is supported in the current version of vim?

  has(CmdlineEnter)

raises an error.

11

There are at least three ways to tell if a particular autocmd is supported:

  1. Use exists('##CmdlineEnter'). This should be exactly what you need.

  2. Check for the patch it was introduced in. For example, CmdlineEnter was added in patch 8.0.1206. I determined this by looking at recent vim commits and git blame on the help files. Thus you can use has('patch-8.0.1206'). Although Neovim does not follow vim development exactly, when features are added from "upstream," the vim patch numbers are included too.

  3. Use a try|catch|endtry block around autocmd. E.g.,

try
     silent autocmd CmdlineEnter
catch /^Vim\%((\a\+)\)\=:E216/
     " autocmd is not supported
endtry

When installing an autocmd handler you could simply use silent! autocmd CmdlineEnter * ... to suppress all errors, if your code doesn't actually need to know whether the command is supported.

Alternatively, you can check if an autocmd is subscribed to using exists('#CmdlineEnter'). This will return 0 if the autocmd exists but has no listeners. Sometimes this is all the info you need.

5
  • 4
    You can use exists('##CmdlineEnter') – Christian Brabandt Apr 9 '18 at 18:37
  • Ahh, yeah... that's the correct answer – Mass Apr 9 '18 at 19:05
  • \@Mass, @Christian Brabandt Thank you both. Exactly what I needed – voger Apr 9 '18 at 19:11
  • What does ## mean? – Jonathan Wheeler Dec 14 '20 at 22:53
  • 1
    Hey @JonathanWheeler! In exists('##CmdlineEnter') ## is essentially a signifier/sigil for "autocmd event." Kind of like saying event_exists('CmdlineEnter'). Meaning it's a real supported event in vim. – Mass Dec 14 '20 at 23:29

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.