vimtricks.wiki Concise Vim tricks, one at a time.

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 to newfile.txt and 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 :saveas to 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.txt keeps the current filename
  • After :saveas, the original file is no longer the active buffer — use :e # or :bprev to return to it
  • :saveas! overwrites an existing file without prompting

Next

How do I visually select a double-quoted string including the quotes themselves?