Project Workspaces with tmux and vim
2012-05-03
Sometimes I have to stop in the middle of a project to turn off my computer or just to pick up where I left off when I don’t know when I’ll be able to get back to it again. With tmux, vim and a little shell scripting, I can save my development environment and resume it using a single command.
My workspace script looks like this:
#!/bin/sh
#
# ws-kittybusiness: kitty business workspace
#
session="kittybusiness"
# cd into the project directory
cd ~/_dev/kittybusiness
# start the database
sudo /etc/rc.d/mysqld start
# set up tmux
tmux start-server
# create a new tmux session, starting vim from a saved session in the new window
tmux new-session -d -s $session -n code "vim -S ~/.vim/sessions/kittybusiness"
tmux selectp -t $session:0
# split the window into two panes by 55%, then start a second vim session in the new pane
tmux splitw -t $session:0 -h -p 55 "vim -S ~/.vim/sessions/kittybusiness2"
# create a new window, running a rails server session
tmux new-window -t $session:1 -n serve "script/server -p 9090"
# create a new window called data
tmux new-window -t $session:2 -n data
# in the new window, cd into the specified directory
tmux send-keys -t $session:2 "cd ~/_clients/kittybusiness; ls" C-m
tmux select-window -t $session:0
# connect to the tmux session
tmux attach-session -t $session
The usage of vim sessions above assumes I already had saved some previous vim work. The current vim session can be saved using the mksession
command:
:mksession ~/.vim/sessions/sessionname
And it can be restored with this:
vim -S ~/.vim/sessions/sessionname
Now by simply executing ws-kittybusiness
I instantly get:
- my current directory set to the project directory
- a running mysql server
- a tmux session with a couple of panes of previously opened files in vim
- a local development webserver
This script could be further abstracted to work with any type of project or used as a base template.