vimtricks.wiki Concise Vim tricks, one at a time.

How do I create a temporary scratch buffer for quick notes without saving a file?

Answer

:enew | setlocal buftype=nofile bufhidden=wipe noswapfile

Explanation

A scratch buffer is a temporary, unnamed buffer that exists only in memory — it won't prompt you to save when you close it and leaves no trace on disk. This is perfect for jotting down quick notes, pasting intermediate results, or experimenting with text transformations without cluttering your filesystem.

How it works

  • :enew opens a new empty buffer in the current window
  • setlocal buftype=nofile tells Vim this buffer is not associated with any file on disk
  • bufhidden=wipe automatically destroys the buffer when it is no longer displayed in any window
  • noswapfile prevents Vim from creating a swap file for this buffer

Together, these options create a fully disposable buffer that behaves like a scratchpad.

Example

You're debugging and need a place to paste and compare some output. Run:

:enew | setlocal buftype=nofile bufhidden=wipe noswapfile

A blank buffer opens. Paste text into it, manipulate it freely, and when you're done, just close the window with :q — no "save file?" prompt, no leftover files.

For frequent use, add a mapping to your vimrc:

nnoremap <leader>s :enew<CR>:setlocal buftype=nofile bufhidden=wipe noswapfile<CR>

Now <leader>s instantly opens a scratch buffer.

Tips

  • Open the scratch buffer in a split instead: :vnew | setlocal buftype=nofile bufhidden=wipe noswapfile for a vertical split, or :new | ... for a horizontal split
  • Use bufhidden=hide instead of wipe if you want the buffer to persist in the background when you navigate away, so you can return to it with :b or :ls
  • You can name a scratch buffer for easier identification: :file scratch-notes gives it a display name without creating a file
  • Scratch buffers are ideal for capturing the output of Ex commands: :put =execute('ls') pastes the buffer list into the current buffer
  • Use :read !command in a scratch buffer to capture shell command output for inspection
  • If you accidentally close a scratch buffer, the contents are gone — there is no undo or recovery, so copy important text to a named register or file before closing

Next

How do I edit multiple lines at once using multiple cursors in Vim?