Map Ctrl-S to save current or new files
Talk0this wiki
created July 30, 2002 · complexity basic · author Thomas R. Kimpton · version 5.7
I wanted to have a single key stroke that would save existing files, or call the file browser.
Here's a key map for Ctrl-S to accomplish that (place in vimrc file):
if has("gui_running")
" If the current buffer has never been saved, it will have no name,
" call the file browser to save it, otherwise just save it.
:map <silent> <C-S> :if expand("%") == ""<CR>:browse confirm w<CR>:else<CR>:confirm w<CR>:endif<CR>
endif
Comments
Recommend an imap for the same functionality so one can hit ctrl-s without having to make sure one isn't in insert mode:
:imap <c-s> <Esc><c-s>
If one prefers to return to insert mode after the save, the following mapping may be used instead:
:imap <c-s> <c-o><c-s>
Has the nice advantage of allowing one to save in the middle of typing. (I actually have two different keys mapped, one for each of the two above behaviours.)
I use:
imap <c-s> <c-o><c-s><CR>
So that the confirmation doesn't hold you up.
mswin.vim (included with GVim 6.2 for windows) defines:
" Use CTRL-S for saving, also in Insert mode noremap <C-S> :update<CR> vnoremap <C-S> <C-C>:update<CR> inoremap <C-S> <C-O>:update<CR>
To enable mswin.vim in vim 7+, just add the following to your .vimrc:
source $VIMRUNTIME/mswin.vim