Looking for an snazzy tool for terminal management (tabs, windows, panel splits)? I challenge you to try tmux. tmux is a terminal multiplexer that gives tight-knit terminal navigation. It also empowers workflow optimizations.

If you have experience in vim, you know that it takes some time to regain efficiency. Expect a similar experience with tmux. Don’t let this scare you, the investment is well worth it.

tmux Session

Installation

MacOS

After installing brew, you can use:

brew install tmux

Debian/Ubuntu

sudo apt-get install tmux

Windows

Cygwin has an installer for tmux build-in.

Getting Started

After that, just run tmux followed by man tmux and you will be off to the races!

Here are some of my most used commands. Note that C-b is shorthand for CTRL + b:

  • C-b (: Go to the previous session
  • C-b ): Go to the next session
  • C-b L: Go to the last session
  • C-b c: create new window
  • C-b l: Move to the last window
  • C-b n: Move to the next window
  • C-b n: Move to the previous window
  • C-b 0: Select window 0
  • C-b 1: Select window 1
  • C-b 2: Select window 2
  • C-b ;: Move to the last pane
  • C-b ": split pane horizontally
  • C-b %: split pane vertically
  • C-b x: force-kill the current pane
  • C-b o: move cursor to a different pane
  • C-b C-o: rotate panes clockwise
  • C-b [: enter scroll/copy mode
  • C-b z: expand pane to full screen
  • C-b z: restore pane back to normal
  • C-b d: detach from session

tmux Scripts

When I grab my morning coffee and open up my computer, I always:

  • Create a new tmux session
  • Split panels and create windows
  • Start postgres
  • Source the environment file(s)
  • Start virtualenv, chruby, nvm, etc (in all panels)
  • Start my development server

But why should this be a manual process every day? If you are asking the same question, DRY has taught you well.

Let’s create a bash script to setup our tmux session:

#!/bin/bash
# A simple script to start a tmux session

tmux has-session -t my_cool_project
if [ $? != 0 ]
then
  tmux new-session -s my_cool_project -n 'My Cool Project - Main' -d
  tmux split-window -t my_cool_project -h
  tmux split-window -t my_cool_project -h
  tmux split-window -t my_cool_project -h
  tmux send-keys -t my_cool_project:0.0 "cd $PROJECTS/my_cool_project" C-m
  tmux send-keys -t my_cool_project:0.1 "cd $PROJECTS/my_cool_project" C-m
  tmux send-keys -t my_cool_project:0.2 "cd $PROJECTS/my_cool_project" C-m
  tmux send-keys -t my_cool_project:0.3 "cd $PROJECTS/my_cool_project" C-m

  tmux select-layout -t my_cool_project main-vertical
  tmux send-keys -t my_cool_project:0.0 "source ./venv/bin/activate" C-m
  tmux send-keys -t my_cool_project:0.0 "source .env" C-m
  tmux send-keys -t my_cool_project:0.0 "FLASK_APP=src/application.py flask run -p 5001" C-m
  tmux send-keys -t my_cool_project:0.1 "source ./venv/bin/activate" C-m
  tmux send-keys -t my_cool_project:0.1 "psql my_cool_database" C-m
  tmux send-keys -t my_cool_project:0.2 "source ./venv/bin/activate" C-m
  tmux send-keys -t my_cool_project:0.3 "source ./venv/bin/activate" C-m

  tmux new-window -t my_cool_project -n vim
  tmux send-keys -t my_cool_project "cd $PROJECTS/my_cool_project" C-m
  tmux send-keys -t my_cool_project 'vim' C-m
fi

tmux attach -t my_cool_project

And there you have it! Now you can start and switch between work-spaces lightning fast! ⚡️