Practical vim: sessions and git
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
_vim_sessions(){
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "$(ls ${vim_sessions})" -- $cur) )
}
complete -F _vim_sessions vs
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
SESSION_NAME=session
else
SESSION_NAME=$1
fi
SESSION_PATH=${vim_sessions}/$SESSION_NAME
if [[ ! -f $SESSION_PATH ]]; then
# make session file if it does not exist
vim -c "Obsession $SESSION_PATH"
else
# source session file if it exists
vim -S $SESSION_PATH
fi
}
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 thecomplete
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:
vb(){
# 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
fi
mkdir -p ${vim_sessions}/$repo
path_segment=$repo/$branch.vim
if [[ $1 ]]; then
rm ${vim_sessions}/$path_segment
fi
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.