Stupid Computer Tricks

There are inevitably things about your computer that you wish worked differently. Broadly, these problems divide into two classes:

  1. It's possible, you just don't know how
  2. It's not possible

Problems in class 1 can be extremely frustrating because you don't necessarily know where you should look to find the solution. Once you do know where to look, the solution is trivial, and the nugget of hard-earned knowledge you've gained is priceless. I'm probably screaming into the wilderness, but I'm posting my golden nuggets of wisdom here in case someone has the same problem and manages to find this page.

Problems in class 2 are usually an illusion. In principle you can program a computer to do anything, so these problems are often just "The developer made a choice I don't like." One of the nice things about open source software is that you can actually dig in to the code and change things. Often the change you want is too much work, but if it's just a nagging detail you can often fix it without an enormous amount of effort. A few small patches are included below.

I'm also including links to particularly useful utilities. This is not meant to be an index of every neat hack that does something cool for someone, but rather those few things that I use every day and impact my life in a positive way.

Some of these are neat, some will make your life better or easier, and some only serve to demonstrate how anal I can be.

Here are additional sources of interesting tidbits:

GNU/Linux

Hardware Monitor

I like to know when my computer is up to something. This is often because I've started something running and want to do something else until it finishes, but I don't want to constantly check whether or not it's finished. I used to embed Xload in my Gnome panel, but sadly Gnome 2 doesn't let you do this anymore. One of the few instances where software became less useful over time.

There are many hardware monitors out there. Most let you plot the percentage of cycles used by your CPU, but not the system load. This is basically worthless because the CPU usage goes up to 100 percent and then gives you no further information. What if I have 10 CPU intensive processes running? It looks the same as if I had only one. The old-time Unix developers crafted a simple, elegant program to serve this purpose: Xload. Amazingly, people have utterly failed to appreciate Xload and proceeded to reinvent inferior wheels to solve this problem.

That said, my current favorite is Hardware Monitor It's small, clean, visually appealing, and it let's you plot system load.

One problem is that it uses the C++ Gnome libraries, which aren't standard on Red Hat 9 systems, so you need to install it via Apt for RPM to resolve the billion dependencies.

Also, I don't like the way Hardware Monitor does the axis scaling when viewing the system load, so here's a patch to fix it. I sent the patch to the author of Hardware Monitor and he said he'll integrate it shortly.

Close buttons

Why, oh why, did Gates and company decide to put the "close" button right next to the "minimize" and "maximize" buttons in Windows 95? All modern interfaces have followed suit, in spite of the fact that this is probably the worst user interface design decision in modern history. They should be in opposite corners of the window. I used to make sure that I reconfigured my window manager so that this was the case, but over the years they've beaten the resistance out of me. I'm swimming against the tide and I've decided to just live with it, in spite of its obvious ridiculousness. If you have a nice fix for this (that can also be used under OSX? Yeah, right!), let me know.

Update 13 Nov 03 Kudos to Scott Seagroves for taking up the challenge and finding out how to fix this. Under Red Hat 9, run gconf-editor and look in apps/metacity/general. There's a key called "button layout" which you can use to rearrange the buttons in the window title bar.

I still don't have a solution for OSX, but I don't have much hope on this front. I can't imagine Apple putting that much user interface control in the hands of amateurs...

Nautilus Scripts

Here's an idea that's almost correct: The Gnome file manager, Nautilus, has a slot where you can stick scripts that you want to run on selected files. Nice, now I won't have to constantly open up terminals to actually do something with the files once I find them.

Almost. I either wanted the script to be run from the current directory, or to have the full pathname of the selected files on the command line. I also wanted the standard output of the script nicely piped to a GUI window. I got none of the above.

The selected files are a newline separated list in the environment variable a NAUTILUS_SCRIPT_SELECTED_FILE_PATHS (newline separated!?! For the love of god, why???). And I had to learn Python so I could pop up a little GUI window showing the output. Here's the result. Put it in ~/.gnome2/nautilus-scripts, make it executable, and you should be able to get it by right clicking on files in Nautilus.

