Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autosave in Vim 7 when focus is lost from the window?

Tags:

vim

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.

like image 831
Kamilski81 Avatar asked Jan 31 '12 21:01

Kamilski81


2 Answers

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.

like image 166
romainl Avatar answered Sep 24 '22 11:09

romainl


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 with g:auto_save option (place 'let g:auto_save = 1' in your .vimrc).

like image 44
bakkal Avatar answered Sep 23 '22 11:09

bakkal