rxvt-unicode on Os X

Warning This article is old and probably outdated. I’ve since moved to iTerm2, and I now use homebrew instead of MacPorts.

Introduction

I’ve done most things using CLIs: Commodore 64 don’t have GUIs or mouses (well.. there was GeOS ), AmigaOS is quite UNIX+X11-like, and I can’t live with Windows without Cygwin installed.

When I bought my MacBook, one of the first things I did was to launch the terminal. My last meal crawling its way back through my throat forced me to close it fast!

Ok... it’s not that bad, but it doesn’t seem to allow turning off that annoying bold and use 16 colors instead. The option "Use Option as Meta Key" is nice, but at the same time a pain... how am I supposed to type @, \, {, [, … without a normal “alt” key? (BTW I’m mostly using a “French Canada” keymap) On the other hand, I’m not used to type ESC followed by left-arrow or w or … I want my meta key!

Then there’s iTerm. It can turn off bold and use 16 colors. Not bad.. but I can’t seem to get a working meta key.

So my quest is over... I want rxvt-unicode back! It supports everything and have the greatest UTF-8 support I’ve ever seen: Hit Control-Shift and 2022 and you get •, hit Control-Shift and click on the bullet, and you get an overlay saying "2022".

After being asked by a few friends about how to get rxvt-unicode working under Os X, I decided to dump this in my newly born blog.

Thus, in this article, I’m going to describe (tersely and yet verbosely enough, I hope) how I launch various X11 or shell applications like gnuplot, urxvt, ssh to server <insert_server_name_here> or gucharmap using QuickSilver.

Be warned that I’m assuming at least some knowledge on how to use a terminal, what is a shell, and such.

Basic installation

X-Windows & UNIX shell utilities

First you need to get a working X-Windows system. I suggest installing MacPorts and as such am redirecting you, my faithful reader, to its installation page.

At this point, you should have a working X11 and compiling suite.

Once this is done, I strongly suggest you upgrade to the latest XQuartz. There are several important bug fixes, including working multi-screen support. The right ALT key is also set to Mode_Switch by default - no more xmodmap-ing in my xinitrc!). (I wish there was an “auto copy x11 selection to os x” feature like iTerm supports though)

Installing rxvt-unicode & friends

$ sudo port install rxvt-unicode

I also suggest installing an up-to-date bash through MacPorts:

$ sudo port install bash

This is not required. If you don’t, you’re going to have to replace “/opt/local/bin/bash” in the examples with “/bin/bash”, to use Apple’s bash.

I also strongly suggest installing “CocoaDialog” as a few script presented here require it to show errors in… a Cocoa Dialog :P

As a side note, you might want to install findutils and fileutils. By default, this will give you GNU’s utilities prefixed with “g” (for example “gfind” or “gls”).

Also, if you want a nice UTF-8 shell editor, there’s Jed in MacPorts. Sadly it’s too old and doesn’t have UTF-8 support. I suggest getting the latest Jed and Slang sources from Jed’s website and compile them yourself.

QuickSilver

As I hate clicking forever to get to the right folder, then find the right application, then click it, I use QuickSilver to launch everything. I strongly suggest you install (and use) it.

For example, if I want to open a UTF-8 rxvt-unicode window running ssh to a server named “popo.greyworld.net”, all I need to do is type Control-Alt-Space, followed by “s-po”, then by the Enter key. Quite fast… no fiddling with the mouse or anything.

Of course if I need to launch DxO Optics, I simply do the same.. Control-Option-Space, “dx”, enter.

Anyway… QuickSilver is real efficient at launching stuff!

Getting rxvt-unicode to look like a terminal

To me, a terminal has a black background and ANSI-like colors, quite similar to a Linux console in fact. I never could get myself to survive more that a few minutes using other colors, especially with a white background.

By default, rxvt-unicode has awful colors with an obnoxious whitish background (obviously this is an opinion!). To get something more linuxconsolish, you can create a file ~/.Xdefaults (or add to it if you already have one) and fill it with the contents of this [sample .Xdefaults][].

These new defaults should take effect at the next launch of a rxvt-unicode window. It it doesn’t, you can quit X11 and restart it, or use this command:

````console $ xrdb –merge ~/.Xdefaults

You can find a full list of the things you can place here by accessing rxvt-unicode’s manpage (e.g. `“man urxvt”`).  Most resources can also be modified using various command arguments.

Be warned that rxvt-unicode in MacPorts is compiled without “afterimage” support, and as such doesn’t support images in the background.  Getting fake transparency in Os X is also a pain anyway, but by reading the man page and recompiling with afterimage, you should get there.



# Launching X11 stuff from QuickSilver

The way I do this is to create a directory where I put my “launchers”, and setup QuickSilver to scan that directory.

Lets suppose that directory is `~/Stuff/quicksilver`.  First, you need to add this path to QuickSilver in the Custom tab.  You might not need this as your home directory is probably already scanned by QS.

Now if you go into that directory and type something like:

```console
$ ln –s /usr/X11/bin/xclock

As soon as QS rescans the directory (or if you force a rescan from the config panel), you can start xclock directly from QS’s command window.

You might notice however that QS seems to be jammed until you quit xclock. This is because QS doesn’t launch things in background. To solve this, you could check the "Run tasks in background" entry in QS’ preferences. For the following method however, this is not required.

My goal is to directly open shells to various servers, or start more complex commands. To achieve this, I use a bash script similar to cmd.sh and place it in ~/bin. (Don’t forget to chmod a+x it!)

When executed, this BASH script takes the name that used to call it, looks up in a case statement, and executes the appropriate commands.

For example, if you go to your ~/Stuff/quicksilver directory and type:

$ ln ~/bin/cmd.sh gnuplot

You can now type your QuickSilver sequence, for example Control-Options-Space, then type gnupl and hit enter. This should open up an urxvt window, with gnuplot in it (assuming you installed it through MacPorts or by yourself).

Note that I used a hard link, not a symbolic one! This is because using a symbolic link is going to make QuickSilver call the shell script by its true name, cmd.sh, while a hard link will make it appear as if the true name is gnuplot. To determine what to execute, that script uses the “true name”.

Before continuing, lets take a look at locales & UTF-8.

Locales & Applications

Applications, in a unix-like system, generally take their language and character settings from the “locale settings”. Essentially this means setting LC_ALL or LANG environment variables prior to the launching of the application. (See man locale)

For example, to start jed in UTF-8 mode if your environment is something else, and assuming you have the latest Slang2 and Jed alpha, you could type:

$ LANG=en_CA.UTF-8 jed

Of course doing this will mess things up in jed: if your terminal is setup to handle things as ISO-8859-1, typing “é” will send a one-byte 0xe9 character, which is invalid UTF-8 and will appear as <E9>.

To change your environment from anything to UTF-8, you could type something like this:

$ export LANG=en_CA.UTF-8

If you live in the US, you might want to use en_US instead of en_CA (Canada). If you want things [supporting French locales] in French and have French Canadian date and time, you should use “fr_CA”.

Well… that’s nice, but you also need to notify the terminal about the change. Using gnome-terminal, konsole, putty or another similar terminal, you need to go in the menu and select the appropriate options.

With rxvt-unicode, you can use an ANSI-like escape sequence to do the trick. For example, to apply a newly changed LANG environment variable to urxvt:

$ printf '\e]701;%s\007' $LANG

Please note that if you are connected to a remote server, setting LANG there to “en_CA.UTF-8” for example requires that locale to be defined there. If you export it to rxvt-unicode, which runs locally, you need to have that locale defined locally!

You can wrap this in nice functions like you can find in my urxvt_stuff.sh. Using this, I simply type “tiso” or “tutf” to switch between ISO-8859-1 and UTF-8, and change the terminal’s title so I can quickly know which is which.

I personally setup urxvt_stuff.sh to be included either system-wide through /etc/profile.d/* on my linux servers and desktop, or in .bashrc in my home directory on the MacBook, which looks like this:

$ PS1='[\u@\h \W]\$ '
$ shopt -s checkwinsize

$ . ~/bin/urxvt_stuff.sh

$ alias ls='gls -F --color=auto'
$ alias eclean="gfind . -maxdepth 1 -type f \( -name '*~' -or -name '#*' \) -print0 \
  | gxargs -0 -n 50 rm -vf"
$ alias ecleanr="gfind . -type f \( -name '*~' -or -name '#*' \) -print0 \
  | gxargs -0 -n 50 rm -vf"

Launching Stuff Continued

Now that we have some idea of how to change a locale (and thus a charset or a language), let’s continue.

In cmd.sh, you can find “s-popo”, which stands for “shell popo”. Thus, if you go to ~/Stuff/quicksilver, and type ln ~/bin/cmd.sh s-popo, you can now start QuickSilver and type “s-po” and hit enter. This will launch a rxvt-unicode window and open up a connection to you server “popo.somewhere.com”.

Assuming you’ve setup urxvt_stuff.sh on that server, you should be able to type tiso or tutf to switch between modes, and have your window title updated accordingly.

Afterword

I hope my instructions are clear and that my method appeals to you. If it doesn’t, I certainly won’t mind, “to each his own”! I do like to have a “real” terminal however, capable of handling everything, with blackish-linux-console like colors, with both ALT and META keys, and having true Unicode support. And I do like to be able to start one without having to move my hands from the keyboard.

Sample files

cmd.sh

Download: cmd.sh

#! /opt/local/bin/bash

. /etc/profile

export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export PATH=$PATH:/usr/X11R6/bin
export MANPATH=$MANPATH:/opt/local/man/

cd ~

function dumperror()
{
  /Applications/MacPorts/CocoaDialog.app/Contents/MacOS/CocoaDialog \
     ok-msgbox \
     --text "$1"\
     --no-cancel\
     --icon hazard\
     --float\
     --timeout 1
}

urxvt=/opt/local/bin/urxvt

cmd=$(basename "$0")

{

case "$cmd" in
  s-sad)
    LANG=en_CA.UTF-8
    exec $urxvt -bg '#330000' -n 'sad' -e ssh -p 22022 afbd@sad.somewhere.com.
    ;;
  s-popo)
    LANG=en_CA.UTF-8
    exec $urxvt -n 'popo' -e ssh -p 11022 afbd@popo.somewhere.com.
    ;;
  urxvt-window)
    LANG=en_CA.UTF-8
    exec $urxvt -n 'bongo'
    ;;
  gnuplot)
    exec $urxvt -n 'GNUPlot' -e gnuplot
    ;;
  gucharmap)
    exec /opt/local/bin/gucharmap*
    ;;
  *)
    dumperror "Unknown command: $cmd $*"
    ;;
esac

} >/dev/null 2>&1 </dev/null &

urxvt_stuff.sh

Download: urxvt_stuff.sh

#! /bin/bash

# Terminal / UTF-8 setup
function tutf
{
  export LANG=en_CA.UTF-8
  if [ "$PS1" ]; then
     if [ "$TERM" == "rxvt-unicode" ]; then
       printf '\e]701;%s\007' $LANG
     fi
     export PROMPT_COMMAND='echo -ne "\033]0;u8:${USER}@${HOSTNAME}: ${PWD}\007"'
  fi
}

function tiso
{
  export LANG=en_CA.ISO-8859-1
  if [ "$PS1" ]; then
    if [ "$TERM" == "rxvt-unicode" ]; then
      printf '\e]701;%s\007' $LANG
    fi
    export PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
  fi
}

if [ -z "$LANG" ]; then
  # Set default LANG
  if [ "$TERM" == "rxvt-unicode" ]; then
    export LANG=en_CA.UTF-8
  else
    export LANG=en_CA.ISO-8859-1
  fi
fi

# Fix "utf8"
if [[ "$LANG" =~ "utf8" ]]; then
  export LANG=en_CA.UTF-8
fi

# Setup prompt, title and such
if [[ "$LANG" =~ "UTF-8" ]]; then
  tutf
else
  tiso
fi

.Xdefaults

Download: .Xdefaults (as Xdefaults.txt)

urxvt.cursorColor: green
urxvt.scrollBar_right: True
urxvt.foreground: #B2B2B2
urxvt.background: #000000
urxvt.color0: #000000
urxvt.color1: #B21818
urxvt.color2: #18B218
urxvt.color3: #B26818
urxvt.color4: #1818B2
urxvt.color5: #B218B2
urxvt.color6: #18B2B2
urxvt.color7: #B2B2B2
urxvt.colorBD: #FFFFFF
urxvt.color8: #686868
urxvt.color9: #FF5454
urxvt.color10: #54FF54
urxvt.color11: #FFFF54
urxvt.color12: #5454FF
urxvt.color13: #FF54FF
urxvt.color14: #54FFFF
urxvt.color15: #FFFFFF
urxvt.saveLines: 4096
urxvt.font: -misc-fixed-medium-r-normal--13-120-75-75-c-70-iso10646-1
urxvt.boldFont: -misc-fixed-medium-r-normal--13-120-75-75-c-70-iso10646-1
urxvt.italicFont: -misc-fixed-medium-r-normal--13-120-75-75-c-70-iso10646-1
urxvt.boldItalicFont: -misc-fixed-medium-r-normal--13-120-75-75-c-70-iso10646-1
urxvt.fading: 25%
urxvt.cursorBlink: true

Link Summary

External links