Reputation: 1630
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
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
Reputation: 2601
# ~/.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
# ~/.zshrc
# enable control-s and control-q
stty start undef
stty stop undef
setopt noflowcontrol
# ~/.bash_profile or ~/.bashrc
# enable control-s and control-q
stty -ixon
Upvotes: 16
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
Reputation: 9350
zsh
)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.
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
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
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