1

I would like to know if there is a command I can run in vim to tell whether a particular window that has focus is actually a Scratch buffer or an empty buffer (such as when creating a new buffer that I haven't saved to a file yet).

The background for why I want to do this:

I have a special context sensitive function bound to my Tab key in Vim, and I'd like to further improve it by allowing it to skip the scratch buffers but not the unsaved new file buffers. I know that @% can get me the file that backs the buffer, but this is empty in both cases.

My current code looks like this:

function! NextWindowOrTabOrBuffer()
    " prefer to cycle thru only the set of windows if more than one window
    if (winnr('$') == 1 && tabpagenr('$') == 1)
        " only situation where we cycle to next buffer
        bnext
    endif
    " Rest of logic is just as sound (and simple) as it ever was
    if (winnr() == winnr('$'))
        tabnext
        1wincmd w "first window
    else
        wincmd w "next window
    endif
endfunc

function! PrevWindowOrTabOrBuffer()
    if (winnr('$') == 1 && tabpagenr('$') == 1)
        " only situation where we cycle to next buffer
        bprev
    endif
    if (winnr() == 1)
        tabprev
        let winnr = winnr('$')
        exec winnr . 'wincmd w'
    else
        wincmd W
    endif
endfunc

" I actually like the mash tab to cycle windows behavior so let's keep it simple
"nnoremap <Tab> :wincmd w<CR>
"nnoremap <S-Tab> :wincmd W<CR>

" Nevermind, I actually really need this on a small screen...
nnoremap <Tab> :call NextWindowOrTabOrBuffer()<CR>
nnoremap <S-Tab> :call PrevWindowOrTabOrBuffer()<CR>
2

You should start by defining "scratch buffer" as there's no such thing in Vim. One could create a "scratch buffer" with something like:

:vnew | setlocal nobuflisted buftype=nofile bufhidden=wipe noswapfile

so you could check for the value of &buftype or &buflisted or any/all of the other options.

If that "scratch buffer" was created by some third-party plugin you may have other "hooks" at your disposal like a b:variable or whatever.

  • I found that bufhidden is set to wipe for the particular "scratch buffers" that I'm interested in. I was also thinking maybe there would be something that sets (that I could retrieve and test) the word Scratch as it does show that as the name of the window in its statusbar. But I guess I'll just have to check the source of the plugin for that. Meanwhile I'll just make my code test for wipe. – Steven Lu Dec 16 '14 at 23:35

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.