I would like to save files without exiting the INSERT mode. Is it possible? How?
1 Answer
Yes, just use the <C-O>
keystroke to temporarily leave insert mode for a single command. You can then run :w<CR>
to write the file and you'll be back to insert mode.
You can use that to create a mapping in insert mode and assign that to a key combination such as <F3>
or <C-W>
, using:
inoremap <F3> <C-O>:w<CR>
Avoid a keystroke such as <C-S>
("S" for "Save") since that specific keystroke can be a problem in many terminals or sometimes through remote connections... <C-S>
is the code for XOFF, which historically has been used to pause a terminal and it's still a valid terminal command in most modern terminals.
If you use a key such as <F3>
, you might want to also map it in normal mode (without the <C-O>
in that case) and visual mode, etc. so that it works regardless of which mode you're currently on.
-
1To disable Ctrl-S hanging the terminal see unix.stackexchange.com/a/72092/2198 . I use Ctrl-S for save as it is great for muscle memory across applications. But if you regularly use new environments that don't share a home directory you'll have to do this fix in each one. Jan 10, 2020 at 16:40