2

I have many autocommands in my vimrc. Some of them do have the augroup wrapping to make them not duplicate when re-sourcing the vimrc, but many do not.

Rather than try to wrap all of them at this point, I figured that for my vimrc sourcing bind

nnoremap <Leader>L :so $MYVIMRC<CR>:runtime! after/plugin/*.vim<CR>:runtime! after/ftplugin/*.vim<CR>

that i should just have it do something similar to treating the entire vimrc as a global augroup and clear that before sourcing the scripts again. I think that I cannot literally do this, because only autocommand-commands should be inside of an augroup, and nested augroups probably are not a thing...

Can this be done? I'm hoping for a feature that just does this, rather than having to programmatically find all set autocommands to clear them one at a time.

  • 3
    just autocmd! .. but no guarantees it doesn't break something somewhere – Mass Mar 7 '18 at 20:08
  • I imagine you could add a group to your non-augroup'd autocmd's. However is it best to use a self clearing augroup. Use something like :vimgrep /autocmd/ $MYVIMRC to find autocmd in your vimrc file. Use :cnext/:cprev to jump between locations. See :h quickfix and :h :vimgrep. – Peter Rincker Mar 7 '18 at 20:16
3

autocmd! at the top of .vimrc.

From the docs:

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.

If you don't want to remove all autocommands, you can instead use a variable to ensure that Vim includes the autocommands only once:

:if !exists("autocommands_loaded")
:  let autocommands_loaded = 1
:  au ...
:endif

Note: That's from the 7.4 docs. The 8.0 docs omit autocmd! in favor of groups:

:autocmd adds to the list of autocommands regardless of whether they are already present. When your .vimrc file is sourced twice, the autocommands will appear twice. To avoid this, define your autocommands in a group, so that you can easily clear them:

    augroup vimrc
      autocmd!      " Remove all vimrc autocommands
      au BufNewFile,BufRead *.html so <sfile>:h/html.vim
    augroup END

If you don't want to remove all autocommands, you can instead use a variable to ensure that Vim includes the autocommands only once:

    :if !exists("autocommands_loaded")
    :  let autocommands_loaded = 1
    :  au ...
    :endif

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.