Update 26 Jan 04 Scott Seagroves pointed out that there are Gnome programs that pop small dialog boxes given command line arguments. Gnome 2.2 uses gdialog and Gnome 2.4 uses zenity. Both are in the gnome-utils rpm. This way you don't have to choose your scripting language based on what has a nice interface to Tk.

OSX

Newlines

The three major computing platforms managed to come up with three different conventions about newlines in text files. This is particularly annoying in OSX since different applications are following different conventions. Here's a script to convert between the three formats.

You'll find yourself in Nirvana if you use this within Emacs: You're looking at a buffer with wonky newlines. Highlight the text and type "M-x shell-command-on-region", then "rets m u" to convert from the Mac convention to the Unix convention. Ta-da! The text is now nicely formatted in a new buffer.

Making Cheap DSL Routers maintain your SSH connections

I have a cheap wireless router so that multiple computers can use our DSL connection. I want to run SSH and leave terminals open on remote machines, sometimes leaving them idle for a long time. The router wants all traffic to be like HTTP traffic (ie, mostly stateless: request, response, pause, request, response, pause...). The router was cutting connections if they were idle for more than five minutes. The router is cheap, so this delay is not configurable. I set SSH to send KEEPALIVE packets to maintain some traffic on the connection, but it didn't start sending them until fifteen minutes had gone by. SSH didn't seem to allow this time delay to be configurable. It turns out that SSH can request keepalive packets, but it's up to the networking code in the operating system kernel to decide when to start sending them and how often to send them.

So, the big solution is that you need to set a few kernel variables to set the time delays. Type "sysctl -a" to see a list of all the kernel variables and look for ones with names like "net.inet.tcp.keepintvl" and such. Change the delays (measured in seconds) with: "sysctl -w name=value "

Next you'll ask...

How do I add something to the OSX startup sequence?

Too detailed to cover here, but it's thoroughly covered in the O'Reilly book "Mac OS X for Unix Geeks."

Xload under OSX

The port of Xload to the Darwin kernel is completely brain-dead. Rather than averaging over the past few system load measurements, Xload just plots the instantaneous system load. So, when I open up my iBook and all of the idle processes are briefly woken up before deciding that they can safely snooze again, the system load shoots up to five or six. But only for a few seconds. This results in a large spike in the Xload display. Silly. Here's a patch to fix it.

Dropscript

You should know about DropScript, which wraps some OSX fluff around a shell script so that you can drop files onto the icon and have the shell script do something to them.

The syntax is a little wacky since OSX filenames can have spaces and such, so here are two examples. This one makes a bunch of files into a tarball, and this one converts a Postscript file to a PDF file and launches Acrobat to view it. Very useful if you have yet to get Ghostview working on your iBook (like me).

Emacs

Ange-ftp over ssh

Ange-ftp lets you edit remote files as if they were local, but just about everybody's switched to ssh these days. However, you can still make ange-ftp work over SSH. I'm told that Tramp is another good solution to this problem, but I haven't used it yet.

The Wiki

Also, you should know about the Emacs Wiki, which is rapidly becoming my preferred source of Emacs documentation.

Swapping Caps Lock and Control

If you make heavy use of Emacs, it's imperetive that you swap the caps lock and control keys to prevent your pinky from turning into a useless, shriveled, RSI-ridden hunk of flesh. Who put the caps-lock key right next to the "a" key, anyway? What's it doing in such a prominent spot? Do you ever use it? I don't. But I use control all the time. Clearly, time for a change.

Under Linux, this is accomplished via xmodmap. Look at the man pages. Putting the following in .Xmodmap works for me.
remove lock = Caps_Lock
remove control = Control_L
keysym Control_L = Caps_Lock
keysym Caps_Lock = Control_L
add lock = Caps_Lock
add control = Control_L

