BRACE YOURSELF

TMUX IS COMING

TMUX

TMUX? 

  • Long running processes - When attached to a remote machine via ssh, long-running processes can be started and left running without needing to stay attached via ssh.

  • Pair Programming - Tmux can be used to allow two or more users to attach to the same session and share control providing an efficient pair programming setup.

 

  • Local Development - By enabling various layouts and communication between multiple processes, tmux can enable enhanced efficiency for local development.

TMUX AREAS

TMUX VS SCREEN

  • Customisable status bar
  • Rich featured command tool
  • Better control of sessions
  • Built-in vim- emacs- like key-binding
  • Better-documentation
  • Multiple paste buffers
  • Better community
  • Package manager

# make install

TMUX runs on OpenBSD, FreeBSD, NetBSD, Linux, OS X and Solaris
$ brew install tmux

# apt-get install tmux

Also installs:

TMUX DIFFERENT

$ brew install reattach-to-user-namespace

The solution:

From there you can add the following lines to your `.tmux.conf` file and you will be able to copy and paste:

# Use vim keybindings in copy mode
setw -g mode-keys vi

# Setup 'v' to begin selection as in Vim
bind-key -t vi-copy v begin-selection
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"

# Update default binding of `Enter` to also use copy-pipe
unbind -t vi-copy Enter
bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"

OFFTOPIC the RCM

$ brew tap thoughtbot/formulae
$ brew install rcm

MAN

OFFTOP THE CAPS LOCK

THE WORLD OF TMUX

Session - A named collection of one or more windows.


Window - A single screen within tmux, similar to tabs in terminal applications.
 At any given time, a client will be attached to a single window.


Pane - A portion of a window running a single process, e.g. Vim, zsh, man, etc.
 Panes can be oriented either vertically or horizontally and resized as needed.

TMUX Sessions

tmux new-session -s {name}
$ cat $(command -v tat)
#!/bin/sh
#
# Attach or create tmux session named the same as current directory.

path_name="$(basename "$PWD" | tr . -)"
session_name=${1-$path_name}

not_in_tmux() {
  [ -z "$TMUX" ]
}

session_exists() {
  tmux list-sessions | sed -E 's/:.*$//' | grep -q "^$session_name$"
}

create_detached_session() {
  (TMUX='' tmux new-session -Ad -s "$session_name")
}

create_if_needed_and_attach() {
  if not_in_tmux; then
    tmux new-session -As "$session_name"
  else
    if ! session_exists; then
      create_detached_session
    fi
    tmux switch-client -t "$session_name"
  fi
}

create_if_needed_and_attach

<prefix> d Detach current session

tmux detach-session

tmux list-sessions; tmux ls

 

bind-key C-h choose-tree

<prefix> <ctrl> h Interface for session selection

tmux list-command | grep session

TMUX Windows

  • set -g base-index 1 - Start window numbering at 1
  • set -g renumber-windows on - Renumber windows as they are created and destroyed to keep the window numbers consistent with the count
bind j new-window -c "#{pane_current_path}"

<prefix> {n} go to the window

bind-key b break-pane -d

tmux list-command | grep window

TMUX Panes

bind-key - split-window -v  -c '#{pane_current_path}'
bind-key \ split-window -h  -c '#{pane_current_path}'
~/.tmux.conf
# Smart pane switching with awareness of vim splits
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"
bind -n C-\ if-shell "$is_vim" "send-keys C-\\" "select-pane -l"

<prefix> o cycle through panes

<prefix> <space> cycle layouts

<prefix> x close pane

<prefix> q show pane numbers

<prefix> z Zoom pane

Explore more:

tmux list-commands | grep pane

TMUX MODES

# Use vim keybindings in copy mode
setw -g mode-keys vi

# Setup 'v' to begin selection as in Vim
bind-key -t vi-copy v begin-selection
bind-key -t vi-copy y copy-pipe "reattach-to-user-namespace pbcopy"

# Update default binding of `Enter` to also use copy-pipe
unbind -t vi-copy Enter
bind-key -t vi-copy Enter copy-pipe "reattach-to-user-namespace pbcopy"

<prefix> [ to start "copy-mode"

TMUX Recommendations

One project per session 

One Vim instance per session 

TMUX CONFIGURATION

Tmux executable

Tmux command prompt

.tmux.conf file 

  • Not persist through restarts

  • Useful for scripting

  • Not persist through restarts

  • Useful for exploring new features

  • Persisted

  • Useful

  • Key bindings

TMUX key-binding

bind-key r source-file ~/.tmux.conf \; display-message "~/.tmux.conf reloaded"

Will bind through `prefix`

-n flag let you setup binding without prefix

-r flag let you input couple commands in chain 

tmux ls | grep : | cut -d. -f1 | grep -v attached | 
awk '{print substr($1, 0, length($1)-1)}' | xargs -L 1 tmux kill-session -t

Script to kill all unnamed, detached sessions 

TMUX configs that rocks

Pair me, pair me in full

The end

tmux

By Roman Bambycha

tmux

  • 1,260