tmux

or how to organize your terminal sessions

tmux

Terminal Multiplexer

Multiplexer?

[...] access multiple separate login sessions inside a single terminal window, or detach and reattach sessions from a terminal.

 

It is a wrapper that allows multiple text programs to run at the same time, and provides features that allow the user to use the programs within a single interface productively.

But why?

But why?

But why?

But why?

But why?

What's wrong with that?

  • Hard to find the exact project you want inside of multiple terminal processes with same process name

 

  • Can't hide / close terminal process without killing all processes within it

 

  • Multiple long running processes needed for a project (e.g. server, console, sidekiq, weback) need to be started manually

How does TMux help?

  • Organize your projects into named sessions, windows and panes that run in the same parent process
  • Script tmux to bootstrap your projects
  • Run long-living processes in the background with sessions
  • Excellent integration with Vim, if you're using it

Terminology

  • Sessions
  • Windows <=> Tabs
  • Panes <=> Splits

Terminology

  • Prefix key - C-b (default), C-a (alternative)

 

  • Shortcut example: prefix-c

 

  • Mentally replace all occurences of 'prefix' with C-b, C-a or whatever you'd like to use as the prefix key

Session

  • A named collection of terminals under the management of tmux

 

  • Keep completely seperated working environments

 

  • Easily create new and destroy existing sessions

 

  • Processes continue to run in the background when a session is not in focus

Working with sessions

  • prefix :new -t session_name<CR> - New named session

 

  • prefix-s - List all sessions

 

  • prefix :kill-session [-t session_name]<CR> - Kill current session or target session

 

  • prefix-$ - Rename current session

Tmux Sessionist

  • Provides convenient shortcuts for managing tmux sessions

 

  • prefix-C - Create new named session

 

  • prefix-S - Jump to previous session

 

  • prefix-X - Kill attached session

 

  • And a couple of more things...

Attaching and detaching sessions

  • You can detach from a session at any time, and the sesssion continues to run in the background

 

  • prefix-d - detach session

 

  • tmux attach -t session-name

Tmux sessions and SSH

  • Doing your work in a tmux session on a remote server will ensure you don't lose it your connection drops

 

  • Multiple clients can attach to the same session, so you can do easy remote pair programming!

Windows

  • Equivalent to tabs in terminal emulators

 

  • Useful for organizing processes within the same project

Working with windows

  • prefix-c - Create new window
  • prefix-p / prefix-n - Previous and next window
  • prefix-, - Rename current window
  • prefix-& - Kill current window

Panes

  • Equivalent to splits in terminal emulators

 

  • Useful for running processes that require the same context (e.g. rails server and rails console)

Working with panes

  • prefix-% - Vertical split
  • prefix-" - Horizontal split
  • prefix-x / Ctrl-d - Kill current pane

Zoom pane

  • Expands the pane to fill the full size of your tmux window

 

  • Incredibly useful if you want to focus on something temporarily

 

  • prefix-z

Configuring tmux

  • ~/.tmux.conf
# Set prefix to C-a
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# Bind prefix-R to reload configuration
bind R source-file '~/.tmux.conf' \; display-message "Tmux reloaded!"

# Enable mouse
set-option -g -q mouse on

# Use C-h/j/k/l to switch panes
is_vim='echo "#{pane_current_command}" | grep -iqE "(^|\/)g?(view|n?vim?)(diff)?$"'
bind -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
bind -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
bind -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
bind -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"

Scripting Tmux

  • You can target sessions, windows and even panes through the terminal

 

  • Enables automation of starting up project environments

Scripting Tmux

# Caveat: guaranteed not to work...

tmux new-session -A -d -s bmx-api -n editor -c /home/marko/source/butterflymx/bmx-api/
tmux send-keys -t bmx-api 'vim' C-m
tmux new-window -n shell -t bmx-api -c /home/marko/source/butterflymx/bmx-api/
tmux new-window -n webpack -t bmx-api -c /home/marko/source/butterflymx/bmx-api/
tmux split-window -v -t bmx-api:1
tmux split-window -h -t bmx-api:1.1
tmux send-keys -t bmx-api:1.1 'rails s' C-m
tmux send-keys -t bmx-api:1.2 'rails c' C-m
tmux select-window -t bmx-api:0
tmux attach -t bmx-api

Your project bash script

Tmuxinator

  • Ruby gem that enables you to intuitively build sessions from YAML configuration files

 

  • gem install tmuxinator

Tmuxinator

name: bmx-api
root: /home/marko/source/butterflymx/api-rails4

# startup_window: 0

windows:
  - editor: vim
  - shell:
      layout: 4a73,271x70,0,0{135x70,0,0,1,135x70,136,0[135x35,136,0,3,135x34,136,36,5]}
      panes:
        -
        - rails s
        - rails c
  - webpack:

Fetching the scary layout thingy

  • Arrange your panes in the way you like them

 

  • Run prefix :list-windows<CR> to display a list of windows in the current session

 

  • Hold shift to select the layout string and copy it over to your YAML file

Plugins - TPM

  • Tmux plugin manager enables easy plugin installation and management

 

  • https://github.com/tmux-plugins/tpm

Sensible

  • Provides sensible tmux configurations

 

  • https://github.com/tmux-plugins/tmux-sensible

Sessionist

  • Adds convenient shortcuts for working with sessions

 

  • https://github.com/tmux-plugins/tmux-sessionist

Resurrect

  • Persist session between system restarts

 

  • https://github.com/tmux-plugins/tmux-resurrect

Continuum

  • Continous saving of tmux environment and automatic restore on tmux start

 

  • Depends on resurrect plugin

 

  • https://github.com/tmux-plugins/tmux-continuum

Conclusion

  • We learned ~80% of what there is to know about tmux

 

  • Leverage tmux's sessions to seperate your project workspaces, and windows and panes to organize them

Thanks!

Marko Ivanek

 

Rails Engineer @ ButterflyMX

Occasional consultant

 

markoivanek@gmail.com

Managing projects with TMux

By mivanek

Managing projects with TMux

  • 1,129