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?

9 Answers 9


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.

  • 1
    How can you notice if it is working?
    – nilon
    Jun 8, 2018 at 13:53
  • 1
    @nilon It should print to the status bar that it's saved, just as if you have pressed :w yourself. Aug 16, 2018 at 19:33
  • how much is 'updatetime' milliseconds? , and how I can edit that? Jul 9, 2021 at 11:23
  • @Mahdimehrabi updatetime is an option. :help 'updatetime' to learn more about it
    – jamessan
    Jul 26, 2021 at 10:38

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

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

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, 2016 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, 2016 at 17:53
  • No, currently, it does not support new/unsaved files. If you want that, go ahead and create a whishlish issue. Oct 14, 2016 at 18:10

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 '
    let s:keeppatterns = ''

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"

    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

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

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).


Try to saves the buffer whenever text is changed:

autocmd TextChanged,TextChangedI <buffer> silent write

This works for me.



Put this on your vimrc:

" => vim-autosave 
" Save on lost focus/exit 
autocmd FocusLost,VimLeavePre * silent! w

" Also, save once per minute if there are changes
let g:autosave_seconds = 60 
au BufRead,BufNewFile * let b:start_time=localtime()
au CursorHold * silent! call UpdateFile()
function! UpdateFile()
  if ((localtime() - b:start_time) >= g:autosave_seconds)
    let b:start_time=localtime()
au BufWritePre * let b:start_time=localtime()


  • 2
    Welcome to Vi and Vim! This answer would be stronger with some explanation
    – D. Ben Knoble
    Nov 6, 2020 at 16:23
  • This code will save your buffers on lost focus/exit. It also, save once per minute, if there are changes. You can configure the autosave interval by editing the constant 'autosave_seconds'. Nov 6, 2020 at 16:44
  • Use the edit button to add more information
    – D. Ben Knoble
    Nov 6, 2020 at 16:45

An indirect way to achieve autobackup is to enable set undofile. Every change of an opened file is saved there, multiple undo levels, even after a file is closed and vim exited. I consider it very practical, better than autosave.

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.