Vim: Saving on FocusLost and executing auto command

谁说我不能喝 提交于 2019-12-07 23:59:52

问题


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?


回答1:


You need to change your FocusLost autocommand to:

autocmd FocusLost * nested silent! wall

See :h autocmd-nested for details.




回答2:


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 %


来源:https://stackoverflow.com/questions/8574387/vim-saving-on-focuslost-and-executing-auto-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!