Showing posts with label configuration. Show all posts
Showing posts with label configuration. Show all posts

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")

My latest urxvt configuration

The latest changes include turning off the scrollbar (who needs a scrollbar anyway), turning off the tabs (screen and tmux do the job better) and also stopping urxvt from jumping forward when a program is producing output and I'm looking previously generated output (the last three lines).
! to match gnome-terminal "Linux console" scheme                                                                                                                                               
! foreground/background                                                                                                                                                                        
URxvt*background: #000000                                                                                                                                                                      
URxvt*foreground: #00ff00                                                                                                                                                                      
! black                                                                                                                                                                                        
URxvt.color0  : #000000                                                                                                                                                                        
URxvt.color8  : #555555                                                                                                                                                                        
! red                                                                                                                                                                                          
URxvt.color1  : #AA0000                                                                                                                                                                        
URxvt.color9  : #FF5555                                                                                                                                                                        
! green                                                                                                                                                                                        
URxvt.color2  : #00AA00                                                                                                                                                                        
URxvt.color10 : #55FF55                                                                                                                                                                        
! yellow                                                                                                                                                                                       
URxvt.color3  : #AA5500                                                                                                                                                                        
URxvt.color11 : #FFFF55                                                                                                                                                                        
! blue                                                                                                                                                                                         
URxvt.color4  : #0000AA                                                                                                                                                                        
URxvt.color12 : #5555FF                                                                                                                                                                        
! magenta                                                                                                                                                                                      
URxvt.color5  : #AA00AA                                                                                                                                                                        
URxvt.color13 : #FF55FF                                                                                                                                                                        
! cyan                                                                                                                                                                                         
URxvt.color6  : #00AAAA                                                                                                                                                                        
URxvt.color14 : #55FFFF                                                                                                                                                                        
! white                                                                                                                                                                                        
URxvt.color7  : #AAAAAA                                                                                                                                                                        
URxvt.color15 : #FFFFFF                                                                                                                                                                        
                                                                                                                                                                                               
URxvt*font: xft:Monospace:pixelsize=18                                                                                                                                                         
                                                                                                                                                                                               
URxvt*letterSpace: -1                                                                                                                                                                          
                                                                                                                                                                                               
URxvt.perl-ext-common : default,matcher                                                                                                                                                        
URxvt.urlLauncher     : firefox                                                                                                                                                                
URxvt.matcher.button  : 1                                                                                                                                                                      
                                                                                                                                                                                               
! scrollbar style - rxvt (default), plain (most compact), next, or xterm                                                                                                                       
URxvt.scrollstyle: plain                                                                                                                                                                       
URxvt.scrollBar: false     ! turn off the scroll bar                                                                                                                                           
                                                                                                                                                                                               
URxvt*secondaryWheel  : true                                                                                                                                                                   
URxvt*secondaryScreen : true                                                                                                                                                                   
                                                                                                                                                                                               
URxvt*scrollTtyOutput   : false                                                                                                                                                                
URxvt*scrollWithBuffer  : true                                                                                                                                                                 
URxvt*scrollTtyKeypress : true

My GNU Screen Configuration

First an update on my latest setup. I switched to Arch Linux a while back and I'm rather happy with it so I'm not thinking of going back to Ubuntu at the moment despite the fact that I'm hearing it's in a good condition.

Second I've started using screen a lot more than before. Specifically, I began running my emacs session inside screen a while back, so that I can connect to my home computer from other places and attach to my screen session and use the running emacs instance. More recently, I've switched to using tmux instead of screen but for the sake of keeping history, I'm posting the code and configuration I used to use here. tmux will be the subject of another post.

This is the .screenrc I used:

escape ^Oo                                                                                                                                                                                     
vbell off                                                                                                                                                                                      
                                                                                                                                                                                               
# Scrollback buffer size in lines                                                                                                                                                              
defscrollback 5000                                                                                                                                                                             
                                                                                                                                                                                               
startup_message off                                                                                                                                                                            
                                                                                                                                                                                               
hardstatus alwayslastline                                                                                                                                                                      
hardstatus string '%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'                                                       
                                                                                                                                                                                               
# enable support for 256 colors                                                                                                                                                                
term screen-256color                                                                                                                                                                           
terminfo rxvt-unicode 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'                                                                                                                                   
                                                                                                                                                                                               
# fix for residual editor text. sometimes when you run an editor and                                                                                                                           
# close it, some of its text may stay visible. this fixes that                                                                                                                                 
# problem.                                                                                                                                                                                     
altscreen on                                                                                                                                                                                   
                                                                                                                                                                                               
