Auto-save in VIM as you type

2020-08-26 11:46发布

问题:

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?

回答1:

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.



回答2:

I recommend to save the buffer whenever text is changed:

autocmd TextChanged,TextChangedI <buffer> silent write

I found it here. It works for me.



回答3:

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.



回答4:

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


回答5:

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.



回答6:

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.



标签: vim