file
:w / :saveas
Write (save) the current buffer to disk. Without an argument :w saves to the current file; with an argument it writes a copy without switching buffers. Use :saveas when you want to rename the buffer to the new file and keep editing that one.
:w [filename] :saveas {filename} (command mode) Variations
| Keystroke | Effect |
|---|---|
| :w! | Force write, even if read-only or file changed on disk |
| :w newfile.txt | Write buffer to newfile.txt (buffer stays on original) |
| :wa | Write all modified buffers |
| :saveas draft.md | Save AS a new filename and switch the buffer to it |
| :w !sudo tee % | Classic trick to save a file that needs root |
Examples
:w Save current file
:w backup.txt Save a copy as backup.txt without switching to it
:saveas notes.md Save as notes.md and keep editing notes.md going forward
:wa Save every modified buffer in the session
Gotcha
:w newname just copies — the buffer still points at the original; use :saveas if you want to actually 'rename and continue editing'. New [No Name] buffers require a filename to :w.
Related
:wq / ZZ
Write the buffer to disk and quit the window. ZZ (normal mode, two capital Zs) does the same thing without leaving the keyboard's home row — with the subtle difference that ZZ only writes if the buffer actually changed.
:q / :q!
:q closes the current window (or exits Vim if it's the last one); :q! quits and DISCARDS unsaved changes. If you're the 'how do I exit Vim' meme — this is the answer.
:e
Edit (open) a file in the current window. With no argument, :e reloads the current file from disk — handy after external changes.