Henrik N
Henrik N

Reputation: 16294

Write unsaved Vim buffer to file without making it saved

I open a new, unsaved buffer in Vim with :enew. My status line says "[No Name]".

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

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

Upvotes: 6

Views: 3641

Answers (3)

Sato Katsura
Sato Katsura

Reputation: 3086

You can set the name of a buffer with :file:

:file foo

This doesn't affect the modified status of the buffer. And you can set the modified status of a buffer by setting the modified option:

:setlocal modified

This doesn't affect the name of the buffer (or other attributes, for that matter).

Upvotes: 2

buff
buff

Reputation: 2053

If you want to have the buffer unsaved, but save the contents to another file, you can try this trick:

:%!tee new-filename

This will work only on a UNIX system as it executes external tee command.

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

:file new-filename

To learn more, read

:help :file_f

Upvotes: 7

Deb Deppeler
Deb Deppeler

Reputation: 1

  1. Open file for edit and make edits
     vi testFile
  1. Write to the new file (as you did)
     :w newFilename
  1. Return to first buffer
    :buf testFile
  1. continue editing testFile buffer from where you saved to newFilename buffer

Upvotes: 0

Related Questions