Monthly Archives: October 2009

Add Color Coding to Mac OS X’s Quick Look for Source Codes

The Problem

The recent releases of Mac OS X has a wonderful feature: quick look. By selecting a file in the Finder and hit spacebar, the user can quickly view the contents of that file, be it text, audio, video, graphics, and many other file types. I often want to quick look the contents of my source codes and encountered two problems: First, As shipped, quick look does not support many types of source code, specifically Tcl. Second, the default quickly is pretty bland without any color code at all.

The Solution

My search took me to a wonderful piece of software called qlcolorcode and it did what I want. Now instead of seeing the above boring quick look, I see something like:

If you like what you see and decide to use it, head over the developer’s site and grab it. The installation is easy, so just jump right in. When you are there, please read the ReadMe for customization such as adding line numbers or changing the font.

Sweeten Bash History by Adding Grep

The Problem

While I know about the Ctrl-R key combination in bash to perform an incremental reverse search the history; I often need to grep the history to find what I want. For example, to find out what directory I changed into, I issue the following command:

$ history | grep cd

That’s a lot of typing for a lazy guy like me. Imagine that. I rather spend my time writing this blog that repeating that command.

The Solution

To solve this problem, I created a simple function and placed it in my ~/.bash_profile file:

function h() {
    if [ -z "$1" ]
    then
        history
    else
        history | grep "$@"
    fi
}

Explanation

  • Line 2-5: If the user call the command h without any parameter, the function calls the history command
  • Line 6-7: Otherwise, issue the history command and use grep to search.

Going back to my original example, the command now becomes:

$ h cd

Clearly, this is the way life should be: short and sweet. See you in another post.

How to Assign a List’s Elements to Variables

The Problem

You want to assign the contents of a list to a number of variables. For example, assigning contents of $argv to variables server, port, user, and password:

set server [lindex $argv 0]
set port [lindex $argv 1]
set user [lindex $argv 2]
set password [lindex $argv 3]

The Solution

This is what the lassign command is designed to do, see my previous post. However, prior to Tcl version 8.5, using lassign means you must load the entire TclX package, which might be expensive for one little command.

If you don’t want to require the whole TclX package just for this task, here is another alternative solution: using the foreach idiom:

foreach {server port user password} $argv break

The above is the same as using lassign:

package require Tclx
lassign $argv server port user password