86

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.

d-_-b
  • 19,976
  • 37
  • 134
  • 224
Ratheesh Pai
  • 1,530
  • 1
  • 15
  • 24
  • 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 '10 at 06:14
  • 18
    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. – Eric Leschinski Jul 02 '12 at 17:24
  • after years of what the hell did i do to freeze my vi session... ctrl-q – ron Jan 14 '21 at 19:40

4 Answers4

161

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.

Fabrizio
  • 7,190
  • 6
  • 37
  • 86
Ned Batchelder
  • 345,440
  • 70
  • 544
  • 649
75

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: /home/el/.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

Eric Leschinski
  • 135,913
  • 89
  • 401
  • 325
13

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
Jean-François Fabre
  • 131,796
  • 23
  • 122
  • 195
Chun Yang
  • 2,323
  • 22
  • 16
11

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.

Community
  • 1
  • 1
user160917
  • 8,935
  • 4
  • 49
  • 62