What are vim sessions?

Don’t be a script kiddie: read the docs! Open vim and type

:h session

The long and short of it is this: sessions are vim’s way of saving workspace state. This means buffers, windows, tabs, registers, recent locations, etc.

Sessions are pretty cool, but they are too tedious to work with. This is why I downloaded tpope’s Obsession. This makes working with sessions a lot easier! Once invoked, the plugin saves the session every time you do something that might change it (like open a window, close a buffer, etc.). This is much nicer than the out-of-the-box functionality, but even interacting with Obsession was too much work.

Using Obsession to replicate modern IDE functionality

I use vim because I want to pick and choose functionality of modern IDEs. This allows me to get all the productivity benefits associated with such fancy pieces of software, without all the useless bloat. In this example, I want to

  • quit vim
  • restart my computer (or whatever)
  • then pick up right where I left off with a single command

I handled this with a bit of Bash.

export vim_sessions=$HOME/.config/vim

    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $(compgen -W "$(ls ${vim_sessions})" -- $cur) )

complete -F _vim_sessions vs

    # open vim and source session Session.vim or parameter1.vim

    mkdir -p ${vim_sessions}

    # use Session.vim if no param given
    if [ "$#" -ne 1 ]; then


    if [[ ! -f $SESSION_PATH ]]; then
        # make session file if it does not exist
        vim -c "Obsession $SESSION_PATH"
        # source session file if it exists
        vim -S $SESSION_PATH

What’s going on here…

  • the vim_sessions variable is an absolute path to the location where the session files are stored
  • the _vim_sessions function and the complete command after that are the bash completion magic
  • The vs function is the meat and potatoes! This opens vim with a session tracked by Obsession, making a new one if needed.

Most of the time I’m writing code in a git repository and I have a certain files open and arranged for a task as hand. Wouldn’t it be great if I could source a session for just this branch? It would, and it is! Take a look:

    # open vim and source session for curent git branch
    # used for creating / writing source code
    # if anything is given for first param
    #   session will be deleted and recreated (start fresh)

    repo=$(basename `git rev-parse --show-toplevel`)
    branch=$(git rev-parse --abbrev-ref HEAD)

    if [[ -z $branch || -z $repo  ]]; then
        return 0
    mkdir -p ${vim_sessions}/$repo

    if [[ $1 ]]; then
        rm ${vim_sessions}/$path_segment

    vs $path_segment

This command takes an argument, usually I give it - but it can be literally anything. If the argument is present, the session for that branch is deleted and created anew.

If you find these useful, head over to my dotfiles repository and check out the bash_aliases file. There might be some other stuff in there you dig.