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)?

  • Have you considered making that an alias? – Herb Wolfe Mar 23 at 19:17
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.

| improve this answer | |
  • 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. – Martin Tournoij Mar 25 at 22:28
  • @MartinTournoij good points. I’ll add alternatives later. – D. Ben Knoble Mar 25 at 23:41
1

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

| improve this answer | |
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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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