1

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!

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
dragonxlwang
  • 462
  • 5
  • 13
  • also noticed that if I use "autocmd BufWritePre * Autoformat" Both commands works flawlessly (however it will format check every file which is not what I desire). So it appears that the problem is resulted from using if ... endif test there. – dragonxlwang Mar 28 '16 at 06:47
  • those are two separate autocmds – dragonxlwang Mar 28 '16 at 18:04
  • Small sidenote: vim-autoformat also ships a RemoveTrailingSpaces command. – chtenb Aug 06 '18 at 12:18

0 Answers0