Tip #294: Use Ctrl-S to save current or new files.
tip karma |
Rating 30/16, Viewed by 2187
|
Read and edit this tip on the
Vim tip wiki.
The wiki may have a more recent version of this tip.
created: |
|
July 30, 2002 12:37 |
|
complexity: |
|
basic |
author: |
|
Thomas R. Kimpton |
|
as of Vim: |
|
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 has("gui_running")
" If the current buffer has never been saved, it will have no name,
" call the file browser to save it, otherwise just save it.
:map <silent> <C-S> :if expand("%") == ""<CR>:browse confirm w<CR>:else<CR>:confirm w<CR>:endif<CR>
endif
Tom Kimpton
<< remember where you had ended reading help |
Line/word/file/whatever completion >>
Additional Notes
[email protected],
July 30, 2002 20:14
|
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>
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>
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.)
|
atkinssc AT engr orst edu,
August 5, 2002 15:09
|
use:
imap <c-s> <c-o><c-s><cr>
So that the confirmation doesn't hold you up.
|
jaldripublic at comcast dot net,
November 17, 2003 7:57
|
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>
|
[email protected],
February 24, 2005 4:06
|
I use vim 6.3 but this doesn't work. Any other way to have this saving featrure?
|
[email protected],
May 25, 2005 17:25
|
I am using 6.3 and am also having problems using this very useful mapping. I look at my current mappings by entering the command :map and it shows me the following:
v <C-S> * <C-C>:update<CR>
no <C-S> * :update<CR>
These two mappings are the only ones with those astericks in them. I guess this means they are not functioning mappings? Is this true and if so why are they not functioning? I have tried every combination of the mappings listed above and non of them work so I imagine that I am just unable to remap <C-S>. Any ideas? If there is a better place to post this question please let me know.
|
[email protected],
May 25, 2005 19:49
|
Very strange stuff. After debugging this a bit further I got it to work with CTRL-F. Here is the code:
" Use CTRL-F for saving the file
nmap <C-F> :update<CR>
vnoremap <C-F> <C-C>:update<CR>
inoremap <C-F> <C-O>:update<CR>
But the really strange thing is that I can use CTRL-S in almost the exact same fassion with the bit of code below and it works fine. But when I do the code above with CNTL-S it won't reognize the sequence?
" Use CTRL-S for saving the session
map <C-S> :mksession! ~/tmp/current_book.vim
I also wanted to do the following bit of code with CTRL-Q but it wouldn't recognize the commands so I found that CTRL-X will work.
" Use CTRL-X for closing the file
noremap <C-X> :quit<CR>
vnoremap <C-X> <C-C>:quit<CR>
inoremap <C-X> <C-O>:quit<CR>
For refrence here is my setup:
All the code on this page works on vim and gvim 6.3 running RedHat and Gnome window manager. I run all of this through a VNC session on my windows laptop. I have switched my Caps Lock and my Left Ctrl key so my wrists don't get all tweeked up and so I stay closer to home row. I did this with KeyTweak (Very easy program to use) http://webpages.charter.net/krumsick/
|
|