You must use augroup with autocmd in vim | Here's how

Waylon Walker - Dec 8 '21 - - Dev Community

If you are running vim autocmd's without a group, you're killing your performance. Granted your probably not sourcing your vimscript files with autocmd's too often, but every time you source that vimscript you are adding another command that needs to run redundantly.

This is what I had

Not silky smooth

For WAAY too long I have had something like this in my vimrc or init.vim. It formats my python for me on every save, works great except if I source my dotfiles more than once I start adding how many times black runs.

autocmd bufwritepre *.py execute 'Black'
Enter fullscreen mode Exit fullscreen mode

Why is a bare autocmd bad

let me demonstrate

Lets create a new file called format.vim and give it the :so %. Works great, it starts telling me that its formatting.

autocmd bufwritepre *.py :echo("formatting with black")
Enter fullscreen mode Exit fullscreen mode

BUT as every time I give it the :so % it formats an extra time on every
single save.

Setting up an augroup

I've been told I need an augroup to prevent duplicates. So I did it, and nothing changes, it still ran as many times as it was sources on every save.

augroup black
    autocmd bufwritepre *.py :echo("formatting with black")
augroup end
Enter fullscreen mode Exit fullscreen mode

Clearing out the augroup

What you need to do is clear out all commands in the augroup with autocmd! right at the beginning of the group.

augroup black
    autocmd!
    autocmd bufwritepre *.py :echo("formatting with black")
augroup end
Enter fullscreen mode Exit fullscreen mode

My Final silky smooth setup

Now this is what I have in my dotfiles for a silky smooth setup that does not run automds like crazy as I am making changes to my dotfiles.

augroup waylonwalker
    autocmd!
    autocmd bufwritepre *.py execute 'PyPreSave'
    autocmd bufwritepost *.py execute 'PyPostSave'
    autocmd bufwritepost .tmux.conf execute ':!tmux source-file %' autocmd bufwritepost .tmux.local.conf execute ':!tmux source-file %'
    autocmd bufwritepost *.vim execute ':source %'
augroup end
Enter fullscreen mode Exit fullscreen mode

Related Links

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .