Friday, July 31, 2009

Fixing Alt Arrow Key Bindings in tcsh

It took a while, but the IT department at work finally got around to upgrading the OS on my workstation to Ubuntu 8.04 LTS. It's a bit outdated when compared to my home setup (Debian/Squeeze), but familiarity goes a long way - it feels better. There were some kinks that had to be ironed out, but for the most part it was a pretty smooth transition.

One problem that irritated me was shell key bindings. I'm used to move the cursor a word at a time with <ALT> <LEFT> / <RIGHT> - and was horrified to find out that these key binding stopped working.

I contemplated the differences between my home setup and my work setup, and figured that it had to be the shell. You see, we use tcsh as the default shell at work, instead of bash - I have no idea why, but it's the standard here.

I tried the alt-arrow keys in bash and found that they work nicely. bash uses readline, so its key bindings are set in ~/.inputrc. tcsh, on the other hand, seems to be handling key bindings on its own.

With my hunch verified, all that needed to be done was add the missing key binding to my ~/.tcshrc. I already had the following:
bindkey -k up history-search-backward
bindkey -k down history-search-forward

so I guessed I could use alt-right as a key name, like this:
bindkey -k alt-right forward-word

but this only earned me an error message.

I did the sensible thing and asked a coworker, who had Ubuntu installed on his workstation before me, and he told me that he had the same problem and that there was a fix - "just add the following magic to your ~/.tcshrc":
bindkey '\e[1;3C' forward-word 
bindkey '\e[1;3D' backward-word


I did as I was told, and it didn't work. Well, not quite - it did fix the key binding under konsole, which is the terminal emulator that most of my coworkers use, but not under rxvt-unicode - the terminal emulator that I use.

So I did the other sensible thing, and searched the Net for a solution. I hit a message on the screen mailing list, which got me on the right track.

Basically all I had to do was run cat at the console, hit alt-arrow keys, record the strings that are echoed back, and use these as the key combinations to bind. Here's what I got:
bindkey '^[^[[C' forward-word
bindkey '^[^[[D' backward-word

Thank you Lazyweb.

Friday, July 24, 2009

Reordering Accounts in Thunderbird/Icedove

I have far too many email accounts. I use Icedove (the non-branded version of Thunderbird) as my mail client and am quite happy with it.

Lately, however, the email server at work was replaced and I had to setup a new email account in Icedove. Being the last to be added, the new account's folders naturally showed up last in Icedove's folders pane.

I wanted to move the new account upwards to a more visible position, so I attempted to drag and drop the new account's top level folder with the mouse.

Guess what? it doesn't work and nothing happens. What a drag. What a drop.

There seem to be two ways of doing this:
  1. the sane, yet obscure method: install the Folderpane Tools add-on and modify the ordering of the folders via its Preferences dialog.
    I only found out about this after I used the next method...
  2. Follow this procedure:
    1. close Icedove
    2. consider backing up your ~/.mozilla-thunderbird directory before going any further...
    3. open the file ~/.mozilla-thunderbird/20bir36j.default/prefs.js in a text editor (e.g. gedit) - replace the bit in red with your own profile directory name
    4. search for a line that starts with
      user_pref("mail.accountmanager.accounts",
      the rest of the line looks like this:
      "account1,account12,account3,account4");
      and it corresponds to the current ordering of accounts, with two exceptions:
      • account1 is associated with local folders (which appear last)
      • the first account that's displayed is the default account, regardless of its position in this list
    5. reorder the accounts to your liking, e.g."account1,account4,account2,account3"
    6. save the file
    7. launch Icedove, verify that the ordering of accounts is correct
    8. Enjoy the resulting brain damage.

Friday, July 17, 2009

Cygwin sshd Service Fails to Start Most of the Time

Recently I've noticed that I can't access my wife's laptop with ssh. The reason was simple - the Cygwin sshd service was not running, even though it was set to start automatically.

I went through the event log and found a relevant error event saying "Error 1067 Application quit unexpectedly". I restarted the service via the control panel and it came up with no error. The file /var/log/sshd.log was empty, so I let it go.

And after a while it happened again, with the same symptoms and resolution. I searched the Net for similar problems, but only found complaints about sshd not starting at all.

After some futzing around I managed to configure the sshd service to repeatedly attempt to start in 5 minute intervals, as long as it fails to start.

With this workaround sshd would start, usually after many (>10) attempts.

I had no idea how to further investigate the situation. So I tried out a few shots in the dark: disable the anti-virus, turn off the firewall, futz with the sshd configuration files, etc. But to no avail. I seriously considered reinstalling Cygwin from scratch, but decided to try my luck with Google, again.

Turns out that I've hit a long standing conflict between Google Desktop Search and any Cygwin service (see this thread on the Cygwin mailing list, and this thread on the Google Desktop Help discussion group).

There are two reported ways to workaround this problem: either remove/downgrade GDS or edit the registry as follows:
  1. launch regedit
  2. navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows
  3. either remove the key AppInit_DLLs, or edit its value (a string) and remove from it the GDS specific DLL (look for something like GOEC62~1.DLL). In my case it was the only DLL on the list, so I just cleared the string.

This hack fixes the sshd problem, but I guess that it may cause problems with GDS.

GDS seems to work for now, but if I find that it's broken I suppose I'll have to switch to Widnows Search as the search engine on my wife's laptop.

Friday, July 10, 2009

Switching Keyboard Layout from the Command Line

I use two keyboard layouts: us (American English) and il (Hebrew), with us being the default. I use a key combination (<SHIFT>-<CAPS>) to switch between them.

I needed a way to make sure that, when a certain application is launched, the keyboard layout is pre-selected to be il. My first attempt was to do the following with setxkbmap:
setxkbmap il ; application
I was rather amused to find that I couldn't get back to the us layout...

Here's how I do it now, that I've learned my lesson:
setxkbmap "il+us:2" ; application ; setxkbmap "us+il:2"

Friday, July 3, 2009

Sending Console Input to a Detached screen Session

The following will send console input to a specific window in a screen session (which may be detached), as if it was typed interactively:
screen -S session-id -p window-id -X stuff "command^M"

The script snippet below demonstrates this by launching a new screen session in detached mode, creating a second window in that session and then executing a different command in each window:
screen -dm
screen -X title shell0
screen -X screen -t shell1
screen -p 0 -X stuff "echo hello window 0^M"
screen -p 1 -X stuff "echo hello window 1^M"

Note that ^M stands for the newline charcter, which you can generate, in emacs or in a terminal running bash, by hitting <CTRL>-q and then <CTRL>-m or <ENTER>.