Remapping Your Macbook’s Keyboard in Ubuntu 12.04

I just installed Ubuntu 12.04 (Pangolin) on the Macbook Pro that I got from work. The fact that Apple rearranges the has a different bottom row of keys drives me nuts, though. The placement of the Command keys are where the Alt keys should be, and there is no right ctrl key. My muscle memory is trained for a standard keyboard and fighting it, especially while coding, slows me down a lot.

Luckily, there’s a utility called xmodmap that will let you remap these keys however you want. Here’s what we’re going to do:

Drop the following code into a text editor and save it somewhere. I saved it as .xmodmappings in my home directory:


!
! clean most of the modifiers
clear control
clear mod4
clear mod1

! -----------------
! left side
! ----------------
! keycode 64 is the left alt key
keycode 64 = Super_L
! key code 133 is the left command key
keycode 133 = Alt_L Meta_L

! ------------------
! right side
! -------------------
! key code 134 is the right command key
keycode 134 = Alt_R Meta_R
! keycode 108 is the right alt key
keycode 108 = Control_R

add mod4 = Super_L
add mod1 = Alt_L Meta_L
add mod1 = Alt_R Meta_R
add control = Control_L
add control = Control_R

To test this, drop into a terminal and type:
$ xmodmap ~/.xmodmappings

It should make the following changes:
Left Alt -> Left Super
Left Command -> Left Alt
Right Command -> Right Alt
Right Alt -> Right Ctrl

Now test it out. If everything looks kosher, you can set this to run every time by pulling down the gear/power menu from the taskbar, choosing “Startup Applications”, and adding a new item containing the above command.

Note that this works on a 7th-gen Macbook, and the layouts may differ slightly on other models. If it doesn’t work for you, running xev from a terminal will let you discover the keycodes for your model, which you can substitute into the above script.

Importing iTunes Ratings into Guayadeque

Guayadeque is a nice lightweight music player for linux that supports smart playlists. I wrote a little ruby script to gather all of my ratings from my iTunes database and import them into Guayadeque, so that I can use some of my advanced filters on songs. At some point I’ll probably extend it to import other metadata (last played, date added, etc), but this is a functional first stab at it.

You can download it on Github: iTunes-to-Guayadeque

Emulate say() in Ubuntu

Macs come standard with the “say” command, which translates text into speech. It’s pretty easy to do the same thing on linux, using a package called ‘festival’.

Install the app:

sudo apt-get install festival

Then try it out:

echo “Hello World!” | festival -tts

If you get an error message that says “can’t open /dev/dsp”, do the following:

sudo apt-get install esound-clients
sudo gedit /etc/festival.scm

and paste in the following lines:

(Parameter.set ‘Audio_Command “esdplay $FILE”)
(Parameter.set ‘Audio_Method ‘Audio_Command)
(Parameter.set ‘Audio_Required_Format ‘snd)

Now, save the file, and try it again.

I added a little alias, as well. Open up your ~/.bashrc and add these lines:

function say {
    echo $1 | festival --tts
}

Now the command requires much less typing:

say “Hello world!”

Have fun!

Fix css-mode indention in emacs

Here’s a quick fix for the obnoxious default indention settings for emacs. Just drop these lines into your .emacs file:

