Saving Buffers


Vim can open files and create buffers using a variety of commands:

CommandActionPattern
:editedit a file:edit [path/to/file]
:readread file into the text:read [path/to/file]
:newcreate a new empty window
:enewedit a new, unnamed buffer
:writewrite to a file:w[rite] [path/to/file]

Commands that take paths generally accept both relative and absolute paths.

The commands summarized above each perform similar tasks, but the :read command offers and extra bit of functionality that can be useful in some situations: in addition to accepting a path as an argument, :read can also insert the output of shell commands into the current buffer. For example, to insert the current time into Vim one might execute:

:read !date '%T'

which inserts the current time at the current cursor location.

No-Name Buffers

You might have noticed that when a file is loaded into a buffer, the buffer takes the name of the file. So what happens when an empty buffer is opened? In that case, the buffer is called a "no-name" buffer.

No-name buffers are just like any other buffers, and the content can be saved to a file or simply thrown away.

To save the content from a no-name buffer to a file, simply add a filename to the :write command:

:write /path/to/file

This saves the current buffer content into the specified path, then updates the buffer to reflect the filename.

Saving Buffers to Files

Although not particularly useful in most cases, it is still useful to note that ranges can be applied to saving files.

:5,9wq

For example, if we had a file with 10 lines of content and in which every line was modified in some way, we could choose to write only the changes on lines 5–9, by using the following command: