Writing an unsaved Vim buffer to a file without saving it

I open a new unsaved buffer in Vim with :enew . My status bar says "[No name]".

If I do :w /tmp/foo , the status bar changes to "/ tmp / foo".

How can I write contents to a file while saving an unsaved buffer (unmodified, unnamed)?

+5
source share
3 answers

If you want the buffer to be saved, but save the contents in another file, you can try this trick:

 :%!tee new-filename 

This will only work on a UNIX system, as it runs an external tee command.

On the other hand, if you want to name the buffer, but leave it unsaved, try the following:

 :file new-filename 

Read more to learn more.

 :help :file_f 
+6
source

You can set the buffer name with :file :

 :file foo 

This does not affect the state of the modified buffer. And you can set the modified buffer status by setting the modified option:

 :setlocal modified 

This does not affect the buffer name (or other attributes, for that matter).

+2
source
  • Open the file to edit and make changes.
  vi testFile 
  1. Write to a new file (like you)
  : w newFilename 
  1. Return to first buffer
  : buf testFile 
  1. continue editing the testFile buffer from which you saved the newFilename buffer
0
source

All Articles