Vim multiple autocmd on BufWritePre

1k Views Asked by At

I am using both Chiel92/vim-autoformat and ntpeters/vim-better-whitespace. The first is for autoformat code and the second is for remove extra whitespace. Both of them I hope to use autocmd to call them on save files. It seems that I am not using autocmd correctly. I hope someone can help me out as I have little knowledge about vimL.

I used to have the following to enable autoformat on save:

Plugin 'Chiel92/vim-autoformat'
let auto_format_type_list = ['c', 'cpp', 'py']
autocmd BufWritePre * if index(auto_format_type_list, &ft) >= 0 | Autoformat | endif

and I also use 'ntpeters/vim-better-whitespace', which strip excessive whitespace on save as well.

Plugin 'ntpeters/vim-better-whitespace'
" turn on by default for all filetypes
autocmd BufWritePre * StripWhitespace

The problem is for each of them alone they works perfectly. But when put them together in .vimrc, at least one of them won't work depends one who shows up first in the script.

here's what I have after dumping the :au BufWritePre

:au bufwritepre
--- Auto-Commands ---
BufWrite
    *         if index(auto_format_type_list, &ft) >= 0 | Autoformat | endif
              StripWhitespace

Update ...

After playing around for a while I found that by changing the way to autoformat on save:

 autocmd BufWritePre * call Determine_if_auto_format()
 function! Determine_if_auto_format()
   let auto_format_type_list = ['c', 'cpp', 'py']
   if index(auto_format_type_list, &ft) >= 0
     Autoformat
   endif
 endfunction

Both of them can works with each other.

Can someone please help me out understanding what's going on here? Thanks!

0

There are 0 best solutions below