Skip to main content
6 of 7
added 10 characters in body
saginaw
  • 6.9k
  • 2
  • 27
  • 47

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=vim 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.

saginaw
  • 6.9k
  • 2
  • 27
  • 47