I'm trying to understand what does exclamation mark after autocommands mean.

Sometimes it is possible to see something like this:

function! LangSpecificOptions()
  if (&filetype == 'markdown' || &filetype == 'html')
    set wrap
    set nowrap        
autocmd! BufReadPost,BufNewFile * call LangSpecificOptions()

I already found what "!" means when placed after the function:

Bao Niu: I am learning Vimscript and I wonder if there is any difference between writing "function!" vs. "function"? What is that exclamation point for here?

Tim Chase: As detailed at :help E123, if a function already exists with the same name as you want to create, the "!" will tell it to overwrite the previously-existing function. Thus, you might have

function Hello() 
  echo 'Hello' 

and then want to change it, so you'd use

function! Hello() 
  echoerr 'Hello' 

to overwrite it. Because of the behavior, I just use the "!" every time that I define a function. That way, I don't have to think about whether it already exists or not.

Source: http://vim.1045645.n5.nabble.com/why-quot-function-quot-instead-of-just-quot-function-quot-td5723708.html

However, I haven't found similar explanation for autocommands.


As it correctly stated in comments by @mass, "!" after autocommands mean that this autocommand should be removed. Well, then another question:

What's the purpose of removing autocommands?

  • it is documented at :help autocmd-remove, has nothing to do with function! – Mass Feb 2 at 19:13
  • Hmm, thanks. Trying to understand what I read just now in help section. – john c. j. Feb 2 at 19:19
  • Deleting autocommands is used in augroups. :help :augroup – Ralf Feb 2 at 21:08
  • What's the purpose of removing autocommands?: It clears the autocommand group, making sure that no old leftover command is there and you start from a fresh clean state. Note: that recently the behaviour of func! also changed recently. Since 8.1.573, you can skip the ! if simply trying to redefine the same function if it is in the same script. – Christian Brabandt Feb 2 at 22:04
  • @ChristianBrabandt and Ralf - thanks. Well, I read something about autocommand groups (never used them before). And it seems the code where I see this exclamation mark doesn't really need it. It was here: shallowsky.com/blog/linux/editors/vim-settings-by-file.html – john c. j. Feb 2 at 22:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.