How do I change the filename associated with the current buffer in Vim without saving to disk?
Answer
:file {newname}
Explanation
:file {newname} (short form: :f {newname}) changes the filename Vim associates with the current buffer. The buffer's name in :ls, the status bar, and the % register all update immediately. The file on disk is not touched — the change is in-memory only. A subsequent :w will write to the new name.
How it works
:file {newname}— set the buffer's filename to{newname}:file(no argument) — display the current filename (same as<C-g>)- After renaming, the buffer is marked modified since it has not been saved under the new name
- The original file on disk is unaffected
Example
Create a nameless scratch buffer, assign it a path, then save:
:enew
" ... write some content ...
:file /tmp/notes.txt
:w
The buffer is now saved as /tmp/notes.txt. The original unnamed buffer is gone.
Tips
- Use
:keepalt file {newname}to rename without overwriting the#(alternate file) register - After
:file {newname}, use:wto write, or:saveas {newname}which renames and writes in one step - To rename the actual file on disk, use shell commands (
:!mv old new) or a plugin likevim-eunuch - Useful in scripts: assign a meaningful name to a scratch buffer before passing it to other commands