Wednesday, July 27, 2011

tmux vs. screen

I am an experienced GNU screen users for many years.  Recently, I learned to use tmux and found it is a great replacement of screen.

Tmux is originally developed in OpenBSD and was ported to many systems.

The configuration is easy with many configurable options. Here is my startup script of tmux.

#!/bin/bash                                                                                                                                                                                                                                                                            

# basic setup
TMUX=/home/me/bin/tmux.bin
export LD_LIBRARY_PATH=/home/me/lib

# session related setup
ENGWEB=/service/engweb
LIBPERL=/tools/lib/perl
HOME=/home/me


ENV=$1

if [ "$ENV" = "" ]; then
    ENV=normal
fi

$TMUX start-server
$TMUX new-session -d -s leo '/bin/bash'
$TMUX new-window -t leo:1 '/bin/bash'
$TMUX new-window -t leo:2 '/bin/bash'

case $ENV in
    normal)
        $TMUX send-keys -t leo:0 'cd; ls -lrt' C-m
    ;;
    engweb)
        $TMUX send-keys -t leo:0 "cd $ENGWEB; vim s.cgi" C-m
        $TMUX send-keys -t leo:1 "cd $LIBPERL; vim DB.pm" C-m
        $TMUX send-keys -t leo:2 "cd $ENGWEB; ls -l" C-m
    ;;
    cov)
        $TMUX send-keys -t leo:0 "cd $HOME; ls -l" C-m
        $TMUX send-keys -t leo:1 "cd $HOME/bin; ls -l" C-m
        $TMUX send-keys -t leo:2 "cd $HOME/stats; ls -l" C-m
    ;;

esac

$TMUX select-window -t leo:0
$TMUX attach-session -t leo



my .tmux.conf file

# Reload key
bind r source-file ~/.tmux.conf

# vim: foldmethod=marker

# {{{1 global options                                                                                                                                                                                                                                                                  
# redefine the prefix key
set -g prefix C-a
unbind C-b
bind a send-prefix

# }}}1

# {{{1 key bindings
bind C-o last-window

bind -r k select-pane -U
bind -r j select-pane -D
bind -r h select-pane -L
bind -r l select-pane -R

bind -r = resize-pane -U 3
bind -r - resize-pane -D 3

#bind -r C-k rotate-window -U 
#bind -r C-j rotate-window -D
bind -r C-k swap-pane -U 
bind -r C-j swap-pane -D

# bind -r C-n next-window
# bind -r C-p previous-window

bind -r y next-layout
bind o select-layout "active-only"
bind O select-layout "main-vertical"

bind '"' choose-window
bind - split-window -v
bind | split-window -h

bind q kill-pane
bind Q kill-window

bind A command-prompt "rename-window %%"

bind-key -n C-n new-window -n bash '/bin/bash'
bind-key -n C-b next-window
bind-key -n C-v previous-window


#}}}1

# {{{1 window options 
# disable automatic window renaming
setw -g automatic-rename off

# enable utf8 
setw -g utf8 on

setw -g xterm-keys on

# use vi mode in scroll mode and paste mode
setw -g mode-keys vi

# }}}1

# {{{1 status bar
# THEME
set -g status-bg white
set -g status-fg black
set -g status-interval 60
set -g status-left-length 30
set -g status-left '#[fg=blue](#S) #(whoami)@#H#[default]'
set -g status-right '%a %m/%d %H:%M'
set-window-option -g window-status-current-bg yellow

# }}}1

set -g default-terminal "xterm-256color"
set -g history-limit 1000

# Set window notifications
setw -g monitor-activity on
set -g visual-activity on

# Automatically set window title
setw -g automatic-rename