Wikia

Vim Tips Wiki

Changes: Map Ctrl-S to save current or new files

Edit

Back to page

(Comments: A mapping to preserve the current selection after saving)
 
(5 intermediate revisions by 3 users not shown)
Line 18: Line 18:
 
" If the current buffer has never been saved, it will have no name,
 
" If the current buffer has never been saved, it will have no name,
 
" call the file browser to save it, otherwise just save it.
 
" call the file browser to save it, otherwise just save it.
nnoremap <silent> <C-S> :if expand("%") == ""<CR>browse confirm w<CR>else<CR>confirm w<CR>endif<CR>
+
command -nargs=0 -bar Update if &modified
  +
\| if empty(bufname('%'))
  +
\| browse confirm write
  +
\| else
  +
\| confirm write
  +
\| endif
  +
\|endif
  +
nnoremap <silent> <C-S> :<C-u>Update<CR>
 
</pre>
 
</pre>
 
Note that in terminal you must somehow disable interpreting <code><C-s></code> by the terminal itself. You may try to use an alias for this:
 
Note that in terminal you must somehow disable interpreting <code><C-s></code> by the terminal itself. You may try to use an alias for this:
Line 31: Line 31:
 
vim()
 
vim()
 
{
 
{
  +
# osx users, use stty -g
 
local STTYOPTS="$(stty --save)"
 
local STTYOPTS="$(stty --save)"
 
stty stop '' -ixoff
 
stty stop '' -ixoff
Line 37: Line 38:
 
}
 
}
 
</pre>
 
</pre>
  +
  +
Or you can just always run <pre>stty -ixon</pre> in your .bashrc or terminal to disable flow control for that terminal completely (not just for vim).
   
 
==Comments==
 
==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:
 
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>
+
:inoremap <c-s> <Esc>:Update<CR>
   
 
If one prefers to return to insert mode after the save, the following mapping may be used instead:
 
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>
+
:inoremap <c-s> <c-o>:Update<CR>
   
 
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.)
 
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.)
   
  +
Also a mapping in visual mode is useful, 'gv' preserves the previous selection (so you can save without losing your selection):
  +
vmap <C-s> <esc>:w<CR>gv
 
----
 
----
 
I use:
 
I use:
imap <c-s> <c-o><c-s><CR>
+
  +
:inoremap <c-s> <c-o>:Update<CR><CR>
   
 
So that the confirmation doesn't hold you up.
 
So that the confirmation doesn't hold you up.

Latest revision as of 09:59, November 22, 2014

Tip 294 Printable Monobook Previous Next

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 the current buffer has never been saved, it will have no name,
" call the file browser to save it, otherwise just save it.
command -nargs=0 -bar Update if &modified 
                           \|    if empty(bufname('%'))
                           \|        browse confirm write
                           \|    else
                           \|        confirm write
                           \|    endif
                           \|endif
nnoremap <silent> <C-S> :<C-u>Update<CR>

Note that in terminal you must somehow disable interpreting <C-s> by the terminal itself. You may try to use an alias for this:

# zsh
alias vim="stty stop '' -ixoff ; vim"
# `Frozing' tty, so after any command terminal settings will be restored
ttyctl -f

# bash
# No ttyctl, so we need to save and then restore terminal settings
vim()
{
    # osx users, use stty -g
    local STTYOPTS="$(stty --save)"
    stty stop '' -ixoff
    command vim "$@"
    stty "$STTYOPTS"
}
Or you can just always run
stty -ixon
in your .bashrc or terminal to disable flow control for that terminal completely (not just for vim).

CommentsEdit

Recommend an imap for the same functionality so one can hit ctrl-s without having to make sure one isn't in insert mode:

:inoremap <c-s> <Esc>:Update<CR>

If one prefers to return to insert mode after the save, the following mapping may be used instead:

:inoremap <c-s> <c-o>:Update<CR>

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.)

Also a mapping in visual mode is useful, 'gv' preserves the previous selection (so you can save without losing your selection):

vmap <C-s> <esc>:w<CR>gv

I use:

 :inoremap <c-s> <c-o>:Update<CR><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

Around Wikia's network

Random Wiki