Saturday, May 5, 2012

Switching from GNU Screen to tmux

GNU Screen is a fantastic program. It is, however, more or less abandoned. tmux promises to be a more full-featured alternative, with cleaner source code and easier configuration. This is the .tmux.conf file I'm currently using.

# change tmux key-bindings to screen key-bindings (with ^O as prefix)

# Set the prefix to ^O
unbind C-b
set -g prefix ^O
bind a send-prefix

# Bind appropriate commands similar to screen.
# lockscreen ^X x 
unbind ^X
bind ^X lock-server
unbind x
bind x lock-server

# screen ^C c 
unbind ^C
bind ^C new-window
unbind c
bind c new-window

# detach ^D d
unbind ^D
bind ^D detach

# displays * 
unbind *
bind * list-clients

# next ^@ ^N sp n 
unbind ^@
bind ^@ next-window
unbind ^N
bind ^N next-window
unbind " "
bind " " next-window
unbind n
bind n next-window

# title A
unbind A
bind A command-prompt "rename-window %%"

# other ^O
unbind ^A
bind ^O last-window

# prev ^H ^P p ^? 
unbind ^H
bind ^H previous-window
unbind ^P
bind ^P previous-window
unbind p
bind p previous-window
unbind BSpace
bind BSpace previous-window

# windows ^W w 
unbind ^W
bind ^W list-windows
unbind w
bind w list-windows

# quit \ 
#unbind \
#bind \ confirm-before "kill-server"

# kill K k 
unbind K
bind K confirm-before "kill-window"
unbind k
bind k confirm-before "kill-window"

# redisplay ^L l 
unbind ^L
bind ^L refresh-client
unbind l
bind l refresh-client

# split -v |
unbind |
bind | split-window

# :kB: focus up
unbind Tab
bind Tab select-pane -t:.+
unbind BTab
bind BTab select-pane -t:.-

# " windowlist -b
unbind '"'
bind '"' choose-window

# Q break-pane (close all other panes)
unbind !
bind Q break-pane

#####################################################################

# enable 256 color support
set -g default-terminal "screen-256color"

# make prefix+S toggle status line visibility
bind S set -g status

# more or less screen style split
# prefix+H: split horizontally by grabbing the previous window into the new pane
# prefix+V: split vertically by grabbing the previous window into the new pane
bind H join-pane -ht :-1
bind V join-pane -vt :-1

The bulk of the code is for making tmux key-bindings like those of GNU Screen (up until the seprator line). Most of that code is copied from the sample config file that came with the Arch package (in /usr/share/tmux/screen-keys.conf). The rest adds these functionalities:
  • Enable 256 color support.
  • Add a key-binding for toggling status line visibility.
  • Add a pair of key-bindings for screen style splitting. In screen, when you split a window, two windows are created and each can be rotated so that it contains one of the previously created windows. In tmux, splitting results in creating two "panes". A window can contain several panes. This is usually desirable (with this config, accessible through the use of prefix+| and prefix+% shortcuts). However, sometimes you might want to split the window so that the new pane contains one of the previously created windows. The last two lines add the prefix+H and prefix+V key-bindings that make this possible (one for splitting vertically, the other horizontally). The new pane will contain the window before the current one.
I also added this to my .stumpwmrc to make switchig to the emacs inside tmux easier (again, this is not exactly correct, but it works most of the time for me).

(defcommand emacs-in-tmux () ()                                                                                                                                                                
  "attempts to switch to an emacs instance run in a tmux window                                                                                                                          
called 'emacs', itself inside a urxvt instance."                                                                                                                                               
  (let ((ret                                                                                                                                                                                   
         (run-shell-command "tmux select-window -t emacs ; echo $?" t)))                                                                                                                       
    (if (eql (elt ret 0) #\0)                                                                                                                                                                  
        (run-or-raise "urxvt" '(:class "URxvt"))                                                                                                                                               
        (message "no screen session found."))))                                                                                                                                                
(define-key *root-map* (kbd "C-e") "emacs-in-tmux")

No comments:

Post a Comment

Better Console Font (in Arch)

In order to use Terminus as my console (tty) font, I simply installed the package terminus-font with pacman (includes both console and X11 ...