0

I'm using NeoVim and trying to understand why the need of creating augroup. Then I realized that I don't know the two related commands doautoall, doautocmd. Can anyone help me point out the important part I should care?

To be more specific, under :h doautocmd, I cannot understand this paragraph:

When the [group] argument is not given, Vim executes the autocommands for all groups. When the [group] argument is included, Vim executes only the matching autocommands for that group. Undefined group is an error.

What does it mean "executes the autocommands for all groups" when these groups are autocmds to execute?


As this is an important topic to me, I will also try to answer myself after I would figure it out.

2 Answers 2

2

why the need of creating augroup.

Groups are used, well, for grouping autocmds. This is essential for managing them. If none was specified then the next autocmd will get into the default augroup.

I don't know the two related commands doautoall, doautocmd

Basically, "doautoall" loops over all loaded buffers/windows and calls "doautocmd" repeatedly. There are several nuances, of course. E.g. no other autocmd (BufEnter, BufLeave, WinEnter, WinLeave etc.etc.) will be triggered during the loop; if a buffer has no window (not in the current tab or no window at all) then the special "autocmd" window will be used instead and so on.

What does it mean "executes the autocommands for all groups"

If a group was specified then only autocmds from that group may be executed (the ones that match all other attributes, of course). Otherwise, it works "as normal".

9
  • @job_start Obviously, if we need it to be something other than the current buffer. There can be different events...
    – Matt
    Nov 23, 2022 at 6:19
  • And more strangely, even the fname can just be a simple string pattern, no need to be a "file pattern" like *.c. But this is not documented in the docs(I've read :h doautocmd and :h autocmd, both not mentioned). Do you know why? Nov 23, 2022 at 7:22
  • 1
    @job_start This is an argument passed to autocommand. Go and read about specific events if you want to know what can it be used for. There are dozens of them.
    – Matt
    Nov 23, 2022 at 7:41
  • @job_start There is a difference between argument passed and pattern matched against that argument, isn't there?
    – Matt
    Nov 23, 2022 at 8:18
  • 1
    1. For bufenter (and many others) it should be current buffer name 2. For no event it is a pattern of any kind AFAIK 3. The extra questions should be asked separately, not within comments. The moders are never happy about that.
    – Matt
    Nov 23, 2022 at 13:54
0

Q: What does it mean "executes the autocommands for all groups" when these groups are autocmds to execute?

You can read it as "executes the autocommands from all groups".


For doau, it will run all matched(the optional [fname] argument you provided) autocmds of the specified augroup. I will show you an example.

First, open any file (in my case git.lua) and run the following commands:

:aug FRANK -- this creates an augroup
:au FRANK BufEnter * echo "Hi, *"
:au FRANK BufEnter *.lua echo "Hi, *.lua"
:au FRANK BufEnter git.lua echo "Hi, git.lua"

Then call :h doau, this will move your cursor to the help-docs. Now call :doau FRANK BufEnter, you will see:

hi *

then move your cursor to your "any file" (git.lua in my case) and run :doau FRANK BufEnter again. You will see (something similar to):

hi *
hi *.lua
hi git.lua

This proves that this line of the docs is correct (see :h doau):

Apply the autocommands matching [fname] (default:
            current file name) for {event} to the current buffer.

Finally, :doautoa will do :doau many times.

0

Your Answer

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

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