4

Often times I use vim as a scratchpad - I just want to throw some text in there, maybe copy it to the clipboard, or munge it a bit, but it's not really important to keep it around.

Most of the time I do vim /tmp/foo.txt or whatever, but this gets a bit annoying. I combined this answer with some responses on a previous question of mine and came up with this solution:

vim +noswapfile +"set buftype=nofile" +"set bufhidden=hide"

Which actually works! But... it's a bit wordy.

Is there a shorter way to start vim with a scratch buffer (without using a plugin)?

3
  • Have you considered making that an alias?
    – Herb
    Commented Mar 23, 2020 at 19:17
  • I'm confused. If I enter the plain command vim on the terminal I get an instance of vim with a scratch buffer. Isn't that what you're wanting?
    – eff
    Commented Apr 30 at 8:54
  • :q doesn't quit - it requires saving or :q! Commented May 1 at 12:57

3 Answers 3

1

A sh/bash answer (even though the site is vi)

vtmp () {
  vim +noswapfile +"setlocal buftype=nofile" +"setlocal bufhidden=hide" "$@"
}

A vim answer:

Put this function in your vimrc or in a plugin/ file:

function Scratch()
  setlocal noswapfile
  setlocal buftype=nofile
  setlocal bufhidden=hide
endfunction

And then call vim as vim -c 'call Scratch()', or make a sh function similar to the above.

2
  • Might be more convenient to use a command over a function: :command! S setlocal noswapfile buftype=nofile bufhidden=hide; you can then call it with vim +S, and converting an existing buffer is just :S (can of course also use a longer command name, but just :S works well enough for me). Also, your shell command can be a wee bit shorter by combining the settings in one :setlocal. Commented Mar 25, 2020 at 22:28
  • @MartinTournoij good points. I’ll add alternatives later.
    – D. Ben Knoble
    Commented Mar 25, 2020 at 23:41
1

Create a function, and then call said function from console when opening vim with the -c flag?

1

There might be better approaches (e.g. alias to vim +noswapfile +"set buftype=nofile" +"set bufhidden=hide") but you can also use VimEnter autocommand to set up initial things, for example:

func! ScratchBuffer() abort
    if bufname() == ''
        setl buftype=nofile
        setl bufhidden=hide
        setl noswapfile
    endif
endfunc

augroup vim_start | au!
    au VimEnter * call ScratchBuffer()
augroup end

I am not sure if check for an empty buffer name is enough but a quick test I did turned out ok for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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