Friday, December 12, 2008

Value Under Cursor #2

Expanding on a previous tip. Here are some methods for figuring out the value of a given character.

1) Using gf - place the character under a character and hit ga in normal mode to obtain it's ascii value.

2) Using g8 - same as above but applies to UTF-8 characters.

3) :set list - toggles displaying unprintable characters.

4) pipe into the shell command od -c

5) type the :ascii ex command to display a characters ascii value

6) use cat -vET filename

Setting the Filetype

Vim normally sets the current filetype automatically and very accurately at that. In some cases though, such as when you're starting a file from scratch, it doesn't know the filetype. In these cases, you can set the filetype manually.

:set filetype=perl
:set filetype=php
:set filetype=python

etc...

Thursday, December 11, 2008

Ubuntu: Downgrading X.org

First things first, if you use multiple monitors and you're thinking about upgrading to Ubuntu 8.10 (Intrepid), I would recommend holding off. A good number of users, myself included, have experienced a very annoying bug where the mouse spontaneously loses it's ability to click. The only way to regain mouse support is to kill X with ctrl-alt-backspace (ugh). I've been able to faithfully reproduce the bug by doing the following:

* Enable Xinerama in xorg.conf
* Open firefox fullscreen on both monitors
* Rapidly move the mouse between monitors

It's worth noting that this bug has nothing to do with what window manager you're using. I've experienced it under Gnome, AwesomeWM, and Xmonad. The bad news is that even though a lot of people are complaining about it, the bug has not been fixed. As of right now, the only reliable method to regain workstation stability seems to be downgrading X.org back to the Hardy packages. To assist anyone who might be Google'ing this issue, here are the instructions for doing so.

Log into a text console as root and shut down X with '/etc/init.d/gdm stop'.

Add the following to your /etc/apt/sources.list:

deb http://us.archive.ubuntu.com/ubuntu hardy main
deb http://us.archive.ubuntu.com/ubuntu hardy-updates main
deb http://us.archive.ubuntu.com/ubuntu hardy-security main

Run apt-get update.

Purge all the packages you can by doing the following:

cd /var/lib/dpkg/info/
dpkg --purge `ls *xorg*.list | sed s/.list// `

Some of these will fail to purge due to dependency issues. That's OK. Continue with the instructions.

Reinstall the packages from the Hardy repo:

apt-get --reinstall install x11-common/hardy xorg/hardy xserver-xorg/hardy xserver-xorg-core/hardy xserver-xorg-input-kbd/hardy xserver-xorg-input-mouse/hardy

Note that if you're using a stock Ubuntu video driver such as the Matrox driver you will need to include it in the list as well (xserver-xorg-video-mga/hardy). I use the third-party Nvidia driver, so it wasn't necessary to include it in the list.

After this, you can start X with "/etc/init.d/gdm start" and hopefully login to a working desktop. The final step is to go into synaptic and lock the reinstalled packages to avoid the upgrade manager from nagging.

Thanks to Nelson for providing these instructions, which I've modified slightly for presentation here.

Friday, December 5, 2008

PHP: Invoking a method with Map

Ever since I learned the basics of functional programming, I've been a fan of the map function. In the right context, it can be an elegant solution for array transformation. Of course, like anything, it can be misused. Using PHP's map, it's non-obvious how to make it invoke an instance method rather than a function. The following example illustrates how this can be accomplished.

class M
{
function double($a) {
return $a * 2;
}
}

$m = new M;
$list = array(1, 2, 3);
$list = array_map(array($m, 'double'), $list);
print_r($list);

Array
(
[0] => 2
[1] => 4
[2] => 6
)

Tuesday, December 2, 2008

Find Dot Files

Sometimes I forget the name of a particular config file but know it's stored as a dot file in my home directory. The following one-liner will non-recursively display any regular file begining with a dot.

find . -maxdepth 1 -type f -name '.*'

Find does it again. ;-)

Right = Left

Here's a tip that's mostly for fun. Open up Vim and type :set rightleft.

This changes display orientation to right-to-left. This can be useful for editing Hebrew and Arabic or just for freaking people out.