I was wondering is it possible to create a custom shortcut in vim or vi to save open file?
For example, CtrlS instead of typing each time :w?
Thanks!
Vim comes with a file providing some mappings intended to match Windows files. This is usually in $VIMRUNTIME/mswin.vim (run :echo $VIMRUNTIME to see what it is for you). You can just do:
:source $VIMRUNTIME/mswin.vim
And get a bunch of shortcuts, including CtrlS. See :h mswin.vim for more.
Note that your terminal emulator might use CtrlS for other purposes, but that's outside Vim.
To just create a mapping to test, do:
nnoremap <c-s> :w<cr>
/usr/share/vim/vim74. And when I list files from there I don't have mswin.vim. I am on Deb8, vim version is 2:7.4.488-7
To expound on @muru's caveat about ctrl+s being trapped by the terminal:
By default, a number of terminal-clients (including Cygwin) bind ctrl+s to the freeze output signal, which affects a number of commands such as scp, and incidentally prevents the keypress from being assigned to other things, since the console grabs it before it reaches vim or other applications.
So, for those who want to use ctrl+s for purposes other than freezing output, try adding the following to your ".bashrc" file:
# free up ctrl+s and ctrl+q:
stty stop ''
stty start ''
stty -ixon
stty -ixoff