screen -t  emacs       0        emacs -nw                                                                                                                                                      
screen -t  shell0
It changes the screen prefix to ^O so that it doesn't clash with the emacs shortcut. It turns off the visual bell, enlarges to scrollback buffer, turns off the welcome screen, adds a status line, enables 256 color support inside screen, enables alternate screen (so that residual text from some editors will not stay visible after closing them), and finally starts a shell and an emacs instance.

I also added this code to my .stumpwmrc so that I can switch to emacs in the usual way (by pressing C-t C-e):
(defcommand emacs-in-screen () ()                                                                                                                                                              
  "attempts to switch to an emacs instance run in a gnu screen window                                                                                                                          
called 'emacs', itself inside a urxvt instance."                                                                                                                                               
  (let ((ret                                                                                                                                                                                   
         (run-shell-command "screen -X select emacs > /dev/null ; 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-screen")
For some reason this sometimes fails to switch to emacs properly, but it usually does!

And one last thing. The GNU Screen version shipped with Arch lacks vertical split support. This AUR package contains the patch for vertical splitting. Unfortunately, it lacks another patch that stops screen from complaining about long $TERM values, so I made a new AUR package that applies this other patch, too. I found that the process of creating Arch packages is surprisingly easy. In this case, I just downloaded the other package, updated its PKGFILE, and made the source tarball using makepkg --source.

Saturday, March 10, 2012

Beginning StumpWM configuration

StumWM stores its configuration in a .stumpwmrc file in the home directory. The number one thing I need in this file is Swank. Here's the code I used.
(load "/home/mostafa/slime/swank-loader.lisp")
(swank-loader:init)
(defcommand swank () ()
  (setf stumpwm:*top-level-error-action* :break)
  (swank:create-server :port 4005
                       :style swank:*communication-style*
                       :dont-close t)
  (echo-string
   (current-screen)
   "Starting swank. M-x slime-connect RET RET, then (in-package stumpwm)."))
(swank)
For some reason, this does not always start swank though. When that happens, I just hit C-t ; and run "swank." I can then connect to the Lisp image by invoking slime-connect in emacs.

I've been using this sample as the base for my own .stumpwmrc. What I've learned? First, I learned how I can declare a command that can be run interactively run through StumpWM's semi-colon prompt. The macro is defcommand. It's similar to defun, only with an additional set of "interactive" parameters which will be prompted for at run time.

I also learned that I can define my own key-bindings using define-key. Here's an example:
(define-key *root-map* (kbd "x") "firefox")
This tells StumpWM to run the command "firefox" previously declared by defcommand when I hit C-t x. C-t is StumpWM's prefix key (can be changed of course). Any key-binding added to *root-map* is should be used after pressing this prefix. If wan't to make a key-binding without the prefix key, I can add it to *top-map*. This is how the "firefox" command is defined:
;; launch web browser
(defcommand firefox () ()
  "start firefox or switch to it if already running."
  (run-or-raise "firefox" '(:class "Firefox")))
Remember that the value after :class should be Firefox with a capital F. If it's not written like that, rerunning the command will not raise the current window, but will open a new one. On Debian, :class should be Iceweasel instead of Firefox. (For a short explanation of class and other window attributes in X see here. In short, class name is, by convention, the instance name with the first letter capitalized.)

One of the most frequent things I do throughout my day is jumping to Wikipedia to look up something, or to search for it in Google. These two commands help me do that easier:
;; ask the user for a search string and search for it in Wikipedia
(defcommand wikipedia (search)
  ((:string "Search in Wikipedia for: "))
  "prompt the user for a search term and look it up in the English Wikipedia"
  (check-type search string)
  (let ((uri (format nil "http://en.wikipedia.org/wiki/Special:Search?search=~a" search)))
    (run-shell-command
     (cat "firefox \"" uri "\""))))

;; ask the user for a search string and search for it in Google
(defcommand google (search)
  ((:string "Search in Google for: "))
  "prompt the user for a search term and look it up in Google "
  (check-type search string)
  (let ((uri (format nil "http://www.google.com/search?q=~a" search)))
    (run-shell-command
     (cat "firefox \"" uri "\""))))
Finally, I need to more things. First, Network Manager seems essential. I'm going to use it's Tray icon at the moment, so since StumpWM doesn't have a tray, I'll use trayer which launches a tray in a floating window. In Debian, trayer can be installed by running apt-get install trayer. This launches trayer properly and then runs the Network Manager applet.
;; system tray
(run-shell-command
 "/usr/bin/trayer --SetDockType false --transparent true --expand false")

;; start network manager applet
(run-shell-command "nm-applet --sm-disable")
And then I need to load my own customized keymap (I swap the ctrl and alt keys and make Caps Lock an additional alt).
;; load keyboard map
(run-shell-command "xmodmap ~/.xmodmap")
That's it for now.

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 ...