Vim persistent buffer list combined with saved sessions

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]]") > ''
        normal o

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.

2 thoughts on “Vim persistent buffer list combined with saved sessions”

  1. Hi,

    unfortunately, this did not work for me. BufferGator still complained (“buffergator: only one buffer available”) after having restored the session.

    To make BufferGator happy after a session restore (performed using “:source Session.vim”), I’ve added the function below to my vimrc. The trick is to automatically call “bnext” as many times as available buffers. The snippet below also defines the command “LoadSession” while can load any saved session (ex: “LoadSession projectA.vim”). The keyboard shortcut “l” loads the default session (equivalent to LoadSession Session.vim)

    function! FuncLoadSession(name)
    exe “source ” . a:name
    let i = len(getbufinfo({‘buflisted’:1}))
    let j = 0
    while j <= 1
    let j+=1

    command! -nargs=1 LoadSession call FuncLoadSession()
    map l :call FuncLoadSession(“Session.vim”)

  2. so the angle brackets of my previous comment were removed automatically. Let’s try one more time using html character codes this time.

    The last line, which defines the keyboard shortcut “<Leader>l”, was supposed to be:

    map <Leader>l :call FuncLoadSession(“Session.vim”)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.