Do you find Test Driven Development (TDD) good in theory but hard to practice? Do you think it requires too much discipline and you don’t have time? Or, are you just struggling to get your workflow streamlined? Fighting to glue your tools together?
Well, you can improve a lot, by borrowing some tricks from me. I’ve practiced TDD with Ruby for many years now, and built an entire web framework only with these techniques.
They are simple, effective and easy to learn.
What is Tmux for?
Tmux is a terminal multiplexer. With a single session you can attach and detach several background processes.
While I use just Vim for projects that don’t need any specific setup. For instance, most of the Lotus gems are just Ruby and don’t need running services to work on them.
A different case are freelancing projects (usually web apps), that need one or more datastore (Postgres + Redis), a running server, console, logs, etc.. In this case I use Tmux to simulate an UNIX based IDE. With just one command I can start to work on a project.
One Command To Rule Them All
Tmux is scriptable, so we can configure the instructions that it has to follow at the startup. I crafted a BASH script named
code for that.
That finds the project directory, starts a Tmux session, by loading general configuration and project specific settings.
#!/bin/bash name=$1 dir="$HOME/Code/$name" cd $dir if !(tmux has-session -t "$name" 2> /dev/null); then tmux start \;\ set -g set-remain-on-exit on \;\ new-session -d -s $name fi tmux source-file "$HOME/.tmux.conf" tmux source-file "$HOME/.tmux/profiles/$name.tmuxrc" tmux attach -t $name
In the morning, if I have to work on a project named
bookshelf, with just
code bookshelf, I have my development environment up and running in a second.
At the bottom of the screen, there are numbered windows. For each window you can have one or more split screens (panes). Usually, Vim is at
1 and Lotus server and console are at
2. If I have to edit, I can switch to the first window with
Ctrl+f 1 or jump to the console with
This helps to quickly move back and forth between several tools that I need.
There are a few amazing features in that script:
- Get back to previous window and pane (
- Navigate between panes (
- Pane zoom in/out (
- Respawn dead pane for crashed processes (
- Kill current window (
- Kill Tmux session (
You can add several customisations to that script, Tmux is really powerful!
Project Specific Configuration
We can specify for each project, some specific layout for our windows.
# vim: syn=tmux neww -n vim 'vim .' neww -n shell 'exec $SHELL -l' neww -n server 'bundle exec lotus server' splitw -v -p 30 'bundle exec lotus console' setw -t server monitor-activity off neww -n db 'postgres -D /usr/local/var/postgres' splitw -v -p 80 'redis-server' setw -t db monitor-activity off neww -n sql 'bundle exec lotus db console' selectw -t vim
Let’s see what that means. I have the first two windows with full screen Vim and shell. The third one has a split between the server (70%) and the console (30%, see
-p 30 option). The remaining are for database shell, Postgres and Redis servers.
The last command tells Tmux to select the window named
vim at the boot time. Please note that window names are arbitrary.
Fast TDD Feedback
This technique is used to run RSpec examples, and let the pane to be invisible if specs are successful, and to remain open in the opposite case.
I use it, alongside with vim-greenbar, which is able to report the current TDD status (green/red) by colouring the Vim status bar.