Tmux

From miki
Revision as of 21:36, 29 November 2016 by Mip (talk | contribs) (→‎Tmuxinator)
Jump to navigation Jump to search

Overview

tmux is actually a mix of screen and Gnome-Terminator. It is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. Terminals can be organised in multiple panes in the same window.

Launch with

tmux                 # Start tmux (assume command 'new')
tmux attach          # Start tmux and reattach to previous session

tmux complains if it is launched as an ssh command (error not a terminal), but this is fixed with option -t:

ssh -t server tmux attach     # Launch tmux directly, require forcing terminal emulation

Useful shortcuts:

key description
C-b "

C-b %
C-b o
C-b C-o
C-b ←|→|↑|↓

Split horizontally

Split vertically
Go to next pane
Go to previous active pane
Go to ←, →, ↑ or ↓ pane

C-b q
Display pane id briefly
C-b d
Detach from tmux. Reattach later with tmux attach

References

Byobu

Byobu is basically tmux on steroids. See Byobu page.

Tmuxinator

Install

tmuxinator is a powerful tmux session manager. It allows for saving / restoring tmux session easily.

gem install tmuxinator

Also:

  • Make sure your ~/.tmux.conf DOES NOT SET base-index or pane-base-index.
# THESE MUST BE REMOVED OR COMMENTED!

# set -g base-index 1
# set-window-option -g pane-base-index 1
# set-option -g base-index 1 
# set-option -g pane-base-index 1 
# set -g pane-base-index 1 
# set-window-option -g pane-base-index 1
  • Add to your ~/.bashrc. This gives access to TAB completion, and alias mux:
#### tmuxinator
[ -f /var/lib/gems/2.3.0/gems/tmuxinator-0.9.0/completion/tmuxinator.bash ] && source /var/lib/gems/2.3.0/gems/tmuxinator-0.9.0/completion/tmuxinator.bash

Usage

mux new PROJECT              # Create a new project named PROJECT
mux start PROJECT            # Start a project
mux start PROJECT ARG1 ARG2  # ... with arguments

Capture a layout

tmux list-windows
# 1: madhax* (6 panes) [169x41] [layout 15a8,169x41,0,0{147...
TODO
  • Byobu (how to install, interface with tmuxinator)
  • Example of project: refer to gdb

Tips

Attach automatically

One can configure tmux to attach automatically to an existing session, or create one if none exists:

  1. Add to ~/.tmux.conf
  2. new-session
    
  3. Add to ~/.bashrc
  4. # tmux - always attach (or pass given command and parameters)
    #        This solution requires to have the line "new-session" in ~/.tmux.conf
    function tmux() {
        T=/usr/bin/tmux
        if [ $# -eq 0 ]; then $T attach; else $T "$@"; fi
    }
    

Another solution (but not atomic) that does not require editing .tmux.conf is to use tmux attach || tmux new. This solutions also allows to use named sessions (see [2]).

Zoom on a pane

version 1.8 adds the zoom feature like terminator (see flag -Z to command resize-pane). Here a work-around for previous versions that binds the feature to C-b z (from [3]):

  1. Edit ~/.tmux.conf
  2. unbind z
    bind z run ". ~/.tmux/zoom"
    
  3. Edit ~/.tmux/zoom
  4. #!/bin/bash -f
    currentwindow=`tmux list-window | tr '\t' ' ' | sed -n -e '/(active)/s/^[^:]*: *\([^ ]*\) .*/\1/gp'`;
    currentpane=`tmux list-panes | sed -n -e '/(active)/s/^\([^:]*\):.*/\1/gp'`;
    panecount=`tmux list-panes | wc | sed -e 's/^ *//g' -e 's/ .*$//g'`;
    inzoom=`echo $currentwindow | sed -n -e '/^zoom/p'`;
    if [ $panecount -ne 1 ]; then
        inzoom="";
    fi
    if [ $inzoom ]; then
        lastpane=`echo $currentwindow | rev | cut -f 1 -d '@' | rev`;
        lastwindow=`echo $currentwindow | cut -f 2- -d '@' | rev | cut -f 2- -d '@' | rev`;
        tmux select-window -t $lastwindow;
        tmux select-pane -t $lastpane;
        tmux swap-pane -s $currentwindow;
        tmux kill-window -t $currentwindow;
    else
        newwindowname=zoom@$currentwindow@$currentpane;
        tmux new-window -d -n $newwindowname;
        tmux swap-pane -s $newwindowname;
        tmux select-window -t $newwindowname;
    fi
    

Scroll with mouse wheel

Add to ~/.tmux.conf:

#set-window-option -g mode-mouse on                  # Does not work when ssh from cygwin
set -g terminal-overrides 'xterm*:smcup@:rmcup@'

Start automatically with several command

Using cron, it is easy to launch tmux with one or several commands. Example of crontab line:

#Run both given script in separate window in the same tmux session. '-d' tells tmux to not attach to current terminal (which will fail in cron).
@reboot      /usr/bin/tmux new-session -d ~pi/bin/autonoekeon.sh \; new-window ~pi/bin/local/netconsole-log.sh