History Report a problem
Article Edit this page Discussion

Auto save files when focus is lost

From Vim Tips Wiki

Jump to: navigation, search

Tip 1160 Previous TipNext Tip

Created: March 2, 2006 Complexity: basic Author: Anon Minimum version: 6.0 Karma: 42/17 Imported from: Tip#1160


This tip tells you how to save files when your window loses focus.

It is useful to save time, for instance, when in one gvim window you are editing some source files and in other windows you are compiling/debugging the same source. Especially useful with window AutoRaise feature.

[edit] Howto

Several ways to do this have been suggested:

[edit] Always autosave everything

Put the following into your .vimrc:

:au FocusLost * :wa

[edit] Selectively save a specific file

Enable it with

:call FocusLost_SaveFiles()
" Put this in vimrc, add custom commands in the function.
function! FocusLost_SaveFiles()
  exe ":au FocusLost" expand("%") ":wa"
endfunction

then whenever the mouse leaves the gvim window, the edited file will be saved to disk.

[edit] Save on buffer switch

This is not exactly "when focus is lost", but maybe you were looking for this:

:set autowrite

or

:set autowriteall

This will save the file when you switch buffers, make it etc. See the appropriate :help pages for the exact details.

[edit] Comments

 TO DO 

  • Have we got a tip on recovery? If so, add a "see also" to that tip.
  • If not, add a note here like the comment below.

Thank you for the tip :au FocusLost * :wa
How can we extend this function to save untitled buffers?
We need to give the untitled buffers a filename, perhaps the date combined with the first 5 words of non-blank space text in the document.

When your computer crashes, it is the buffers that you have not yet saved, not the ones you have already saved, that you are most concerned with losing. So it is necessary to have the script choose the best possible filename and automatically save these buffers. Any guidance on how to implement this function would be appreciated. 217.83.26.249 12:09, 28 June 2008 (UTC)

By default, Vim keeps a swapfile for each buffer. You don't have to do anything to save your work. It is saved frequently in the swapfile. You do need to remain calm when recovering from the crash, but so long as you read the messages and :help recovery (which directs you to the introduction in usr_11.txt), you should not lose anything significant if the computer crashes.
Frankly I don't understand the point of this tip. I hate anything that overwrites my file automatically (maybe I was just experimenting with an idea, and was planning to undo it). At any rate, it really isn't hard to save a file when you want. I think this tip is trying to make sure the file is saved when you switch to a compiler to compile the file (so you want to make sure the latest version is on disk).
BTW also see :help 'autowriteall'. --JohnBeckett 12:49, 28 June 2008 (UTC)

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Pushing Daisies
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Astronomy Wiki
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions


Vote for featured Wikia!

Send this article to a friend
"Auto save files when focus is lost"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation