Sometimes I see a window open with name
I know it is some kind of temporary buffer/window.
How can I create one?
:h scratch returns nothing.
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.Sign up to join this community
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
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 (
Put this function in your vimrc. To create a scratch buffer use
function! Scratch() split noswapfile hide enew setlocal buftype=nofile setlocal bufhidden=hide "setlocal nobuflisted "lcd ~ file scratch endfunction
You can test this quickly by yanking the lines and then
:@". You can then try
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.
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:
(From my VimL library plugin: lh-vim-lib)
function! lh#buffer#scratch(bname, where) abort try 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."'!" endtry setlocal bt=nofile bh=wipe nobl noswf ro return bufnr('%') endfunction
I'm posting the code here, in case his site ever goes offline (I modified the names of the functions slightly):
function! ScratchEdit(cmd, options) exe a:cmd tempname() setl buftype=nofile bufhidden=wipe nobuflisted if !empty(a:options) | exe 'setl' a:options | endif endfunction command! -bar -nargs=* Sedit call ScratchEdit('edit', <q-args>) command! -bar -nargs=* Ssplit call ScratchEdit('split', <q-args>) command! -bar -nargs=* Svsplit call ScratchEdit('vsplit', <q-args>) command! -bar -nargs=* Stabedit call ScratchEdit('tabe', <q-args>)