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.
Several ways to do this have been suggested:
Always autosave everythingEdit
Put the following into your vimrc
:au FocusLost * :wa
Ignore warnings from untitled buffersEdit
The above command will complain if you have untitled buffers open. The command below will cause those warnings to be ignored.
:au FocusLost * silent! wa
This variant will silently ignore errors from the
:wa command, but it does mean any of your untitled buffers, buffers for read-only files, etc. will not be saved just from losing focus, and you will not be notified of this. You will need to remember to save them manually.
Selectively save a specific fileEdit
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.
Save on buffer switchEdit
This is not exactly "when focus is lost", but maybe you were looking for this:
This will save the file when you switch buffers, make it etc. See the appropriate
:help pages for the exact details.
- 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.
- or just the buffer number
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. 18.104.22.168 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)
- after saving unnamed buffers with in e. g. $HOME/tmp/123_2000-01-01T00_00_00Z.txt, :0file! might be useful
If you want to save only the current file (as opposed to writing all changed buffers), be sure to use "up[date]", not "w[rite]", i.e ":au FocusLost * !silent update". Update writes only when the file is actually changed, whereas write writes always, and that might lead to unwelcome side-effects — most notably, encrypted files will get corrupted if focuslost happens while password input is active.