Sometimes I see a window open with name scratch.

I know it is some kind of temporary buffer/window.

How can I crate one?

:h scratch returns nothing.


It is probably a scratch buffer, which can be named. From the help:


Contains text that can be discarded at any time. It is kept when closing the window, it must be deleted explicitly. Settings: >

      :setlocal buftype=nofile
      :setlocal bufhidden=hide
      :setlocal noswapfile
  The buffer name can be used to identify the buffer, if you give it a meaningful name.

This can be found in :help special-buffers.

| improve this answer | |
  • 1
    which :h command used to access this help? – wizzup Feb 8 '17 at 13:33
  • 2
    :h buffer, then read down through "Special kinds of buffers". You were on the right track. – wbogacz Feb 8 '17 at 13:36
  • When you open a file using any of the Vim commands, a buffer is automatically created. For example, if you use :edit file to edit a file, a new buffer is automatically created. An empty buffer can be created by entering :enew or :new or :vnew. From Vim buffer FAQ: vim.fandom.com/wiki/Vim_buffer_FAQ – NeilG Oct 2 '19 at 23:15
  • 2
    @wizzup :helpgrep scratch turns it up as one of 10 (in my installation) results. – Rich Oct 3 '19 at 13:05

I remember a vim tip (which has been migrated to vim.wikia) on the subject.

The latest version of the function I use to create scratch buffers also sets:

  • 'buflisted' to false
  • 'readonly'

(From my VimL library plugin: lh-vim-lib)

function! lh#buffer#scratch(bname, where) abort
    set modifiable
    " The next function takes care of working around this damn E36
    call lh#window#create_window_with(a:where.' sp '.escape(substitute(a:bname, '\*', '...', 'g'), '#%'))
  catch /.*/
    throw "Can't open a buffer named '".a:bname."'!"
  setlocal bt=nofile bh=wipe nobl noswf ro
  return bufnr('%')
| improve this answer | |

A 'scratch' buffer is just an informal term for a place to type arbitrary temporary content.

Following the accepted answer and another question, I created the following function in my vimrc.

The function creates a blank buffer in the current window and names it 'scratch'. The scratch buffer is not protected if you try to quit. It will be discarded without prompting even with unsaved changes if you quit Vim or wipe it (:bw).

Put this function in your vimrc. To create a scratch buffer use :call Scratch().

function! Scratch()
    noswapfile hide enew
    setlocal buftype=nofile
    setlocal bufhidden=hide
    "setlocal nobuflisted
    "lcd ~
    file scratch

You can test this quickly by yanking the lines and then :@". You can then try :call Scratch().

A split is used so your current buffer is not affected when you create the scratch buffer.

(If you prefer to omit the split from the function then your current buffer would be "hidden" (no longer in a window) when you create the scratch buffer. Vim will still stop and prompt you if you try to quit with unsaved changes in that buffer).

The scratch buffer would normally have the same working directory of the buffer you are in when you call the Scratch function. I've included a commented out line to change the scratch buffer working directory to home. This is for those with funny status lines that may display the path of the current directory as the buffer path. Or just for those who prefer it.

You can set the scratch buffer to be "listed" or "unlisted" (listed by default). If you make it unlisted you won't see it in your :buffers list, but it will be in :buffers!. Uncomment the nobuflisted line if you prefer it to be unlisted. Listed or unlisted, you can always change to your scratch buffer with :b scratch anyway. Making the scratch buffer listed makes it slightly more accessible. For instance :sball will include it in a split.

If you use :bw (buffer wipe) on the scratch buffer Vim will delete the content and remove the buffer without warning. If you use :bd (buffer delete) on the scratch buffer Vim will drop the content without warning and make the buffer unlisted.

You can create multiple scratch buffers. Vim starts to complain but you still end up with additional ones.

| improve this answer | |
  • 2
    The execute’s arent necessary. Your function can just run enew etc. – D. Ben Knoble Oct 3 '19 at 12:20
  • 1
    If you set buftype=hide before setting the name then changing the directory isn't necessary, because the buffer name will not be "handled like a file name." Also, might be worth mentioning 'nobuflisted', which prevents the buffer from showing up in the output of :ls. You sort of alluded to it, but didn't mention it explicitly. – Rich Oct 3 '19 at 13:17
  • Thanks for your comment, @D.BenKnoble. I've cleaned up the function on your advice. – NeilG Oct 6 '19 at 13:36
  • 1
    buftype=hide is indeed nonsense, and yes, I was actually talking about buftype=nofile :). But I can't reproduce the behaviour you describe, and still believe the statement is Vim buffer names are always filenames is incorrect. If I remove the cd line from your function and then run it, there is no path in my statusline or in the output of echo bufname('%') I wonder if perhaps this is an issue with your statusline rather than the buffer name. What is your 'statusline' option set to? – Rich Oct 7 '19 at 11:13
  • 1
    Aha! Glad we got it figured out! If it was me, I'd still prefer not to change directory in the Scratch function, so instead I'd change statusline to use something like expand(&buftype == 'nofile' ? '%' : '%:p:~:h'), but I think now we're into the realms of personal preference. If you decide to stick with the change directory solution, then it might be better to use :lcd to confine the changes to the window in question, but again that's kind of up to you. Anyhow, I think this is a great answer. +1! – Rich Oct 9 '19 at 9:48

I have created a "Scratch" command

command! Scratch new | setlocal bt=nofile bh=wipe nobl noswapfile nu
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.