Search code examples
vimvim-plugin

Temporarily disable BufWrite script in vim


I have vim setup to run Autoformat when I write to files but periodically forget to add an extension to my blacklist before making edits to it. Now I can't save the edits since the autoformating messes up the indentation. Is there a way of saving without running the BufWrite scripts?

The line in my vimrc is:

au BufWrite * if index(blacklist, &ft) < 0 | :Autoformat


Solution

  • There are three options:

    :noa[utocmd] w[rite]
    

    will perform a save without triggering any autocmds. As long as you don't have any other customizations / plugins using autocmds, that would be fine.

    :set eventignore=BufWrite | write | set eventignore=
    

    will temporarily turn off just the BufWrite event.

    Alternatively, you could also add a conditional around your autocmd:

    au BufWrite * if ! exists('g:no_autoformat') && index(blacklist, &ft) < 0 | :Autoformat
    

    That would enable you to selectively disable just that particular autocmd via :let g:no_autoformat = 1.

    PS: Your :autocmd is missing the closing | endif.