Saturday, October 9, 2010

Screen, Tmux and 256 colors

Text terminal application can find out terminal type connected by consulting environment variable TERM. You should make sure that your terminal (most likely terminal emulator) will set TERM variable to the correct value.

excerpt from ~/.Xresources
XTerm*termName:         xterm-256color
URxvt*termName:         rxvt-256color

In my case I set my terminals to 256 color variants. Of course you have to make sure the terminal really support 256 colors. You can verify your setup by 'tput' or 'infocmp'.

$ echo $TERM
rxvt-256color
$ tput colors
256
$ infocmp
# Reconstructed via infocmp from file: /usr/share/terminfo/r/rxvt-256color
rxvt-256color|rxvt 2.7.9 with xterm 256-colors,
 am, bce, ccc, eo, mir, msgr, xenl, xon,
 colors#256, cols#80, it#8, lines#24, pairs#32767,
...

In case of screen or tmux it's needed to assure that TERM = screen-256color. To achieve that in quite easy way put something similar to ~/.bashrc.

excerpt from ~/.bashrc
[[ ${TERM} == "screen" ]] && export TERM="screen-256color"

For the record, below is my current screen and tmux configuration.

~/.screenrc
startup_message off
vbell off
defscrollback 5000
hardstatus string "(%n: %t) - %h"
caption always "%{=}%-Lw%{=b .W}%n%f %t%{-}%+Lw %= %h"

# so that OSC escape sequence for setting window title works
termcapinfo rxvt* 'hs:ts=\E]2;:fs=\007:ds=\E]2;\007'
termcapinfo xterm* 'hs:ts=\E]2;:fs=\007:ds=\E]2;\007'
~/.tmux.conf
# change prefix key to C-a
set -g prefix C-a
unbind C-b
bind C-a send-prefix

# last active window
bind a last-window

# copy mode
setw -g mode-keys vi
setw -g mode-mouse on

# splitting
unbind %
bind | split-window -h
bind h split-window -h
unbind '"'
bind - split-window -v
bind v split-window -v

# history
set -g history-limit 1000

# window title
set -g set-titles on
set -g set-titles-string '(#S:#I.#P #W) - #T'

# status bar
set -g status-bg black
set -g status-fg white
set -g status-keys vi
set -g status-left '#[fg=green,bold]#S'
set -g status-right '#[default]#T'
set -g status-interval 0
setw -g window-status-current-attr bold

# window modes
setw -g mode-attr bold
setw -g mode-bg black
setw -g mode-fg white

# activity alert
#setw -g monitor-activity on
#bind-key / setw monitor-activity on

# clock
setw -g clock-mode-colour green
setw -g clock-mode-style 24

# other
set -g default-command /bin/bash
bind-key / command-prompt "split-window -h 'exec man %%'"