Under OSX, the situation is more complicated. I currently use uControl, but another program is DoubleCommand. John Johnson at Berkeley directed me toward a third solution which was posted to Mac OSX Hints. Or, you can get the Happy Hacking Keyboard. There seems to be no affiliation with Richard Stallman or the Free Software Foundation in spite of the fact that the keyboard is named after RMS's characteristic greeting (which can be found on various bits of Gnu gear). Oh well, I guess RMS doesn't own the phrase.

Compilation Mode

For some reason, compilation-mode stopped scrolling with the output of the compilation command. It took me a while to notice this since it's not a big deal, but I had a vague memory of better times, when I didn't have to hit "C-x o end" to see whether or not there were any errors. Fix this with: (setq compilation-scroll-output t).

Forms Mode

Use Forms mode to maintain simple flat-text databases. I use this to maintain the captions for my Photo Album, from which the html pages are generated with a Perl script.

Rectangle Mode

Want to select a column of characters in a text file, then paste the column somewhere else? Easy, use rectangle mode. Type "M-x apropos-command (ret) rectangle (ret)" to see the options, and pay particular attention to kill-rectangle and yank-rectangle.

Html Helper Mode

This may be getting out of hand since Emacs can do anything, but I also like Html Helper Mode (which is what I'm using to write this page). Available here and here, but the first seems to be more recently maintained.

GDB

First of all, you should be using GDB from within Emacs. Anything less is uncivilized. There's a menu item under "Tools" when you're editing C code that starts gdb. This will, for example, automatically whisk you to the relevant line of your source code in an Emacs buffer when you hit a breakpoint.

My major annoyances with GDB include the inability to reasonably display data structures like arrays.

There's a program called DDD which helps with this, but I never fell in love with it. At least, not until I noticed its interface to Gnuplot. That looks pretty cool.

A simple way to help with this is to just define a new GDB command to print your data structure formatted in whatever way you want. Here's how you do it.

This technique is also handy for debugging parallel programs. As long as your bug doesn't require more than a few separate processes to manifest itself, this technique works: First use Norm Matloff's parallel debugging trick and put this

int gdb_parallel_debug=1;

while (gdb_parallel_debug) {
  printf("Process %d waiting for gdb process to attach...\n", getpid());
  fflush(stdout);
  sleep(5);
}


in your code. Then put this in .gdbinit

define pattach
  attach $arg0
  finish
  set gdb_parallel_debug=0
  next
  next
end

And ta-da! You can start gdb, then just type pattach 12345 and you'll automatically find yourself at the first statement after the above while loop.

LaTeX

Printing EPS Files

I produce many plots. Postscript files print well, but aren't well suited for incorporation into larger documents. Encapsulated Postscript files are good for incorporation into larger documents, but don't print well by themselves. So, I store my plots as eps files and I wrote this script to wrap them inside a postscript file for printing. To make the plots 4 inches wide and then print them, type "printplot 4in plot1.eps plot2.eps"

AucTeX

AucTeX is a high-powered emacs mode for editing LaTeX files. I particularly like the "C-x C-e" command--you basically don't have to remember any environment formats.

Poster Printing

Sometimes I print posters for conferences. Often I want to preview them, but scaling them all the way down to 8.5x11 leaves them unreadable. I'd like to scale the poster to, say 17x22, then print it out on four standard 8.5x11 sheets that are then taped together.

Here's how to do it.

EPS and PDF files

It is occastionally necessary to convert eps files to pdf files for inclusion in LaTeX documents (for example, when using one of several toolsets to make sa presentation). The situation on this seems to be extremely confused. Bounding boxes are typically handled very poorly. Here's a script (I didn't write it) which seems to do the right thing.

Mozilla

Once you get used to mouse gestures, you won't want to be without them.

Online Books

This isn't computer related, but everyone should know about Project Gutenberg and the Online Books Page
Greg Novak<novak at ucolick dot org>
Last modified: Thu Dec 4 15:12:11 PST 2003