Use Quick Look from Terminal

As we’ve discussed here in a number of places, Quick Look is one of the more compelling new features in OS X 10.5—no longer do you have to open an application just to glance at a file; a press of the Space Bar in the Finder is all it takes.

But you may be surprised to find that you can use Quick Look when working in Terminal, a decidedly non-GUI application. This feature can be useful if you’re working in Terminal and want to be sure a certain file is what you think it is before you do something to it. Instead of switching out to the Finder, or using the open command, you can Quick Look the file directly from Terminal.

How, you might be wondering? The key is a new Unix program, qlmanage, included with OS X 10.5. This program was really designed for programmers writing Quick Look generators, but we can use it for our own purposes as well. To see the Quick Look preview for any file, just type qlmanage -p somefile, where somefile is the full name of a file in the current directory. As an example, here’s how you could use Quick Look from Terminal to examine one of the Apple-provided desktop images. The two commands below first change to Apple’s Desktop Pictures folder, then ask the system to display the Quick Look preview for one of the images:

$ cd /Library/Desktop\ Pictures
$ qlmanage -p "Jaguar Aqua Graphite.jpg"

After you see quite a bit of text go flowing past in Terminal, a Quick Look window will open, displaying the Jaguar Aqua Graphite desktop picture. It’s important to note that this is not the Finder’s Quick Look window—in fact, if you switch out to the Finder with the Terminal’s Quick Look window still showing, you’ll see that you can open new Quick Look windows in the Finder. To close the Terminal’s Quick Look window, you can either use the mouse and click the preview window’s close box, or (much simpler) just press Command-C to end the qlmanage program.

But what about all that text that goes flowing by? That’s there to help developers debug their Quick Look code. To you and I, though, it’s completely extraneous. To get rid of it, you can redirect the output from the Terminal’s screen to /dev/null, which is a special Unix device that simply discards anything it receives—so it will receive all of the text output from qlmanage, and then just ignore them. The end result is that we don’t have to see all that ugly text. Using the example from above, this version of the command will open the Quick Look window while skipping all the needless text output (press Command-C to close the Quick Look window):

qlmanage -p "Jaguar Aqua Blue.jpg" >& /dev/null

If you think you’re going to use this command a lot, you can make it much simpler by creating an alias in your account’s .profile file. To do that, make sure you’re in your user’s home folder by typing cd and pressing Return. If you already have a .profile file (or other such run-at-session-start file), then you can skip down to the alias command below and just insert it into your existing file. If you don’t have a .profile file, here’s how to create and edit one in one step:

nano .profile

This will open the nano text editor on a new file in your home folder named .profile. All you need to do is copy and paste this alias command into the editor’s window:

alias qlf='qlmanage -p "$@" >& /dev/null'

You can change qlf to whatever you like—this is what you’ll type to open a Quick Look preview window in Terminal. I chose qlf (for Quick Look File) because it’s short to type and easy to remember. The rest of the command is basically identical to the “no text” version from above, with one key change. The "$@" bit will be replaced by the name of the file you’ll provide with the qlf command. Once you have the command entered into the nano text editor, press Control-X (for Exit), then press Y and Return (to save your changes and exit the editor).

Open a new Terminal window, and try out your new alias:

$ cd /Library/Desktop\ Pictures
$ qlf "Jaguar Aqua Graphite.jpg"

If your alias doesn’t work, use nano again to edit your .profile file and compare what you’ve entered with the alias command above.

Subscribe to the MacWeek Newsletter

Comments