Skip to content

Instantly share code, notes, and snippets.

@notpeelz
Last active March 16, 2018 21:38
Show Gist options
  • Select an option

  • Save notpeelz/80b4da9b8a503c954a02af6e303a2867 to your computer and use it in GitHub Desktop.

Select an option

Save notpeelz/80b4da9b8a503c954a02af6e303a2867 to your computer and use it in GitHub Desktop.
Per-tab buffers; don't use this, doesn't work.
" Inspired by https://vi.stackexchange.com/questions/4091/how-to-bind-a-set-of-buffers-to-a-tab
if !exists("g:WindowBufManager")
let g:WindowBufManager= {}
endif
function! InitTabBufferList()
if !has_key(g:WindowBufManager, tabpagenr())
let g:WindowBufManager[tabpagenr()] = []
endif
endfunction
function! StoreBufTab()
call InitTabBufferList()
" add the new buffer to our explorer
if index(g:WindowBufManager[tabpagenr()], bufname("%")) == -1 && bufname("%") != ""
call add (g:WindowBufManager[tabpagenr()], bufname("%"))
endif
endfunction
function! WindowBufManagerNext()
call InitTabBufferList()
" find the next index of the buffer
let s = index(g:WindowBufManager[tabpagenr()], bufname("%"))
if (s != -1)
let s = (s + 1) % len(g:WindowBufManager[tabpagenr()])
execute 'b ' . get(g:WindowBufManager[tabpagenr()], s)
endif
endfunction
function! WindowBufManagerPrevious()
call InitTabBufferList()
" find the previous index of the buffer
let s = index(g:WindowBufManager[tabpagenr()], bufname("%"))
if (s != -1)
if (s == 0)
let s = len(g:WindowBufManager[tabpagenr()]) - 1
else
let s = s - 1
endif
execute 'b ' . get(g:WindowBufManager[tabpagenr()], s)
endif
endfunction
function! WindowBufManagerCloseTab()
call InitTabBufferList()
let bname = bufname("%")
let s = index(g:WindowBufManager[tabpagenr()], bname)
if (s != -1)
let total = len(g:WindowBufManager[tabpagenr()])
" if we don't switch to another buffer prior to unloading the buffer,
" it'll close the tab
if (s > 0)
execute 'b ' . bufname(get(g:WindowBufManager[tabpagenr()], s - 1))
elseif (s < total)
execute 'b ' . bufname(get(g:WindowBufManager[tabpagenr()], s + 1))
endif
call remove (g:WindowBufManager[tabpagenr()], bname)
execute 'bd ' . bname
else
execute 'tabclose'
endif
endfunction
augroup WindowBufManagerGroup
autocmd! BufEnter * call StoreBufTab()
augroup END
nnoremap <C-r> :call WindowBufManagerPrevious()<CR>
nnoremap <C-e> :call WindowBufManagerNext()<CR>
nnoremap td :call WindowBufManagerCloseTab()<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment