Creating Custom Scratch Buffers in VIM

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

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 (1)For the right syntax highlighting & javascript utilities . 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!

comments powered by Disqus