Is there a command to exit insert mode and perform :w or :wq in the same command?
I am tired of pressing esc + :w every time I need to write to a file after making edits.
Alternatively, is there an autosave function?
Is there a command to exit insert mode and perform :w or :wq in the same command?
I am tired of pressing esc + :w every time I need to write to a file after making edits.
Alternatively, is there an autosave function?
It is pretty straightforward to create a mapping, e.g.
inoremap <C-S> <cmd>update<CR>
But the point is that you shouldn't stay in Insert mode for too long. That is, always press "esc" immediately after you've finished typing. Use "J", "d", "o", "<<" and other Normal mode keys to reduce the time spent in Insert mode as much as possible. Ideally, you shouldn't need any Insert-mode keys except for typing regular characters and spaces. And if you still don't like pressing :w<CR> then create a Normal-mode mapping instead.
The command is CTRL-OZZ.
In insert mode CTRL-O will change to normal mode for one command and then return to insert mode: :help i_CTRL-O. In this case it doesn't return because the following command is an exit command, but the return behaviour is useful for much more than this particular situation.
ZZ is the command to write the current file if modified and then close the current window: :help ZZ. You can use any of the quit commands (:help write-quit) here as well, of course.
It's an old question, but I find interesting to answer in order to suggest the autosave option. I'd also advise to not spent all your life in text input mode.
:writewill be natural. Not all edits are done in insert mode, so<esc>is not always necessary. If you find yourself spending a lot of time in insert mode (especially in insert mode but not actually inserting text, such as moving around, or deleting things), it may be worthwhile to try spending more time in normal mode. https://twitter.com/nelstrom/status/1354367205010530314?s=20 – D. Ben Knoble Jan 09 '22 at 15:22