Wikia

Vim Tips Wiki

Saving a file

1,623pages on
this wiki
Talk0
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):

:w

If you would like to quit Vim simultaneously, you can use the following command:

:wq

Vim also provides an update command that writes the buffer only when there are unsaved changes:

:up

Analogously to :wq, Vim provides a way to save your file (only if there are unsaved changes) and exit Vim at the same time with:

:x

The difference between {:w, :wq} and {:up, :x} is that the former always writes the buffer to a file even when there are no unsaved changes, so that the last modified timestamp is always updated. The latter commands will not update the last modified timestamp unless the buffer was modified.

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

:wa

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

CommentsEdit

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

Around Wikia's network

Random Wiki