For example, what's the difference between calling this command this way:
autocmd BufWritePre * call StripTrailingWhitespace()
vs. this way (with the bang after autocmd
):
autocmd! BufWritePre * call StripTrailingWhitespace()
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityThe command
autocmd! BufWritePre * call StripTrailingWhitespace()
Removes all autocmds for the event BufWritePre
and the file pattern *
from the default autocmd-group and sets a new autocmd for this event and pattern to call StripTrailingWhitespace()
.
Example:
autocmd BufWritePre * echomsg "First"
autocmd BufWritePre * echomsg "Second"
If you then enter :autocmd BufWritePre
you will get the following (plus maybe other autocmds defined for this event):
--- Autocommands ---
BufWrite
* echomsg "First"
echomsg "Second"
Then you execute
autocmd! BufWritePre * echomsg "Third"
followed by :autocmd BufWritePre
and you get:
--- Autocommands ---
BufWrite
* echomsg "Third"
So the autocmds echoing "First" and "Second" were removed and the new autocmd echoing "Third" was defined.
See :help autocmd-remove
.
autocmd!
command, you're fine without augroup
s. Otherwise, shared event autocmd!
s will clear each other. So, wrapping your autocmd
commands inside an augroup
is the safer option here.
vimrc
and wrapped even this single cmd in a group called my_syntax_autocmds
.