28

Some word processors and text editors auto-save the files we're editing from time to time, which is very convenient for surviving from nasty crashes, etc.

Is there a way to make Vim auto-save our files after a specific interval?

29

There isn't a builtin option, although :help 'autosave' (in todo.txt) refers to a description of what such an option could do.

One possible way to implement this is to use the CursorHold autocommand event. This event is triggered when the user hasn't pressed a key for 'updatetime' milliseconds.

autocmd CursorHold * update

The :update command only saves the buffer if it is modified. If you also wanted this to happen while insert mode is active, CursorHoldI could be added to the event list.

autocmd CursorHold,CursorHoldI * update

Another alternative is the 'autowrite' option, which will save the buffer before executing an external command (useful for saving before running :make) or switching to a different buffer.

  • How can you notice if it is working? – nilon Jun 8 '18 at 13:53
  • @nilon It should print to the status bar that it's saved, just as if you have pressed :w yourself. – MaudPieTheRocktorate Aug 16 '18 at 19:33
9

Vim does have an auto-save feature, but it saves it to the "recovery" file.

When you edit a document, e.g. MyFile.txt vim will create the revovery file .MyFile.txt.swp. If your system were to crash, or your session were to die, then you can reconnect, and recover from essentially where you left off, by typing:

vim -r MyFile.txt

This will open the file for editing, and recover the last changes you made to it.

You can then save the recovered version the normal way :w.

Unfortunately, when you exit vim, it will leave the .MyFile.txt.swp around, and you need to remove it manually:

rm .MyFile.txt.swp

See "Recovery" Man page Also Chapter 11

  • When you exit Vim properly (i.e. with :wq or :q! or ZZ or etc.), swap files should automatically be removed. – Doorknob Feb 3 '15 at 22:52
  • 4
    @Doorknob not when you exit after a recovery... ;-) – rolfl Feb 3 '15 at 22:52
7

I made a plugin vim-autosave which uses Vim 8 feature of Timers to periodically save your buffers.

  • Woah, Vim 8 plugins coming out already. Looks great, Christian! – thameera Sep 13 '16 at 18:18
  • Thanks, Christian. This looks promising. I wonder if the plugin supports new/unsaved files. I can't find the answer in the plugin documentation. My computer decided to restart last night and I had several quick notes on new buffers. Vim didn't create or conserve swp files. – JAponte Oct 14 '16 at 17:53
  • No, currently, it does not support new/unsaved files. If you want that, go ahead and create a whishlish issue. – Christian Brabandt Oct 14 '16 at 18:10
2

I make my Vim save everything every time focus is lost.

autocmd FocusLost <your-patterns-here> silent! wall

My patterns are pretty long and I use them in several autocmds but for completeness this is what it looks like:

if exists(':keeppatterns')
    let s:keeppatterns = 'keeppatterns '
else
    let s:keeppatterns = ''
endif

augroup AutoSaveGroup
    " :help file-pattern has some info,
    " but some of it is is buried in the PATTERNS section of usr_40.txt
    let s:autoPattern = "*.{c,h,[ch]pp,[acjt]s,inl,cg,cgfx,fx,py,bat,cmd,jam,vim,yml,yaml,vsprops,erb,rb,html,htm},SCons*,*vimrc"

    autocmd!
    execute "autocmd FocusLost"     s:autoPattern   "silent! wall"
    execute "autocmd BufWritePre"   s:autoPattern   s:keeppatterns . 'call FixFormatting(expand("<afile>"))'

    execute "autocmd FileChangedRO" s:autoPattern "silent !p4 edit %:p"
    execute "autocmd FileChangedRO" s:autoPattern "write!"

    autocmd FocusLost   *.txt   silent! wall
augroup END
1

As others have indicated, vim has a swap file (.swp) that will be created where ever your original file is, but in your .vimrc file, you can set the .swp directory.

You'll need to create the directory with mkdir and then set your .vimrc to that location.

set backup " make backup files
set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
1

The vim-workspace plugin has a fairly customizable auto-save feature that may suit your needs. By default, it enables autosave when in a workspace session (its main feature), but you can set it to always autosave if that's all you're looking for (let g:workspace_autosave_always = 1).

1

Try to saves the buffer whenever text is changed:

autocmd TextChanged,TextChangedI <buffer> silent write

This works for me.

Source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.