Auto-save in VIM as you type

2020-08-26 10:55发布

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?

标签: vim
6条回答
爷的心禁止访问
2楼-- · 2020-08-26 11:35

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.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2020-08-26 11:39

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楼-- · 2020-08-26 11:50

I recommend to save the buffer whenever text is changed:

autocmd TextChanged,TextChangedI <buffer> silent write

I found it here. It works for me.

查看更多
时光不老,我们不散
5楼-- · 2020-08-26 11:50

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.

查看更多
看我几分像从前
6楼-- · 2020-08-26 11:53

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
查看更多
地球回转人心会变
7楼-- · 2020-08-26 11:58

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.

查看更多
登录 后发表回答