I currently use scratch buffers a lot for various uses such as when I read from STDIN to quickly analyze some data or take some very temporary notes. That way I don't have to worry about a swap file or the quit message when I leave the buffer. However, once in a while I decide to save the file for future reference. I'm wondering how to cause the buffer to switch to that new file automatically after saving instead of staying in my scratch buffer.
When I am referring to the scratch buffer, these are the commands I'm using to turn on and off the scratch buffer settings:
command! TurnOnScratchBuffer setlocal buftype=nofile bufhidden=hide noswapfile command! TurnOffScratchBuffer setlocal buftype="" bufhidden="" swapfile
When I don't set the buffer as a scratch buffer in the first place or when I turn the scratch buffer on then off again, vim seems to already have the behavior I desire by immediately switching to the newly created file. However when I try to automate this with autocmds, then I get weird results that I believe have something to do with when various things trigger but I'm not sure. Here is an example of the current autocmds that I'm trying to use:
" Turn on scratch buffer settings when reading from STDIN or working with autocmd StdinReadPre,StdinReadPost * :TurnOnScratchBuffer autocmd VimEnter,BufNew * if @% == "" && &buftype == "" | execute ':TurnOnScratchBuffer' | endif autocmd BufWrite * if (@% == "" || @% == "-stdin-") && &buftype == "nofile" | execute ':TurnOffScratchBuffer' | endif
An as a pure example of how I would like it to work, say I run
ls | vim - then what should happen is a scratch buffer should open with the listings of the current directory in a scratch buffer. If I then save this to a file
:w test, then it will smoothly move to a buffer that is for the
test file. If I save again, it should save directly to the
test file. If I try to exit after making some changes, it should prompt me to save as you would expect when editing any normal file in vim.
The first part of that example I can get to work just fine, but the saving part is not behaving how I would like and I'm unsure why.
Let me know if there is another way of going about this too! Thanks for the help.
Finally got everything working thanks to the help of the wonderful people here. I learned so much from this little change that I can honestly say I'm better for it. I'm adding the final changes I made to simplify it for others that see this going forward.
command! -bar TurnOnScratchBuffer setlocal buftype=nofile bufhidden=hide noswapfile command! -bar TurnOffScratchBuffer setlocal buftype= bufhidden= swapfile command! -bar NewScratch new | TurnOnScratchBuffer augroup scratch_buffers autocmd! autocmd StdinReadPre * TurnOnScratchBuffer autocmd VimEnter * \ if @% == '' && &buftype == '' \ | TurnOnScratchBuffer \ | endif autocmd BufWritePost * ++nested \ if (empty(bufname()) || bufname() == '-stdin-') && &buftype == 'nofile' \ | TurnOffScratchBuffer \ | setlocal nomodified \ | edit <afile> \ | endif augroup END