Ratheesh Pai
Ratheesh Pai

Reputation: 1630

How can I map "save" to Ctrl + S in Vim?

In Vim, how can I map "save" (:w) to Ctrl + S?

I am trying "map" the command, but xterm freezes when I press Ctrl + X.

If I give Ctrl + V, Ctrl + S, still I see only a ^, not ^S.

Upvotes: 97

Views: 59201

Answers (6)

Friedrich
Friedrich

Reputation: 4846

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. as well as the (undocumented) Ctrl-S to save the buffer, even in Insert mode. Instead of re-inventing the wheel, you can use an 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

This will also work on Linux (as long as freezing terminals are not the problem as explained in the accepted answer).

Upvotes: 0

Chun Yang
Chun Yang

Reputation: 2601

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

Z shell (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

Upvotes: 16

Eric Leschinski
Eric Leschinski

Reputation: 154101

On 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, and 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 file:

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

It 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 file you have to rerun it with the command 'source .bash_profile' or logout/login.

More information: Map Ctrl + S to save current or new files

Upvotes: 82

user160917
user160917

Reputation: 9350

Mac OS X Terminal + Z shell (executable zsh)

In your .zprofile file

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 aliases Vim, so that before we run Vim, we turn off that mapping.

In your .vimrc file

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.

Upvotes: 12

user23216039
user23216039

Reputation: 21

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

Upvotes: 2

Ned Batchelder
Ned Batchelder

Reputation: 376052

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.

Upvotes: 171

Related Questions