Skip to content

Instantly share code, notes, and snippets.

@dantonyuk
Last active February 21, 2022 02:39
Show Gist options
  • Save dantonyuk/5103bab981a3efc437ad612965466b75 to your computer and use it in GitHub Desktop.
Save dantonyuk/5103bab981a3efc437ad612965466b75 to your computer and use it in GitHub Desktop.
" A command that creates a scratch buffer.
"
" A scratch buffer is an unlisted buffer that is not assigned to a file,
" and will be wiped out as soon as you're out of it.
"
" 1. Opens a scratch buffer in a new window.
" 2. With a bang (:Scratch!) opens it in the same window.
" 3. Accepts arguments (:Scratch ft=javascript).
" 4. It auto-completes the arguments.
function! Scratch(bang, options) abort
if a:bang
let l:command = ':enew'
else
let l:command = ':new'
endif
let l:command .= ' | setlocal buftype=nofile bufhidden=wipe nobuflisted'
if len(a:options) > 0
let l:command .= ' ' . a:options
endif
execute 'silent ' . l:command
endfunction
command! -bang -nargs=* -complete=option Scratch call Scratch(<bang>0, <q-args>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment