Getting back to writing code again. One thing that I always wanted was a way for my vim to understand the sets of files I was working on in a particular git branch. I think I figured this out, along with how to use vim python interface.

In .vimrc

python << ENDPYTHON
import vim
import commands
vim_sessions_dir = '~/www-vim-sessions/'
def save_git_session():
  branch = commands.getoutput('git branch 2> /dev/null | grep -e '\*' | sed 's/^..(.*)/\1/'')
  branch_session_file = vim_sessions_dir + branch + '.vim'
  session_save_cmd = ':mksession! ' + branch_session_file
  vim.command(session_save_cmd)
  print "saved session to", branch_session_file
  vim.command(':qall')
ENDPYTHON
nnoremap <leader>z :python save_git_session()

That says: here’s a function called save_git_sessions, it shells out to get the current git branch, creates a file name based on it, runs “:mksession” with that file name to dump the current state, then quits the program.

All this gets mapped to “z” using remapping.

Now on the bash side, I need a function to restore stuff from the session, which is as simple as:

function vr {
   SESSION_FILE="~/www-vim-sessions/$(git branch 2> /dev/null | grep -e '* ' | sed 's/^..(.*)/1/').vim"
   vim -S ${SESSION_FILE}
}

Et Voila!

Leave a comment

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