0

If I put this

augroup AU_NAME
  autocmd!
  autocmd CursorMoved *.cpp,*.hs echo localtime()
augroup END

autocmd! AU_NAME CursorMoved *.cpp

in my vimrc, then open a file and enter :au AU_NAME, I see

--- Autocommands ---
AU_NAME  CursorMoved
    *.hs      echo localtime()

i.e. the autocmd! deletion action was only limited to *.cpp files, and it did counteract part of what the autocmd had targeted.

However, if the autocmd had been autocmd CursorMoved * echo localtime(), then the autocmd! for *.cpp has no effect.

I think the reason is clear: the * pattern in autocmd Event * ... does not expand to a comma separated (infinite) list of patterns from which autocmd! Group Event pattern can subtract the pattern.

However, is there a way to accomplish the task?


To avoid the XY problem, I'll give a hint of what my plan is.

With this plugin I've created, and which I'm still refining, I create a popup on CursorHold showing the whole line soft-wrapped on top of itself and some lines below (a mapping is provided to dismiss the popup). The user can pass, as an option, a string, say '*.cpp,*.html' which I use as the pattern for the autocmd CursorHold. The string defaults to '*' to target all files.

However, I also want to give users a way to request that the popup is shown as soon as the cursor moves, i.e. on CursorMoved (and still be given the same mapping to dismiss it). I would let the user pass another string to use as the pattern for this autocmd CursorMoved.

Now it's clear that for a given file type I don't want both CursorHold and CursorMoved to create a popup. Indeed, if the user gives the options this way,

let g:myplugin_opts = #{ onHold: '*', onMove: '*.cpp' }

to mean that they want to show the popup on CursorHold for every filetype except for C++ file, for which they want to show popup on CursorMoved, I can't just use those two strings for autocmd CursorHold and autocmd CursorMoved respectively, because in C++ files both autocmds would create a popup (and since I'm providing one nnoremap to close the popup by ID, it would close only one of the two popups).

1 Answer 1

1

"Comma" here is simply a shortcut for two autocmds invoking same list of commands: one for *.cpp and another for *.hs patterns.

Therefore, you can't. So delete "the star" and re-create autocmd anew. Or always trap "the star" and implement all pattern checking yourself.

4
  • implement all pattern checking yourself cannot be done, as I can't list all (infinite) possible extensions.
    – Enlico
    Jan 14, 2023 at 16:37
  • @Enlico Kidding me? Just check that extension is NOT equal to "cpp".
    – Matt
    Jan 14, 2023 at 16:39
  • How d I write an autocmd Event pattern-matching-everything-but-cpp cmd?
    – Enlico
    Jan 14, 2023 at 21:20
  • @Enlico By script, not by autocmd pattern, obviously.
    – Matt
    Jan 15, 2023 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.