vim sessions, python, and git branches

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
  print "saved session to", branch_session_file


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 Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: