titles in GNU screen

I love screen. (Though I am contemplating a migration to tmux, pending hackery)

But one thing I /hate/ is having to manually title windows.

Enter this hack (Currently only works with zsh afaik, but if you know the name of the related function for other shells let me know!)

This code is now dated- you can always retrieve the most uptodate version from richo’s zshrc

function preexec()
{ # {{{ Prexec hax
    # Potential TODO:
    # Set a variable for commands where we care about return status
    # if returnstatus and $! -> Add something to sTITLE
    case $1 in
            # Clear home if we just 'cd'
            if [ "$1" = "cd" ]; then
                export t_prefix=""
                dir=$(echo $1 | sed -e "s/^cd //")
                if [ -e $dir/.title ]; then
                    case $dir in
                            export pdir=$dir;;
                            export pdir=`pwd`/$dir;;
                    # XXX Should this happen for all titles?
                    dTITLE=$(cat $dir/.title | sed $sed_r 's/[\r\n]//g')
                    export t_prefix="$dTITLE: "
                if [ -n "$AUTOTAGS" -a -e $dir/.autotags ]; then
                    # TODO
                    # Store some more info about the tags, command to run and
                    # git branch, and use the stat time of the file, rather
                    # than the contents to work out timing
                    if [ $((`cat $dir/.autotags` + $TAGS_LIFETIME)) -lt `_time` ]; then
                        _time > $dir/.autotags
                        echo "Tags are mad old, regenerating."
                        b_tags $dir &|
        # Rails kludges
        "rails "*)
            work=`echo $1 | sed -e 's/^rails //'`
            case $work in
        "bundle exec"*)
            arg=`echo $1 | sed -e 's/bundle exec/BE:/'`

            return ;;

        # If we're doing it to everything, the command is more interesting than
        # the target
            arg=$(echo $1 | awk '{print $1}');;
        # Catch kill early
        "kill "*)
            arg=$(echo $1 | awk '{print $NF}');;
            return ;;
            case $rTITLE in
                    export rTITLE="$rTITLE [tmux]"
                    urxvt_t $rTITLE
            arg=$(echo $1 | sed $sed_r -e 's/^\.\/deploy.sh/deploy:/' -e 's/114\.111\.139\.//' -e 's|/var/www/||g');;

        # For Source control I want the whole line, I think...

        #TODO Parse thor queries
            arg=$(pwd | grep -o "[^/]*/[^/]*$");;

        # TODO Include host
            # hax
            #arg=$(echo $1 | grep -o "(deploy[^ ]*)");;
            arg=$(echo $1 | awk '{print $2}');;
            arg=$(echo $1 | sed $sed_r -e 's/ ?-h */:/');;

        # Webby stuffs
            arg=$(echo $1 | sed $sed_r -e 's/^(lynx|links) (http[s]?:\/\/)?(www\.)?//' -e 's/\/.*$//');;

            export reTITLE=$sTITLE
            arg=$(echo $1 | awk '{print $NF}')
            # Don't care where in the local fs we are
            export t_prefix=""
            export reTITLE=$sTITLE
            # Vim likes to play funny buggers with my terminal. Show that
            # bastage who's in charge.
            export reTITLE=$sTITLE
            # Don't bother setting a title- handles it.
            arg=$(echo $1 | awk '{print $NF}');;

    t $arg
} # }}}

The obvious first prereq is that $INSCREEN be set. I do this with

setenv INSCREEN yes

in my screenrc.

The rest uses a feature of zsh- that you can create a preexec() function which runs after a command is submitted, but before it is executed, which then interprets the command, and sets a relevant title for screen. Note that the same control characters will set the title of many terminals (urxvt for sure) but this triggers a bug in vim, at present.

I have just dumped my function verbatim, but I feel it’ll give a good starting point to achieve many tasks, as it uses a wide variety of filters to do common tasks such as a noop, complex interpolation and dropping obvious parts of the command.

About richo

I enjoy exploring interesting concepts in weird languages. I also like hacking on all of the things.
This entry was posted in Guides, Hax and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *