2

I've setup my vim editor (I use MacVim) to save files automatically when the focus is lost:

autocmd FocusLost * silent! wall

I also automatically strip trailing whitespace from python files using this auto command:

autocmd BufWritePre *.py :%s/\s\+$//e

This auto command works perfectly when I save the file manually (either by typing :w or by pressing ⌘s) but it is not executed (i.e. the whitespace is not stripped) when I switch to another application and the buffer is automatically written.

How can I modify these auto commands to make them work together?

GaretJax
  • 7,462
  • 1
  • 38
  • 47

2 Answers2

5

You need to change your FocusLost autocommand to:

autocmd FocusLost * nested silent! wall

See :h autocmd-nested for details.

Andy Stewart
  • 5,013
  • 2
  • 28
  • 38
1

I can't test this in a graphic Vim, but you can try some options:

  • Join some events in the same autocommand autocmd BufWritePre,FocusLost *.py ...
  • Execute an autocommand from an event, something like:
    autocmd BufWritePre *.py :execute "%s/\s\+$//e" | doautocmd FocusLost %
Birei
  • 35,723
  • 2
  • 77
  • 82
  • Thanks... could have thought of it myself! ;-) I used the "multiple events joined" method. – GaretJax Dec 20 '11 at 13:41
  • Have you any thoughts why it does not work straight with my original setup? – GaretJax Dec 20 '11 at 13:42
  • @GaretJax: Sorry, I can't help with that question. Seems like autocommands cannot be chained, but no idea about the reason. – Birei Dec 20 '11 at 17:48