Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write unsaved Vim buffer to file without making it saved

Tags:

vim

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)?

like image 693
Henrik N Avatar asked Jul 17 '15 20:07

Henrik N


People also ask

How do I close a single buffer in Vim?

Close buffer named Name (as shown by :ls ). Assuming the default backslash leader key, you can also press \bd to close (delete) the buffer in the current window (same as :Bclose ).

How do I save a buffer in Vim?

The :wa and :xa commands only write a file when its buffer has been changed. By contrast, the :w command always writes the current buffer to its file (use :update to save the current buffer only if it has been changed). Warning: If you enter :qa! , Vim will discard all changes without asking "are you sure?".

Where are vim buffers stored?

For example, when you start VIM without supplying a file for it to open, VIM starts with an empty buffer. If you want to save the changes to that buffer, then you have to tell VIM what file to write those changes to. So again, a buffer resides in memory.


1 Answers

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
like image 144
buff Avatar answered Sep 27 '22 21:09

buff