(setq cssm-indent-level 4)
(setq cssm-newline-before-closing-bracket t)
(setq cssm-indent-function #'cssm-c-style-indenter)
(setq cssm-mirror-mode nil)

Thanks to stokebloke.com for saving my sanity.

uTorrent and Ubuntu

RSS feeds plus bittorrent makes auto-downloading video content from the web easy, and keeps our library full of shows to watch through the Xbox. I struggled for a while to find a bittorrent client that was simple and had the features I wanted, though.

Azureus is a bloated mess, Transmission is far too simple, Deluge made feeds a pain in the ass . . . you get the idea. Finally, I found that uTorrent had all the features I wanted in a lightweight, easy to use client. The only problem is, it doesn’t run natively on linux. Thankfully, it runs almost perfectly through Wine, with a taskbar icon and everything.

The only problem I had was that when I clicked on a torrent in my browser, I couldn’t get it to auto-launch in uTorrent. The extra layers added by wine complicated things. I finally solved the problem by writing this little bash script:

#!/bin/bash
cd ~/.wine/drive_c/Program\ Files/uTorrent
echo ""
if [ "$1" != "" ]; then
    dt=`date +%s%N`
    cp "$1" /tmp/$dt.torrent
    var="Z:\\tmp\\"$dt".torrent"
    wine utorrent.exe "$var"
else
    wine utorrent.exe
fi

It takes the torrent file given as an argument, saves it to an accessible location, then opens it using uTorrent. Save the script, make it executable, and then set firefox so that it uses the script to open any torrent files. Problem solved.

Slow internet on Ubuntu 8.10

A week or so after updating to Intrepid on my work desktop, I started experiencing really slow internet connections. (around 100k/s). Tried a different network port – no change. Booted into windows, and got 2M/s. After perusing the ubuntu forums, I found this solution:

edit /etc/sysctl.conf and add:
net.ipv4.tcp_sack = 0
net.ipv4.tcp_window_scaling = 0

edit /etc/modprobe.d/aliases to remove:
alias net-pf-10 ipv6
and add:
alias net-pf-10 off
alias net-pf-10 off ipv6

This turns off IPv6, which seems rather heavy-handed, but disabling ipv6 in firefox alone didn’t fix the problem. I suspect that it was the result of a change somewhere in the school’s network, but dealing with their IT dept is like banging your head against a wall repeatedly. Hope this can help someone else with the same issue.

HPL Bookmarklet

Here’s a bookmarklet that searches the webpage you’re on for an ISBN number, then searches the Houston Public Library for that book. I didn’t test it on a lot of sites, but it works well enough on Amazon. To use it, just drag it to your bookmarks toolbar.

HPL Library Search

Rotating One Monitor with Ubuntu

Ubuntu’s support for graphical displays has come a really long way. In Hardy 8.04, most of the configuration is handled through a GUI that works very well, in my experience. There are still a few edge cases that require a little manual configuration, though. Today I ran into one of them.

I have two monitors at work, and today I decided to rotate one of them to a vertical orientation. It’s nice for coding, because I can see many more lines of code at once. The other one I like to keep widescreen. Here’s how to accomplish this using an Nvidia dual-headed card, the nvidia proprietary driver (not nv), and two Dell flatscreen monitors.

Assuming you already have your monitors set up and working with the nvidia driver, start by backing up your xorg.conf. If something goes wrong, you can always restore this and be back where you started.

sudo cp /etc/X11/xorg.conf etc/X11/xorg.conf.bck

Then, fire up nvidia-settings from the terminal. Under “X Server Display Configuration”, make sure that you’re using “Separate X screen” and not “Twinview”. Twinview works great when the monitors are in the same orientation, and even gives slightly better performance, but didn’t allow me to rotate just one of the monitors. Check the box that says “Enable Xinerama”, and then write the changes to your X Configuration file.

Now, for the rotation:

sudo emacs /etc/X11/xorg.conf

In the appropriate “Monitor” section, add the lines:

Option "RandRRotation" "on"
Option "Rotate" "CCW"

Change “CCW” to “CW” for clockwise, instead of counter-clockwise.

Save the file, hit CTRL-ALT-Backspace to restart your Xserver, and you should be seeing the results. Great! Well, almost great…

There’s just one problem. There’s a known bug where gnome-terminal doesn’t work properly when nvidia composite drivers are enabled. The first solution in that thread (disabling the composite) didn’t work for me, so I did the following workaround that sets some environment variables every time the terminal is launched. Hit ALT-F2 and type “xterm” (since we can’t use gnome-terminal), then do the following:

sudo mv /usr/bin/gnome-terminal /usr/bin/gnome-terminal2
sudo emacs /usr/bin/gnome-terminal

In the new file you’re now editing, paste the following:

#!/bin/bash
XLIB_SKIP_ARGB_VISUALS=1 gnome-terminal2 $@

Save the file and make it executable:

chmod +x /usr/bin/gnome-terminal

Now, everything should work correctly.

Update 10/28/08:
Due to popular demand, my xorg.conf is now posted here. It’s important to note that using Xinerama disables the composite extension, so compiz eye candy (aka advanced desktop settings) will not work. Fortunately, I value the setup for coding far more than I value shiny spinny desktop cubes.

Unique Column Count in Open Office

Sandra complained about compatibility issues while teaching an online course, and in response, I and several others recommended using Open Office instead of excel. She responded with a list of reasons why she prefers excel, which included the use of pivot tables to count unique items in a column.

To prove that this is easy to do without using pivot tables in OO Calc, I whipped up a short screencast:

It’s a little small, but you can see the details by viewing it full screen.

I used recordMyDesktop to do the screencast, which makes it dead simple.

Using vpnc to connect to a Cisco VPN on Ubuntu

First instal vpnc:
sudo apt-get install vpnc

Now, find the .pcf file that your office provides you. Open it in a text editor and use the relevant info to create file /etc/vpnc/default.conf. The contents shoudl look something like this:

IPSec gateway 123.456.789.123
IPSec ID
IPSec secret
Xauth username

There’s one hold up, though, as many companies provide their secret phrase pre-hashed. This means that it can’t be entered correctly into the configuration file for vpnc.

(your pre-hashed secret string will look something like this)

E6A01097705A303C1128DC224D959D741A708AA5A
AE8D5DAA8E982A5AC5328B76639038E03A8620395
A11C5C1732D5149FB4743293B4E50A

The solution is to hop over to this cisco vpnclient password decoder, pop in your hash, and retrieve the key. If you’re really paranoid, you can download the C source code and run it on your machine.

Finish filling out the conf file, save it, then run
sudo vpnc --natt-mode cisco-udp /etc/vpnc/default.conf

To disconnect, run
sudo vpnc-disconnect

DIY Screen Door

Houston has some nice weather this time of year. The days are warm and evening temperatures hover around 70 degrees. They’re the perfect type of evenings for opening up the windows and enjoying some fresh air.

Unfortunately, my apartment’s windows don’t open. The only way to get fresh air in is by opening the sliding glass door in my living room, and there’s no screen door on it. So for the last six months, I’ve had to choose between stale air and higher A/C bills, or opening the door and letting all sorts of bugs in.

This weekend, I had had enough, and decided that I could build a screen door myself. After some quick measurements and mental calculations, I headed out to Home Depot and rounded up my supplies:

  • 25 feet of half-inch PVC pipe
  • A roll of fiberglass screen
  • 4 ninety degree PVC connectors
  • 2 ‘T’ PVC connectors
  • 1 small roll of foam rubber weather stripping
  • Total Cost: About 20 bucks.

Assembly was pretty simple. I cut up the PVC into the right lengths, assembled the frame with the connectors, then used my staple gun to attach the screen. After making sure that it fit into the track properly, I added the weather stripping so that I could close the sliding glass door up against the screen and prevent bugs from slipping through the crack.

I think it looks pretty good:
screen from outside
screen from inside

I have to slip it into place every time I want to use it, so it’s not quite as convenient as a sliding door, but it works well. Painting the PVC black would have also made it look a little nicer, but I was going for cheap.

All in all, I’m pretty satisfied. I figure I’ll more than make my 20 bucks back by running my air conditioner less this summer. And as long as the weather holds, my apartment won’t have to smell like what I’ve been cooking for days afterward.

|