I've tried the following in my .vimrc
:
:au FocusLost * silent! wa
autocmd BufLeave,FocusLost silent! wall
And also tried:
How can I make Vim autosave files when it loses focus?
but can't get it to work, when I do a Ctrl+Z or switch to another tab in Terminal (mac) it still doesn't auto save.
BufLeave
is triggered when you go to another buffer. Neither <C-z>
nor switching to another Terminal.app tab will trigger this because you are using CLI Vim which doesn't care at all about the terminal emulator's GUI or environment and… you are not leaving your buffer.
The same is true for FocusLost
(more or less, the doc says that it's GUI only but can work in some terminals without telling which one).
So, these setting will probably work in MacVim but definetly not in CLI Vim.
Actually, since Vim is not aware of your terminal emulator's tabs or about it being put in the background, I doubt you can achieve your goal in CLI Vim.
I happen to have autocmd FocusLost * :wa
in my ~/.vimrc
but I've put it in an if has("gui_running")
conditional and also inoremap <Esc> <Esc>:w<CR>
to save on ESC. Hope it helps.
On OS X and Vim CLI, I use this plugin http://www.vim.org/scripts/script.php?script_id=4521
AutoSave - automatically save changes to disk without having to use
:w
(or any binding to it) every time a buffer has been modified. AutoSave is disabled by default, run:AutoSaveToggle
to enable/disable AutoSave. If you want plugin to be always enabled it can be done withg:auto_save
option (place'let g:auto_save = 1'
in your.vimrc
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With