Setting Up a Project Workspace in Tmux

As a developer, I spend a lot of time working in the terminal. Besides starting long-running daemons such as web servers in the terminal, I also use Git, Vim, and other command line tools throughout the day, and terminal sessions tend to pile up. On any given day, I can have more than a dozen terminal sessions open at a time, and that’s just for one project.

It’s tough to manage all those terminal sessions with tabs alone. More than four tabs are hard to keep track of. The more tabs that are open, the more tempting it is to conserve tabs, killing processes to run a quick command rather than opening a new tab. Rather adopt this reduce-reuse-recycle approach to managing terminal sessions, I use Tmux.

Tmux is a terminal multiplexer that lets you run several terminal sessions within one. Instead of tabs, I use Tmux to organize terminal sessions into workspaces. My typical workspace has three separate windows: a shell prompt, Vim, and daemons. The daemons window often has two or more side-by-side panes within it, since long-running processes just need their own session but not much real estate. Workspaces and windows can be named, and switching between them is done via keyboard shortcuts, which makes navigating very fast.

Getting all those workspaces, windows, and panes set up can be a chore. Fortunately, Tmux supports reading commands from a file, so workspace setup can be scripted. Here’s an example Tmux script:

rename-session api
rename-window shell
new-window -n vim
send "vim" C-m
new-window -t 9 -n daemons
send "zeus start" C-m
split-window -h
send "sleep 2 && zeus server" C-m
split-window -v
send "postgres -D /usr/local/var/postgres" C-m

From inside a new session, I can get my workspace configured by running tmux source-file /full/path/to/setup/script.

Scripting the setup of a project workspace is not only helpful for day-to-day setup but can also save a lot of time when dusting off an old project. It’s easy to forget key steps for old projects (say, starting the right database process), and scripting what it takes to set up a working environment can save a lot of time you might otherwise spend trying to remember which command you forgot or exactly how to invoke it.

Personally, I keep these scripts for initializing a Tmux session in the relevant project directory under the name .tmuxrc.

If you’d like to get started scripting your own Tmux workspace setup, you can see an overview of what commands Tmux supports by running tmux list-commands in your shell. More documentation is available in the Tmux manpages.

Conversation
  • Nick L. says:

    How did you configure the bottom bar of tmux to display like that? That is, with solid arrows?

  • Nick L. says:

    Thank you!

  • Comments are closed.