Saving a file

From Vim Tips Wiki

Jump to: navigation, search
Tip 803 Printable Monobook Previous Next

created 2004 · complexity basic · version 6.0

To save a file, you would normally first leave insert mode by hitting the Esc key one or more times. Then you type the following (and press Enter):


You can save your file (if modified) and exit Vim at the same time with:


You can save all modified buffers (all open files) with:


For a tweak, you can set Vim to automatically save the current buffer when you hit Esc twice. This can be done by simply adding the following line to your vimrc:

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

Note: In a console, mapping with a single Esc interferes with the Up/Down/Left/Right arrow keys, since these are actually escape sequences and send an Esc followed by a key id. (The reason that hitting Esc in Vim does not always give an immediate response is that Vim first waits a little to see if a key id is following the Esc.)

[edit] Comments

This would also work.

ino <Leader>:w <Esc>:w<CR>a

I use the following:

nmap <c-s> :w<CR>
vmap <c-s> <Esc><c-s>gv
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.

Under UNIX/Linux, in a console or terminal, CTRL-S sends the signal SIGSTOP, which stops the process in foreground until SIGCONT is sent; this is done by CTRL-Q. This behaviour can usually be disabled by executing
stty -ixon
(add it to your shell's profile or rc file so it will be executed whenever you log in).
Personal tools