95

In vim, how can I map "save" (:w) to ctrl-s.

I am trying "map" the command, but xterm freezes when I press ctrl-s.

If I give ctrl-v,ctrl-s still I see only a ^, not ^S.

3
  • 8
    what's wrong with Vim's usual :w ? I'm guessing ctrl-s freezes your xterm because it freezes all output in terminals. ('stty -ixon' might help)
    – Yoni H
    Aug 10, 2010 at 6:14
  • 22
    Pressing (Escape shift colon w Enter) takes about 3 times longer than pressing Ctrl-s. Although I have found pressing ctrl-s causes carpal tunnel more than Esc shift colon w enter, it spreads out the strain to have two options to save. Jul 2, 2012 at 17:24
  • 1
    after years of what the hell did i do to freeze my vi session... ctrl-q
    – ron
    Jan 14, 2021 at 19:40

6 Answers 6

170

Ctrl+S is a common command to terminals to stop updating, it was a way to slow the output so you could read it on terminals that didn't have a scrollback buffer. First find out if you can configure your xterm to pass Ctrl+S through to the application. Then these map commands will work:

noremap <silent> <C-S>          :update<CR>
vnoremap <silent> <C-S>         <C-C>:update<CR>
inoremap <silent> <C-S>         <C-O>:update<CR>

BTW: if Ctrl+S freezes your terminal, type Ctrl+Q to get it going again.

5
  • 7
    works with OSX, iTerm2, ZSH, oh-my-zsh when you include "stty -ixon" in the .zshrc file
    – Richard
    Aug 12, 2013 at 18:16
  • 5
    "stty -ixon" in your .bashrc is also required for Terminal.app with bash. May 5, 2014 at 17:23
  • @ned-batchelder Is it ok if you map to Ctrl-W, instead? Will there be a problem with vim or terminal default commands?
    – Thi G.
    Jun 3, 2016 at 22:09
  • @ThiG. <C-W> is usually used for window splitting and window commands
    – Juve
    Jun 26, 2016 at 17:40
  • 7
    the BTW is was the reason the search engines landed me here, thanks. Apr 8, 2017 at 20:28
82

In linux with VI, you want to press Ctrl-S and have it save your document. This worked for me, put the following three lines in your .vimrc file. This file should be located in your home directory: ~/.vimrc. If this file doesn't exist you can create it.

:nmap <c-s> :w<CR>
:imap <c-s> <Esc>:w<CR>a

The first line says: pressing Ctrl-S within a document will perform a :w <enter> keyboard combination.

The second line says: pressing Ctrl-S within a document while in 'insert' mode will escape to normal mode, perform a :w <enter, then press a to get back into insert mode. Your cursor may move during this event.

You may notice that pressing Ctrl-S performs an 'XOFF' which stops commands from being received (If you are using ssh).

To fix that, place these two commands in your ~/.bash_profile

bind -r '\C-s'
stty -ixon

What that does is turn off the binding of Ctrl-S and gets rid of any XOFF onscreen messages when pressing Ctrl-S. Note, after you make changes to your .bash_profile you have to re-run it with the command 'source .bash_profile' or logout/login.

More Info: http://vim.wikia.com/wiki/Map_Ctrl-S_to_save_current_or_new_files

6
  • 3
    For gnome terminal doing adding 'stty -ixon' to my .bashrc was enough.
    – DavidGamba
    Jul 29, 2013 at 15:17
  • 1
    :imap <c-s> <Esc>:w<CR>a <----- This won't work as expected when at the beginning of the line ('a' will append instead of insert, which is what I'd expect). Is there are way to make it do 'a' or 'i' according to where the cursor is? Apr 11, 2014 at 9:07
  • Could you please explain why you use 2 imap commands? It seems like you only need imap <c-s> <Esc>:w<CR>a. Thanks.
    – gwintrob
    Jan 21, 2015 at 22:37
  • I thought the 2nd imap command line had significance, but on further inspection it does nothing. Excellent catch, I removed the lame line. Jan 22, 2015 at 0:59
  • 1
    Is there a reason why you're using a instead of i?
    – DemiImp
    Mar 9, 2019 at 1:15
15

vim

# ~/.vimrc
nnoremap <c-s> :w<CR> " normal mode: save
inoremap <c-s> <Esc>:w<CR>l " insert mode: escape to normal and save
vnoremap <c-s> <Esc>:w<CR> " visual mode: escape to normal and save

zsh (if you use)

# ~/.zshrc
# enable control-s and control-q
stty start undef
stty stop undef
setopt noflowcontrol

bash (if you use)

# ~/.bash_profile or ~/.bashrc
# enable control-s and control-q
stty -ixon
2
  • Shouldn't it be vnoremap <c-s> <Esc>:w<CR> # visual mode: escape to normal and save ? Oct 23, 2015 at 22:44
  • BTW, looks like # doesn't work for comments in .vimrc. Use the double quote ("): " <comment txt>
    – Dut A.
    Aug 21, 2022 at 5:36
12

Mac OSX Terminal + zsh?

In your .zprofile

alias vim="stty stop '' -ixoff; vim"

Why?, What's happening? See Here, but basically for most terminals ctrl+s is already used for something, so this alias vim so that before we run vim we turn off that mapping.

In your .vimrc

nmap <c-s> :w<cr>
imap <c-s> <esc>:w<cr>a

Why? What's happening? This one should be pretty obvious, we're just mapping ctrl+s to different keystrokes depending on if we are in normal mode or insert mode.

0
1

If you are using nvim & init.lua add the following lines

vim.keymap.set({'i'}, '<C-s>', '<C-o>:w<ENTER>')
vim.keymap.set({'n'}, '<C-s>', ':w<ENTER>')
0

Vim ships with a file mswin.vim that maps the more popular shortcuts we all know and love from Windows applications. As documented in :help mswin.vim, the line

source $VIMRUNTIME/mswin.vim

will provide support for Ctrl-C, Ctrl-V etc. and - undocumented but true - Ctrl-S to save the buffer, even in Insert mode. Instead of re-inventing the wheel, use this one-liner to get the most shortcuts with the least unwanted side effects.

Actually, mswin.vim provides the following mappings:

" Use CTRL-S for saving, also in Insert mode (<C-O> doesn't work well when
" using completions).
noremap <C-S>       :update<CR>
vnoremap <C-S>      <C-C>:update<CR>
inoremap <C-S>      <Esc>:update<CR>gi

And yes, it does work on Linux as well (as long as freezing terminals are not the problem as explained in the accepted answer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.