I want to do the following:
autocmd changeFromItoN * if &readonly == 0 && filereadable(bufname('%'))
\ | silent update | endif
That is: I want to implement the auto-save, so when I exit the insert mode and back to normal mode it will run :w
automately. So what is the real command for changeItoN
?
Note: This sounds like a little problem but actually there is a similar problem on SO that the accepted answer on that thread won't work with an important plugin which is used by a lot of people --- The CoC.nvim, and so I want to accomplish this feature myself :)
Conclusion: The command should be:
autocmd InsertLeave,TextChanged * if &readonly == 0 && filereadable(bufname('%'))
\ | silent update | endif
Notice that the TextChanged
is also needed so autosave will also work with daily-commands, e.g. after you delete a line by dd
.
InsertLeave
.:h autocmd-events
lists the available events it might be useful next time you have a similar question. Also rather than using an autocommand you could go with a mapping like this:inoremap <silent> jk <Esc>:update<CR>
You'd usejk
to switch from insert mode to normal mode and it would save each time you switch mode. I've been using this for years and I'm perfectly happy with it.