screen setup with custom hard status and dialog menu

The following set of scripts produce a nice screen setup along with a dialog menu for the most common tasks.

This is how it looks like

And this is how it’s done. Please see inline comments for details.

#!/bin/bash
## use shift-left/right to switch between windows
## use ctrl-n to add new shellwindow
## use shift-PgUp/PgDown for scrolling history hit esc to stop
## use ctrl-a-k for killing a window
## mark area with space, end marking with space, paste with ctrl-a-]
setenv TERM xterm

#kill startup messes
startup_message off

# detach on hangup
autodetach on

# define a bigger scrollback, default is 100 lines
defscrollback 4096

# shell
shell -bash

#make scrollbar work
termcapinfo xterm ti@:te@

defmonitor on                   # turn monitoring on
activity "%"                    # tell me when stuff happens!

# Make shift-PgUp and shift-PgDn work like they do in xterm.  (Note that this
# requires xterm to be configured to pass those keys through, and not try to
# act on them itself.)
bindkey "^[O2A" eval "copy" "stuff ^b"
bindkey -m "^[O2A" stuff ^u
bindkey -m "^[O2B" stuff ^d

bindkey "^[O2D" prev
bindkey "^[O2C" next

# ctrl-N for new window
bindkey "^N" screen

# ctrl-a-b to copy selection to osx-clipboard
#bind b eval "writebuf" "exec sh -c 'pbcopy < /tmp/screen-exchange'"

# Window numbering starts at 1, not 0.
bind c screen 1
bind 0 select 10

# Run everything in UTF-8.
defutf8 on
# If a window goes unresponsive, don't block the whole session waiting for it.
nonblock on

# An alternative hardstatus to display a bar at the bottom listing the
# windownames and highlighting the current windowname in blue. (This is only
# enabled if there is no hardstatus setting for your terminal)
# see http://www.debian-administration.org/articles/560
hardstatus on
hardstatus alwayslastline
hardstatus string "%{.bW}%-w%{.rW}%n %t%{-}%+w %=%{..G} %H %{..Y} %m/%d %C%a "

screen -t selector sh screen-selector.sh

I use this via screen -c selector-screenrc. The helper script for initializing a screen session looks like this and would either connect to an existing one or create a new one.

#!/bin/bash

screenpid=`ps x|grep -E '(.*)SCREEN(.*)selector-screenrc\$'|awk '{print \$1}'|head -1`
if [ $screenpid -gt 0 ]; then
        screen -x $screenpid -c selector-screenrc
else
        screen -c selector-screenrc
fi

The screen-selector.sh file is a bonus and provides a dialog menu for frequently used commands. It looks like this

#!/bin/bash

while [ 1 ]; do

select=`/opt/local/bin/dialog --stdout --menu "select task" 20 60 13 \`cat tasks | tr '\040' '_' | tr '\012' '\040'\``

echo "selected $select"

if [ ! -z $select ]; then 

	echo $select

	command=`grep $select tasks|awk 'BEGIN{FS=OFS="\t"}{print \$2}'|tr -d "\""`

	echo "executing command \"$command\""

	$command
#	sleep 2
else
	exit 0
fi

done

The “tasks” file holds a simple list of tasks/commands. See the example below.

su		"screen -t rootshell sudo su -"
newshell	"screen -t shell"

Growlnotify from emacs

Post a message via Growl from emacs

(defvar growlnotify-command (executable-find "growlnotify") "The path to growlnotify")

(defun growl (title message)
  "Shows a message through the growl notification system using
 `growlnotify-command` as the program."
  (flet ((encfn (s) (encode-coding-string s (keyboard-coding-system))) )
    (let* ((process (start-process "growlnotify" nil
                                   growlnotify-command
                                   (encfn title)
                                   "-a" "Emacs"
                                   "-n" "Emacs")))
      (process-send-string process (encfn message))
      (process-send-string process "\n")
      (process-send-eof process)))
  t)

Post clipboard to your blog from emacs using org2blog

Quick helper function that posts the contents of your clipboard using org2blog.

(defun post-clipboard-to-blog ()
  (interactive)
  (org2blog/wp-new-entry)
  (insert "\n#+BEGIN_SRC\n")
  (clipboard-yank)
  (insert "\n#+END_SRC\n")
  (search-backward "#+BEGIN_SRC")
)
(global-set-key (kbd "C-x V") 'post-clipboard-to-blog)

Open current buffer in largest window

This little function opens the current buffer in the largest available window.

(defun open-in-largest-window()
  "Open current buffer in largest window"
  (interactive)
  (let ((oldbuf (current-buffer)))
  (select-window (get-largest-window))
  (switch-to-buffer oldbuf))
)
(global-set-key (kbd "C-x 5") 'open-in-largest-window)

Adjusting Emacs frame size and position based on screen size

I’m regulary jumping between my big display and the notebook screen.
This little function resizes and repositions my emacs window based on the screen size.
You will need to adjust the sizes to your need.

(defun set-frame-size-according-to-resolution ()
    (interactive)
    (if window-system
    (progn
      (if (> (x-display-pixel-width) 1500) ;; 1500 is the delimiter marging in px to consider the screen big
             (set-frame-width (selected-frame) 237) ;; on the big screen make the fram 237 columns big
             (set-frame-width (selected-frame) 177)) ;; on the small screen we use 177 columns
      (setq my-height (/ (- (x-display-pixel-height) 150) ;; cut 150 px of the screen height and use the rest as height for the frame
                               (frame-char-height)))
      (set-frame-height (selected-frame) my-height)
      (set-frame-position (selected-frame) 3 90) ;; position the frame 3 pixels left and 90 px down
  )))
  
  ;; (set-frame-size-according-to-resolution)
  (global-set-key (kbd "C-x 9") 'set-frame-size-according-to-resolution)

Just put this in your .emacs or any other startup script

lighttpd for image hosting

In order to take some work of the webserver I recently installed a lighttpd server for delivery of static files.

The following tuning did the job (thanks to the video sharing script)

/etc/lighttpd/lighttpd.conf

server.max-keep-alive-requests = 4
server.max-keep-alive-idle = 4
server.event-handler = "linux-sysepoll"
server.network-backend = "linux-sendfile"
server.max-fds = 8192
server.stat-cache-engine = "fam"

/etc/sysctl.conf

# These ensure that TIME_WAIT ports either get reused or closed fast.
net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_tw_recycle = 1
# TCP memory
net.core.rmem_max = 16777216
net.core.rmem_default = 16777216
net.core.netdev_max_backlog = 262144
net.core.somaxconn = 262144
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_orphans = 262144
net.ipv4.tcp_max_syn_backlog = 262144
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
# For Large File Hosting Servers
net.core.wmem_max = 1048576
net.ipv4.tcp_wmem = 4096 87380 524288

in the apache virtual host we just use proxy pass to forward our static urls to the lighttpd server running on port 8080

ProxyRequests Off
ProxyPreserveHost On
ProxyPass /images http://127.0.0.1:8080/images
ProxyPass /js http://127.0.0.1:8080/js
ProxyPass /css http://127.0.0.1:8080/css
ProxyPassReverse / http://127.0.0.1:8080/