Vim logo vim online Vim Book Ad

basic Tip #294: Use Ctrl-S to save current or new files.

 tip karma   Rating 15/10, Viewed by 939 

created:   July 30, 2002 12:37      complexity:   basic
author:   Thomas R. Kimpton      as of Vim:   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


Tom Kimpton

 rate this tip  Life Changing Helpful Unfulfilling 

<<remember where you had ended reading help | Line/word/file/whatever completion >>

Additional Notes

[email protected], July 30, 2002 20:14
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.)
atkinssc AT engr orst edu, August 5, 2002 15:09
use:
imap <c-s> <c-o><c-s><cr>

So that the confirmation doesn't hold you up.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo