sponsor Vim development Vim logo Vim Book Ad

basic Tip #803: Saving a file

 tip karma   Rating -9/13, Viewed by 626 

created:   October 6, 2004 12:58      complexity:   basic
author:   Anonymous      as of Vim:   6.0

In order to save a file you'll have to come out of the insert mode, commonly by hitting the Esc key.

Once you are up there to hit the Esc key, why not make it save the file too with a few more hits.
This can be done by simply adding the following line in the .vimrc file:

map <Esc><Esc> :w <CR>

Note: Mapping with a single Esc is somehow causing problems with the Up/Down/Left/Right arrow keys.

 rate this tip  Life Changing Helpful Unfulfilling 

<<Specify what you do not want to have in a search and other helpful searches. |

Additional Notes

Anonymous, October 7, 2004 5:31
This would also work.
ino <leader>:w <esc>:w<cr>a

Mark
[email protected], October 7, 2004 6:54
I use the following:

nmap <c-s> :w<CR>
vmap <c-s> <esc><c-s>
imap <c-s> <esc><c-s>

nmap <f2> :update<cr>
vmap <f2> <esc><f2>gv
imap <f2> <c-o><f2>

Control-S ALWAYS saves the file, and, if called from visual mode, restores the visual selection when done.  It does NOT re-enter insert mode, though, so I use it as my quit-insert-mode-and-save macro.

F2 only saves if necessary AND returns the user to insert mode (or restores their visual selection), as needed.

For your purposes of save-in-insert-mode-and-come-back, I suggest you take the imap for <f2> and change the key combination to whatever you like (it only saves when necessary).
[email protected], October 7, 2004 6:55
Sorry, the visual-mode mapping for Control-S should be:

vmap <c-s> <esc><c-s>gv

As it was in my original comment, it wouldn't have restored the selection when it was done.
Anonymous, October 10, 2004 23:10
Thanks to all in the thread!  This helps another anonymous person out there.  Me.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo