Looking to “save as” a new file in Vim or set up a shortcut to save while typing? Find out here how to save files in Vim (including “save as”, file encodings and setting up your own shortcuts).
First, the Midnight Commute
Birds flying far do it mostly at night.
A good look at the moon and stars and a lack of diurnal predators are advantages of traveling by night, but that is not all: without the heat and turbulence of the sun’s direct rays, the air is both cooler and calmer to fly through.
This means a more direct route, fewer ups and downs and less chance of overheating. So, flying during the night helps migrating birds save energy.
Now, how about saving something, too—this time in Vim?
How to Save Files in Vim
Save the Current File
Time needed: 1 minute
To save the file you are currently editing in Vim or Neovim:
- Switch to the buffer you want to save in Vim.
“Buffer”: “Buffer” is Vim’s name for a the contents of a document presently open in Vim (either visible or hidden).
Switch to a buffer: You can switch to a buffer by pressing : for command mode in normal mode, then writingbu
orbuffer
followed by the filename (use the Tab key to auto-complete or get a list). - Enter normal mode.
Here’s how: Press Esc, for instance, to exit insert mode in Vim.
- Press : (colon) for command mode.
- Type
w
orwrite
.Example: The Vim command line would read
:w
, for instance.
Write and quit: An oft-used command is:wq
to save the current file and then close the editor window (or quit Vim if you are in the last open window); see below for similar commands. - Press Enter.
Possible errors
E45: When you try to save a file that is set as read-only, you get error E45: ‘readonly’ option is set; you can force Vim to (attempt to) save the read-only file using:w!
instead of:w
.
E32: When you try to save a new file that does not yet have a name, you will get error E32: No file name; see below for supplying a file name.
E212: When you try to save to a file that you truly cannot change (because you do not have the permission or the file name is invalid, for instance), Vim returns error E212: Can’t open file for writing even with the:w!
command.
Wrote what you did not mean to write? How to Undo in Vim
Save a Copy with a Different (or New) File Name
To save the current buffer under a specific file (but not change the name of the buffer):
- Go to the buffer you want to save.
- Type
:w <path and filename>
and replace <path and filename> with the name you would like to use for the copy.
Force: To force writing a read-only file, use:w! <path and filename>
.
Important: The name for the buffer will not change; if you use the :w command afterwards, it will use the original file name.
Exception: The exception are unnamed files; these will assume the name from the written copy.
Save as: You can also change the buffer’s name together with saving the file; see below.
Example::w ~/ladedu.txt
saves a copy to the file ladedu.com in the Home directory and closes the copy, leaving the original buffer open. - Press Enter.
“Save As…” in Vim
To save a file using a new or different file name and continue editing the new copy in Vim:
- Open the file’s buffer in Vim.
- Type
:sav <path and file name>
and replace <path and file name> with the name (and, optionally, directory) you would like to use.
Overwrite: Use:sav! <path and file name>
to force writing to the new file.
Long version: You can also use:saveas
instead of:sav
.
Example::sav ~/ladedu.txt
will save a copy of the current buffer as ladedu.txt, and you will continue editing that buffer instead of the original file (which will be hidden in Vim, but remain open). - Press Enter.
Save Everything in Vim
To save all files currently open in Vim:
- Use
:wa
in command mode.
Forcing it: You can, again, force Vim to write read-only files using :wa! instead. - Press Enter.
Save and Quit
To save the current or all files and quit Vim in one go:
- Use
:x
or:wq
in command mode.
Overwriting: You can, again, append ! to the end of the commands to save and overwrite read-only files.
Normal mode: In normal mode, you can press Z Z for saving the current file, then quitting.
Nothing new: Note that:x
only saves the file when you have made changes,:wq
will always save (and change the file’s modification time, for instance). - Press Enter.
Using Graphical Vim
In Vim using a graphical interface (instead of the terminal), you can, of course, also save using menu or toolbar.
To save a file in Gvim:
- Select File | Save from the menu or click the Save current file button in the toolbar.
To save a file under a new name in Gvim:
- Select File | Save As… from the menu.
How to Save Files in Vim: FAQ
Can I save from normal mode or insert mode?
Yes.
You can map a key combination to saving the current file for normal and insert mode. For example, to make Ctrl S save when you are typing in insert mode and Z S write the file in normal mode, for instance, add the following to your .vimrc file:
inoremap <C-S> <C-O>:w<CR>
nnoremap ZS :w<CR>
(Here’s what it does in part: <C-O>
stands for Ctrl O, which exits insert mode for just one command in Vim.)
Can I save a file from the buffer list in Vim?
No.
To save a file from the buffer list, first go to the buffer, then save the document as above.
Exception: You can, of course, save all edited files without opening them separately using the :sa
command; see above.
Can I save a file using a specific charset and newline character format?
Yes.
To change the encoding for writing a file:
- Append
++ff=[unix|dos|mac]
to the saving command in command mode.
Your pick: Choose from Unix, DOS/Windows and Mac encoding, e.g, +ff=unix for Unix/Linux newline and end-of-file conventions.
To write a file using a specific character encoding:
- Append ++enc=[encoding name]
Your pick: Vim supports a large number of character encodings to choose from.
Example: Use:w ++enc=iso-8859-7
to save a file with the (historic) Latin/Greek character encoding, if some software expects this, for instance.
Important : It is easy to end up with a jumbled mess when saving a file in one character encoding, then opening it with software set to another (or entirely ignorant of the encoding in use). Take care to have a Unicode copy around as a backup. In Vim, Unicode is usually the default and can be set with++enc=utf8
for saving.
(How to save files tested with Vim 9.0–9.1 and Neovim 0.9.5; first published October 2023, last updated April 2024)