0

As title. I just randomly come up with this question. To do so one would need to iterate over all autocmds in the augroup. Is this possible?

2 Answers 2

4

To do so one would need to iterate over all autocmds in the augroup.

No. Operating on a bunch of autocommands in one go is precisely why autocommand groups exist, as explained under :help autocmd-group:

Autocommands can be put together in a group.  This is useful for removing or
executing a group of autocommands.  For example, all the autocommands for
syntax highlighting are put in the "highlight" group, to be able to execute
":doautoall highlight BufRead" when the GUI starts.

Anyway, back to your question:

How to execute all autocmds of a specific augroup?

You can't do it elegantly and you probably don't want to anyway.

The use case for an autocommand is to execute a specific command when a specific event is triggered with a specific value.

When you enter a window…

  1. a WinEnter event is triggered with the name of the buffer displayed in the window as value,
  2. Vim tests the patterns of every WinEnter autocommand in every group against that value,
  3. if the pattern matches, the command is executed.

Now, you have the possibility of doing the same thing manually with :help :doautocommand:

:doautocmd WinEnter

and you can even pass an arbitrary value:

:doautocmd WinEnter foobar

In both cases, every autocommand that matches against any value (pattern is *) and every autocommand that specifically matches foobar are going to be executed.

That's the main scenario envisioned by the Vim developers for :doautocmd and :doautoall: triggering a specific event in an explicit manner.

The whole autocommand system is designed around the notion of event, not around patterns or commands or groups. Using it correctly is only a matter of thinking in events. All the rest is implementation detail.

You listen to a specific event. You trigger a specific event. And that's it.

Some events, like BufNewfile, can have hundreds if not thousands of autocommands associated, with as many patterns. In that context, doing:

:doautocmd BufNewFile
(goes through a massive number of autocommands)

may not be a great idea. Or you may only want to execute the subset of available autocommands that actually suits your needs. That's where autocommand groups enter the picture. By specifying a group name, you tell Vim to only execute the autocommands associated with the given event that belong to the given group:

:doautocmd MyGroup BufNewFile
(goes through fewer autocommands)

Which is not much more than a specialized case of the scenario above.

Another benefit of grouping autocommands is the example provided at the bottom of :help autocmd-groups.

In short, Vim makes it easy to do the following:

  • execute all autocommands for a given event,
  • execute a subset of all commands for a given event,

but it doesn't provide a way to do the following:

  • execute all autocommands
  • execute all autocommands for a given group

presumably because there are lots of events and therefore lots of potentially available autocommands, which means lots of commands and lots of risk.

In fact, as noted by @Matt in the comments, the following will throw an error:

:doautocmd MyGroup *

So Vim really goes out of its way to actually prevent what you want to do.

That said, there is still this little loophole:

:doautocmd MyGroup BufNewFile,Bufread,WinEnter[and so on…]

but that sounds extremely silly.

5
  • But sometimes it is not that easy for me to find the exact part related to my confusion. For example, with :h doautoa it doesn't say explicitly that it will execute all autocmds from the specified augroup. Instead it says: Like ":doautocmd", but apply the autocommands to each loaded buffer. The current buffer is done last.. In my perspective it is not related to my question here, where I want to execute all autocmds from an augroup. Nov 23, 2022 at 9:08
  • @job_start :help :doautoall starts off with Like ":doautocmd", but[…]. If you don't know what :doautocmd does then you can't understand :doautoall so you are expected to read :help :doautocmd, which is quite explicit about autocommand groups. Your perspective and your strategy don't work. Change them.
    – romainl
    Nov 23, 2022 at 9:46
  • Thanks for your update on it. I usually have trouble reading long documentation. What I learned these years is to express my confusion as long as I have any doubt about any little part. Apologies if any of my comments may be too offensive. Nov 23, 2022 at 13:54
  • 1
    You could add that doau testgroup * results in E217. So such action is intentionally prohibited by Vim.
    – Matt
    Nov 23, 2022 at 14:27
  • Indeed, I did try something similar days before, maybe that's the source of my post here. Thanks for your beautiful answer, it does solve many of my recently asked questions (all in today). (Oops, I just realized that the comment is by Matt) Nov 23, 2022 at 14:51
0

There's the autocmd_get() function for this, which accepts a dict to filter the returned value:

:echo autocmd_get(#{group: 'my-autocmds'})[3]
{'cmd': 'setl et', 'group': 'my-autocmds', 'pattern': 'awk', 'nested': v:false, 'once': v:false, 'event': 'FileType'}

The full output is rather long, so only printing one entry here, which is:

augroup my-autocmds
    " ...

    au Filetype awk         setl et
augroup end

This was added to vim fairly recently, with Vim 8.2.5030 (May 2022). From a quick search, it seems it's not ported to Neovim, but they may have a different API for it.

0

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.