052 Tmux

04 Oct 2013

Tmux is a terminal multiplexer. We will explore how to create new sessions, windows and panes alone with various key bindings and configurations. Finally, we will also learn how to use Tmuxinator to create custom configurations for various projects.

Download video: mp4

Sample code: Github

Version: 1.8

Similar episodes: 022 SSH, 029 Oh My ZSH, 037 Jekyll, 044 Node

Background on Tmux

  1. Tmux main website
  2. Tmux documentation form Arch Wiki
  3. Tmux productive mouse free development, a Pragpub book by Brian Hogan
  4. Tmuxinator

Things to learn with Tmux

1. install

  1. install with a package manager - macports / brew / apt-get, etc

    $ sudo port install tmux
    
  2. know more about tmux

    $ tmux -V
    tmux 1.8
    
    $ which tmux
    /opt/local/bin/tmux
    
    $ man tmux
    

2. start session

running a simple nodejs web server - create a file server.js with the hello world example

  1. without tmux:

    • run node server.js in the command line
    • visit the browser at http://127.0.0.1:1337/ to see the 'Hello World'
    • quit the command line application/terminal
    • visit http://127.0.0.1:1337/ again to find that the server is not running
  2. with tmux:

    • run tmux
    • run node server.js in the command line
    • visit the browser at http://127.0.0.1:1337/ to see the 'Hello World'
    • quit the command line application/terminal
    • visit http://127.0.0.1:1337/ again to find that the server is still running

3. sessions

  1. start a tmux session

    tmux
    
  2. start a named tmux session

    tmux new -s [your-session-name]
    
  3. list all on-going sessions

    tmux ls
    
  4. attach a session

    tmux attach -t [your-session-name]
    
  5. kill a particular session

    tmux kill-session -t [your-session-name]
    
  6. kill all sessions

    tmux kill-server
    

4. windows

  1. to invoke Tmux commands, by default we press Ctrl+b followed by another key
  2. create new session with a new window

    tmux new -s [your-session-name] -n [your-window-name]
    tmux new -s monitor -n top
    
  3. create another window in the current session

    Ctrl+b c
    
  4. rename the current window

    Ctrl+b ,
    
  5. with Ctrl+b c and Ctrl+b , let's create the following 4 processes running in 4 windows:

    • window name is top running top
    • window name is ping running ping google.com
    • window name is node running node server.js
    • window name is errorlog (or any other status/error logging files) running tail -f /var/log/apache2/error_log
  6. move through the windows

    Ctrl+b n    // move to the next window
    Ctrl+b p    // move to the previous window
    
    Ctrl+b 0    // move to the first window
    Ctrl+b 1    // move to the second window
    Ctrl+b 2    // move to the third window
    Ctrl+b 3    // move to the fourth window
    
    Ctrl+b w    // display a menu of windows to choose from
    Ctrl+b f    // search for the window name
    

5. panes

  1. create a new session

    tmux new -s monitor
    
  2. divide vertically

    Ctrl+b %
    
  3. divide horizontally

    Ctrl+b "
    
  4. cycle through the panes

    Ctrl+b o
    
  5. cycle through some pre-defined pane arrangement

    Ctrl+b spacebar
    
  6. close current pane

    Ctrl+b x
    

6. configuration

  1. create a file touch ~/.tmux.conf
  2. some other color and char set config

    set -g pane-border-fg colour245
    set -g pane-active-border-fg colour46
    
    set-option -g status-utf8 on
    setw -g utf8 on
    
  3. unbind the prefix from Ctrl+b to Ctrl+a

    set -g prefix C-a
    unbind C-b
    bind C-a send-prefix
    
  4. setting delay between the prefix key and the command

    set -s escape-time 1
    
  5. setting the base index of windows and panes to 0 instead of 1

    set -g base-index 1
    setw -g pane-base-index 1
    
  6. reload tmux config file with a message confirmation

    bind r source-file ~/.tmux.conf \; display "Reloaded config file!"
    
  7. vertical and horizontal window split

    bind v split-window -h
    bind | split-window -h
    bind h split-window -v
    bind - split-window -v
    
  8. more obvious pane border for active and inactive status

    set -g pane-border-fg green
    set -g pane-border-bg black
    set -g pane-active-border-fg white
    set -g pane-active-border-bg yellow
    
  9. status bar

    set -g status-left "Session:#S"
    set -g status-right "#H #[fg=white,bg=default]%a%l:%M:%S %p, %d %b#[default]  "
    set -g status-justify centre
    

tmuxinator

  1. install with gem install tmuxinator

    tmuxinator --help
    
  2. create a new project and a new file ~/.tmuxinator/project-name.yml will be created

    $ tmuxinator open [project-name]
    $ tmuxinator open build-podcast
    
  3. configure the file with what commands to run in which windows and panes - the starter file is pretty good as a guideline

    name: build-podcast
    root: /Users/sayanee/Sites/build-podcast/shownotes
    windows:
      - editor:
          layout: main-horizontal
          panes:
            - #empty
            - jekyll serve --watch --config _dev_config.yml
            - grunt
    
  4. open the Tmuxinator configuration for the project

    $ tmuxinator [project-name]
    $ tmuxinator build-podcast
    
  5. to navigate through texts that has scrolled off the pane

    Ctrl+b [
    

More Resources on Tmux

  1. Tmux for solo or pair programming
  2. How We Use tmux for Remote Pair Programming
  3. Tmux Reddit links
  4. Tmux crash course by ThoughtBot
  5. Intro to Tmux by Nettuts+
  6. Tmux vs. GNU Screen
  7. Tmux a simple start
  8. Boost your productivity with Tmux

Build Link of this episode

Web Platform Daily by Šime Vidas