When plugging in or out the network cable (ie when moving a laptop), new X11 applications can sometimes no longer be launched. The usual way to get around that is to quit and restart the X11.app: this creates a new ~/.Xauthority file which works.
However, this means quitting running applications with open windows, which can be bothersome. Fortunately, there is another way: with a text editor (like emacs, not TextEdit.app), just edit the file ~/.Xauthority so that the hostname appearing close to the beginning of the file reflects the current hostname. The currnet hostname would have changed after plugging in or out of the network, and can be seen with the command hostname. When unplugged, the name, for instance, ends with .local.
The following perl script does this automatically. It would be nice to have it executed automatically whenever the name of the machine changes: anyone knows how to do that in a clean way (other than having a dedicated daemon running)?
#!/usr/bin/perl -w
$hostname=`hostname`;
chomp $hostname;
$xauthority=$ENV{'HOME'}.'/.Xauthority';
undef $/; # read the whole file in a variable
open IN,$xauthority;
$_=;
s/^x01x00x00x0d([^x00]+)/x01x00x00x0d$hostname/;
print "Old hostname in ~/.Xauthority was $1, replaced by $hostnamen";
rename $xauthority, "$xauthority.back";
open OUT, ">$xauthority";
print OUT;
exit;
[robg adds: I haven't tested this one...]
Mac OS X Hints
http://hints.macworld.com/article.php?story=20040426134835245