Scratch Buffers in Vim

Vim has the concept of buffers which represent the contents of a file, or more abstractly, the text you are interacting with. This separation of concerns allows Vim windows (split screens), Vim buffers, and files written to disc--to be remixed at will. For example, one buffer might have multiple split screens which let you view different parts of the same file at the same time. Vim has special buffers for other purposes like a terminal buffer or file browser (directory) buffer.

One special buffer in Vim is the [Scratch] buffer. This is a temporary buffer, which has no underlying file, is never written to disc, and which is discarded when Vim exits. This can be useful if you want to edit a large blob of text without creating a temporary file.

In my work, I typically would leave a bunch of temp.txt files behind after working. And, having a backing file might be important if you want to save or recover from a computer crash. But, after reading about scratch buffers, I wanted to give myself the option of easily creating a scratch buffer. That way, I have another option if I do not want to use a temporary file for editing.

Below, I demonstrate some examples for quickly creating and reusing scratch buffers in Vim.

Note that Vim has a lot of jargon specific to it. You might want to review *buffers* in the help file (which also covers what a window is), *vim-modes*, and, if Vim is new to you, you might start with just :help which is the main help file and which lists basic information, and links to the user manual and tutorial. There are also online tutorials.

Create a Scratch Buffer

My approach for quickly creating scratch buffers is to save a function in my VIMRC and then to call that function when I want a scratch buffer.

Copy the function below into $MYVIMRC and save the changes.

Note that I have taken the added step of naming the scratch buffer, i.e., "MyScratchBuffer".

And, I've added the 'nowrap' option as my personal preference. If you don't like this, you can omit 'nowrap'

function ScratchBuffer()
  vnew MyScratchBuffer
  setlocal buftype=nofile bufhidden=hide noswapfile nowrap
  return bufnr('%')
endfunction

For the function to take effect, open a new instance of Vim, or :source $MYVIMRC

Call the user function to create a scratch buffer in a new window:

:call ScratchBuffer()

Open an Existing Scratch Buffer

If you previously created a scratch buffer in a given Vim instance, then you can open the same buffer in other windows.

Since I named my scratch buffer in my function (i.e. MyScratchBuffer), we can simply rerun the function to open the same buffer:

:call ScratchBuffer()

Remember that a "window" in Vim is just a split screen, or a new pane with a view of the same file.

Deleting a Scratch Buffer

Remember that a scratch buffer has no backing file and will simply disappear once Vim closes.

But, if you want to actively remove the scratch buffer from the buffer list, you can use the :bdelete command:

:bdelete MyScratchBuffer

If you knew the buffer number (discussed below), you could also use that:

:bdelete 2

Background Information

The above advice abstracts away a lot of the manual steps used to create and manage scratch buffers. That information might be useful if you want a different solution.

To begin with, there is no Vim command specifically used to create a scratch buffer. Instead, scratch buffers are simply defined, conceptually, in the help files. And, you are left to use the existing commands to make your own scratch buffers by applying the criteria.

To make a scratch buffer, you create a new buffer and then apply local options to turn it into a scratch buffer.

So, there are two things you have to understand:

What is a scratch buffer?

In Vim, a scratch buffer has the following local options set on it:

:setlocal buftype=nofile
:setlocal bufhidden=hide
:setlocal noswapfile

Any existing buffer that has these local options set on it will become a scratch buffer.

For example, if you open an existing file and then set these local options on that buffer, it will break the link with the underlying file. Then any changes to the file on disc won't be reflected in the buffer, and any changes to the buffer won't be written to disc.

So, a scratch buffer is just a buffer with those local options applied to it.

How do I create a buffer?

So, that begs the question of how to create a buffer: something which can then be configured as a scratch buffer.

Generally, the :new commands create buffers.

So, you would use one of those :new commands to create a buffer.

:new 

Manually Creating a Scratch Buffer

Here are the steps for manually creating a scratch buffer.

This also has the advantage of allowing you to create more than one scratch buffer.

" Create a new buffer. 
:new 

" Apply the local scratch buffer options to the active buffer. 
:setlocal buftype=nofile bufhidden=hide noswapfile

Or, you could use different buffer names with your :new commands to create multiple named scratch buffers. Remember to apply the options to each new buffer.

" Use a different name each time. 
:new [optional_buffer_name]

" Apply the options to each new buffer. 
:setlocal buftype=nofile bufhidden=hide noswapfile

List Buffers

Vim uses the :buffers command to list the currently loaded buffers.

For example:

:buffers
  1 #a + "notes.txt"                    line 0
  2 %a   "[Scratch]"                    line 3
  3  a   "links.txt"                    line 1
  6      "help"                         line 0
Press ENTER or type command to continue

A new buffer that has the scratch options applied to it is given the special buffer name "[Scratch]", as shown above.

Note that the output from the :buffers command includes a unique number, e.g., 1,2,3,6; for each buffer in the current session. And, a buffer can be referenced by that number (see next section).

However, remember that my function gives the buffer a name, MyScratchBuffer. So, if you use my function and :buffers then you will see "MyScratchBuffer" instead of "[Scratch]":

:buffers
  1 #a   "[No Name]"                    line 1
  2 %a   "MyScratchBuffer"              line 1
Press ENTER or type command to continue

Edit Buffer [N]

There are several Vim commands to open, or edit, an existing buffer.

After using the :buffers command to list the buffers and show their buffer numbers, you can use the commands below to open a buffer. Each command accepts a buffer number as an argument. Just substitute your buffer number to open that buffer.

" Open a buffer in the current window. 
:buffer 2

" Split a new window and open a buffer there. 
:sbuffer 3

" Vertically split a window. 
:vertical sbuffer 6

Note these work with names as well.

:buffer MyScratchBuffer
:sbuffer OtherBuffer
:vertical sbuffer FreyaTheWalrus

There are also two keyboard shortcuts you can use to access buffers by number.

Each of these shortcuts accepts a "count" which is a number pressed immediately before pressing the shortcut. In this case, the count represents the buffer number, e.g., 1, 2, 3, etc.

One shortcut opens the specified buffer in the current window. The other shortcut splits a new window and opens the buffer there.

" Same window. 
2 Ctrl-^

" Split window. 
6 Ctrl-W ^

where "2" and "6" are the buffer numbers in the examples above.

Note the caret "^" is above the 6 key on US keyboards. So you must press Shift-6 to input a caret.

Check for a Scratch Buffer

Since a scratch buffer is defined by local options, the only way to definitively check if a given buffer is a scratch buffer is to check those options.

We can do that by combining the options with question marks (?) to query the current values:

:setlocal buftype? bufhidden? swapfile?
  buftype=nofile
  bufhidden=hide
noswapfile
Press ENTER or type command to continue

Remember that a scratch buffer has the values shown in the output above. This is how you would check manually.

If you want to check automatically from a script, then I included one link below which discusses checking the value of an option in Vimscript. If you need more help with scripting in Vim, you'll have to search online.

References

Vim Buffers (and most of the help you need.) | VimHelp.org
Vim Modes | VimHelp.org
:help (Start Here)
Interactive Vim Tutorial | OpenVim
vimrc | VimHelp.org
Is there a shorter way to start vim with scratch buffer? | Stack Exchange
How to detect the buffer number of new buffer? | Stack Exchange
{count}CTRL-^ | VimHelp.org
:set {option}? | VimHelp.org
How do I check the value of a Vim option in Vimscript? | Stack Exchange

Created: Sunday, October 26, 2025

Updated: Monday, October 27, 2025