Reputation: 15976
As the question title mentions. I'm looking to automatically get the file saved as I type in VIM (insert mode).
Is this possible? How to achieve it?
Upvotes: 23
Views: 24740
Reputation: 630
Using a function with logic to handle read only buffers and empty buffers in tandem with a autocommand works pretty well:
" auto save file when it is modified
augroup auto_save
autocmd!
" call save function
autocmd BufModifiedSet * call AutoSave()
augroup end
" save function that is called when buffer is modified
function AutoSave()
if (bufname() != "" && &buftype == "" && &filetype != "" && &readonly == 0)
silent write
" prevent empty, readonly, etc... buffers from being saved
else
endif
endfunction
The autogroup auto_save
contains an autocommand that calls AutoSave()
. It is executed whenever the current buffer is modified(see :help BufModifiedSet
). The AutoSave()
function writes the buffer only if it is writeable, it is not blank, and it(the buffer) has a file type.
Upvotes: 0
Reputation: 34565
I recommend to save the buffer whenever text is changed:
autocmd TextChanged,TextChangedI <buffer> silent write
I found it here. It works for me.
Note (thanks to @Kevin): Unfortunately, it will result in errors if you open vim without opening a file because vim will try to save the text you type but won't have where.
Upvotes: 46
Reputation: 41
Don't know if someone mentioned this. Autosave Per File Type
( in this case it is for a Markdown *.md file)
autocmd BufNewFile,BufRead *.md :autocmd TextChanged,TextChangedI <buffer> silent write
This will write the contents of the file the moment they are modified but only for Markdown (*.md) files.
Upvotes: 2
Reputation: 244
907th/vim-auto-save auto saves file. But if your .vimrc depends on write event, then it could have issue.
Recently, I notice https://github.com/chrisbra/vim-autosave, which saves files to a backup dir, which sounds promising if your .vimrc depends on write event.
Upvotes: 0
Reputation: 81
This will handle read-only buffers (like netrw) and undetected filetypes. Using TextChangedI instead of InsertLeave seems to cause a write for every character typed in insert mode, which may or may not be what you want.
augroup autosave
autocmd!
autocmd BufRead * if &filetype == "" | setlocal ft=text | endif
autocmd FileType * autocmd TextChanged,InsertLeave <buffer> if &readonly == 0 | silent write | endif
augroup END
Upvotes: 6
Reputation: 19578
You can use AutoSave plugin to perform that:
https://github.com/907th/vim-auto-save
Please notice that AutoSave is disabled by default, run :AutoSaveToggle
to enable/disable it.
Upvotes: 13
Reputation: 1051
There is no native support for auto-saving in Vim. But you can use vim-auto-save plugin to perform that.
This plugin auto-saves in normal mode only by default, but there is a section in it's README which describes how to configure the plugin to save in insert mode too. Hint: you should configure the plugin to auto-save on CursorHoldI
and/or TextChangedI
Vim events.
Please, refer to the plugin documentation on how to install and use it.
Upvotes: 2