In most text editors, the standard shortcut for saving the current document is Ctrl+S (or Cmd+S on macOS).
Vim doesn't have this feature by default but this can be mapped to make things easier. Adding the following lines in .vimrc
file will do the job.
nnoremap <c-s> :w<CR>
inoremap <c-s> <c-o>:w<CR>
The nnoremap
command maps Ctrl+s to :w
(write current contents to file) command whereas the inoremap
command maps the Ctrl+S to :w
command and returns back to the insert mode (<c-o>
goes into normal mode for one command and returns to insert mode afterwards, without altering cursor position which other solutions like <esc>:w<cr>a
cannot ensure).
Similarly,
" This is commented, as Ctrl+Z is used in terminal emulators to suspend the ongoing program/process.
" nnoremap <c-z> :u<CR>
" Thus, Ctrl+Z can be used in Insert mode
inoremap <c-z> <c-o>:u<CR>
" Enable Ctrl+C for copying selected text in Visual mode
vnoremap <c-c> <c-o>:y<CR>
PS: However it must be noted that Ctrl+S may not work as expected while using ssh (or PuTTY). The solution to this is not within the scope of this document, but can be found Here.