Search code examples
vimautocmd

Vim: Saving on FocusLost and executing auto command


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?


Solution

  • You need to change your FocusLost autocommand to:

    autocmd FocusLost * nested silent! wall
    

    See :h autocmd-nested for details.