Auto-save in VIM as you type

随声附和 提交于 2020-04-12 19:52:09

问题


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


来源:https://stackoverflow.com/questions/17365324/auto-save-in-vim-as-you-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!