How do I save a file under a new name in Vim?
Answer
:saveas
Explanation
The :saveas {filename} command writes the current buffer to a new file and makes that new file the current buffer's file. It's Vim's equivalent of "Save As" — the original file is left unchanged and you continue editing the new copy.
How it works
:saveas newfile.txt— write the buffer tonewfile.txtand switch to it- After the command, the buffer's name changes to
newfile.txt - The original file is not modified or deleted
This differs from :w newfile.txt, which writes to the new file but keeps the original as the buffer's current file.
Example
You have config.json open and want a modified copy:
:saveas config.backup.json
Before: editing config.json
After: editing config.backup.json (config.json unchanged)
Tips
- Use tab-completion after
:saveasto navigate directories quickly - To save to a different directory:
:saveas ../backup/config.json - If you just want to write a copy without switching:
:w newfile.txtkeeps the current filename - After
:saveas, the original file is no longer the active buffer — use:e #or:bprevto return to it :saveas!overwrites an existing file without prompting