4

For each autocmd (or set of related autocommands) in my .vimrc I currently define an augroup to prevent having multiple autocommands when I re-source my .vimrc. I do not need the augroup for anything else (e.g. toggling them on or off via settings).

Can I just remove all the augroup and autocmd! clutter surrounding my actual autocmd and tell Vim to remove all autocommands upon re-sourcing?

Perhaps adding something similar to the following line on top of my .vimrc (to unset all previous autocommands)?

autocmd! * *

(Putting all autocommands in one augroup is no viable option for me, as I lose my structure then)

3

Just adding autocmd! before defining your autocommands, and without any augroup, should be enough.

Here is an excerpt from :h :autocmd :

When your .vimrc file is sourced twice, the autocommands will appear twice. To avoid this, put this command in your .vimrc file, before defining autocommands:

:autocmd! " Remove ALL autocommands for the current group.

I think that if you don't have any group, and you put autocmd! inside your vimrc, vim considers that it's inside a default group, and so will delete all the autocommands for the latter which is all autocommands globally, because of this (:h autocmd-groups) :

When no specific group is selected, Vim uses the default group. The default group does not have a name. You cannot execute the autocommands from the default group separately; you can execute them only by executing autocommands for all groups.

But if you want to be sure, there's a way to check.

Add this code in a file that is sourced by vim, for example your ~/.vimrc (it will create the :RedirInTab command) :

function! RedirInTab(command)
    redir => s:output
    silent! execute a:command
    redir END
    if empty(s:output)
        echoerr "No output"
    else
        tabnew
        setlocal ft=help buftype=nofile noswapfile nobuflisted bufhidden=wipe nomodified
        silent! put=s:output
    endif
endfunction
command! -nargs=+ -complete=command RedirInTab call RedirInTab(<q-args>)

To see all the autocommands installed in your session, you can type :autocmd.

But to navigate more easily in the output with the same commands you have in a normal buffer, and to see the number of lines contained in it, type this :

:RedirInTab autocmd

You'll see all your currently installed autocommands in a normal buffer displayed in a new tab (it works for any other command that gives an output, like :command, :function, :highlight, etc.). Go at the bottom of the buffer to see how many lines there are (hit <C-g> or set number if necessary).

Next source your ~/.vimrc a few times (let's say 10). You can source it manually with :so% and you can repeat this command 10 times by typing 10@:. Now reexecute :RedirInTab autocmd and see how many lines there are this time.

If you don't put autocmd! in your ~/.vimrc, you should probably see a few hundred more lines. If you put autocmd!, the number of lines should be approximately the same.

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.