The other day I wanted to open a scratch buffer to write some temporary code and the pass it on to a repl to test it out, the idea was that I could modify it as needed and resend it back to the repl and see the changes, helps to debug easily.

So I just wrote out this simple code here that allows me to launch a scratch buffer in different ways, but the difference is I can set various options for it while creating it. First lets have a look at the code :

function! functions#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 functions#ScratchEdit('edit', <q-args>) command! -bar -nargs=* Ssplit call functions#ScratchEdit('split', <q-args>) command! -bar -nargs=* Svsplit call functions#ScratchEdit('vsplit', <q-args>) command! -bar -nargs=* Stabedit call functions#ScratchEdit('tabe', <q-args>) 1 2 3 4 5 6 7 8 9 10 function ! functions # 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 functions # ScratchEdit ( 'edit' , < q - args > ) command ! - bar - nargs = * Ssplit call functions # ScratchEdit ( 'split' , < q - args > ) command ! - bar - nargs = * Svsplit call functions # ScratchEdit ( 'vsplit' , < q - args > ) command ! - bar - nargs = * Stabedit call functions # ScratchEdit ( 'tabe' , < q - args > )

Now this allows me to open a scratch buffer with various options, like I could call :Sedit ft=javascript and that would open a scratch buffer with filetype javascript (and hence show the right syntax highlighting). The cool part is all these options are set local to the buffer and can just be space separated to the command. The buffer will not be listed in :ls and after you :q it will be wiped out completely from vim. Couldn’t be any simpler!