Vim persistent buffer list combined with saved sessions

January 08, 2015 [Tech, vim]

I like to use several saved Vim sessions to remember what files I was working on in a particular project.

I also like to have a list of buffers I have open on the left-hand side of the screen that is always visible and up-to-date.

Sometimes, the existence of a generated buffer like the list of buffers can confuse the session restore code, but I have found a way to make this work nicely using the Buffergator plugin and the built-in mksession command.

Here are the relevant snippets from my .vimrc.

I am a beginner: please comment if you see bugs or possible improvements.

" Prerequisites: install the Buffergator plugin

" Save session on exit, excluding problematic stuff like options

set sessionoptions=blank,buffers,curdir,tabpages,resize,winpos,winsize
au VimLeavePre * if v:this_session != '' | exec "mks! " . v:this_session | endif

" Open Buffergator when you press F5

nnoremap <F5> :BuffergatorOpen<CR>

" Various Buffergator options to make it persistent,
" and displayed how I like

let g:buffergator_autodismiss_on_select = 0
let g:buffergator_display_regime = "bufname"
let g:buffergator_sort_regime    = "filepath"
let g:buffergator_autoupdate = 1

" Suppress the error message about the Buffergator buffer
" when you restore a session

function! OpenBuffergatorIfItsBufferExists()
    if bufnr("[[buffergator-buffers]]") > ''
        BuffergatorOpen
        normal o
    endif
endfunction

autocmd VimEnter * call OpenBuffergatorIfItsBufferExists()

Now when I start vim with vim -S path/to/session.vim the session is restored and saved when I exit, with a persistent buffer list on the left, and no errors during the session restore.