For all you Leopard users out there, here’s a handy trick to use Quick Look from the command line.
Leopard ships with a command called ‘qlmanage’. The -p option shows a preview of the file passed to the command. In the terminal, type the following:
qlmanage -p thefile
You can extend this by creating the following shell script:
#!/bin/bash
qlmanage -p $1 >& /dev/null &
The >& /dev/null prevents output from displaying, and the & runs the process in the background so a new prompt displays on the terminal.
Save this script as an executable file and store it somewhere in your PATH. I recommend naming it something short like ‘ql’.
(Note: I stored mine in a home bin folder used for custom-made scripts).
You can close the Quick Look window with the mouse (the conventional way), or close it in the terminal by getting the pid from the ps command and using kill [pid].
Using the ql command in Terminal
Closing the Quick Look window using the kill command
This feature is really handy for me because I spend a lot time in the Terminal. Many times I encounter a file such as an image, pdf, word doc, etc. that I’d like to briefly preview.
Before I discovered this, I would resort to opening Finder and navigating to the same directory to preview the file. Being able to do this all from Terminal is a real time saver!
[UPDATE]
A better solution is to use “$@” (with quotes) instead of $1 in the script. This will allow multiple arguments and wrap quotes around each one to account for spaces or other odd characters in the filename. Additionally, providing multiple arguments creates a slideshow in Quicklook.
#!/bin/bash
qlmanage -p “$@” >& /dev/null &


Shaun Haber
When in quicklook you can also just hit the escape key to close the ql window.
oh… except when using it from the command line
sorry about that.
“open .” will pop up a Finder window for the current directory. It is very handy. (In fact, the “open” command itself is fantastically useful.)
Or, just run qlmanage -p in the foreground and type ^C to kill it.
Thanks for that, it’s a great tip!
It’s worth noting that qlmanage has a couple of other options that might be useful to someone, its worth checking out the man page….
Now, is there any way to disable Quick Look altogether? Or for certain volumes? We’re having a problem with QL slowing down server accesses; you open a server volume with 100 items in it and the Finder window remains blank as QL accesses all 100 to create thumbnails. Any ideas welcomed!
@Kurt:
That functionality isn’t caused by quicklook, but by the icon preview Finder setting. Hit command-J in Finder to open View Options, and uncheck “Show icon preview”, then hit “Use as defaults” to apply it Finder-wide.
In the script, you need quotes around $1 like to make it work for files with characters like spaces and hyphens.
Actually, a better way is to replace $1 with “$@”. This will accept multiple arguments, and wrap quotes around each one.
#!/bin/bash
qlmanage -p “$@” >& /dev/null &
Thanks for the tip.
One thing to keep in mind is that the passed value might contain special characters, in which case qlmanage does not behave appropriately. [For example, if the file name is "Jim Clark. _Debbie Does Dallas_. 1978. (Showers).mov" (w/o the quotes)]
In that case, encasing the filename in quotation marks as in the following works like a charm.
#!/bin/bash
qlmanage -p “$1″ >& /dev/null &
I prefer the following version:
#!/bin/bash
qlmanage -p “$1″ >& /dev/null
The changes are the quotation marks around $1, so it works on files with spaces in their names, and the removal of the backgrounding, so you can use ^C to kill the window. You can always add that back in upon invocation:
ql Some\ File &
A request disguised as a comment. It would be really nice if someone could whip up a script that would run “ps | grep qlmanage” find the pid and then issue a kill for the resulting pid. The resulting script might be called, qlk.
Anyone?
@mistersquid
Good idea, here’s a script that should work. This will close any quicklook window opened from the shell (more specifically kills any process containing “qlmanage -p”). Feel free to modify to suit your needs.
#!/bin/bash
ps | grep “qlmanage -p” | awk ‘{print $1}’ | while read FN; do kill $FN >& /dev/null; done;
To borrow and extend:
#!/bin/bash
if [[ $1 == '' ]]
then
echo “Usage: ql ”
else
qlmanage -p “$1″ >& /dev/null &
read
ps | grep “qlmanage -p” | awk ‘{print $1}’ | while read FN; do kill $FN >& /dev/null; done;
fi
This should fairly closely copy the finder functionality. Press return after the file opens in quick look and it should then close. Don’t think its possible to make it happen from hitting the space bar sadly.
@srhaber
No need for grep and awk - there is already a unix command to do this:
killall “qlmanage -p”
instead of
read
using
read -n 1
will allow you to press any key, spacebar, return etc. and have the command return.
So the final code would be:
#!/bin/bash
if [[ $1 == '' ]]
then
echo “Usage: ql ”
else
qlmanage -p “$1″ >& /dev/null &
read -n 1
killall “qlmanage -p” &> /dev/null
fi
Better still, since we’re using Bash,
$!will give us the PID of the most recently launched background process. So we can turn it into something along the lines of:$(qlmanage -p "$@" > /dev/null 2>&1 &local ql_pid=$!
read -sn 1
kill ${ql_pid}) > /dev/null 2>&1
I’m pushing it into a subshell so that we can hide the job messages bash spits out, capturing the background process’ pid, reading a single character (while not echoing it to stdout), then killing that pid.
@Will
Thanks for the tip. “killall qlmanage -p” works just as well, and is much less verbose.
To force quicklook to display a file as plaintext you can use the -c switch:
qlmanage -p -c public.plain-text “$2″ >& /dev/null &
More bash-fu - eliminate the need for a script that you have to alias and keep track of and just add the following to your ~/.bashrc :
# Display files in Quick Look
function ql ()
{
(qlmanage -p “$@” > /dev/null 2>&1 &
local ql_pid=$!
read -sn 1
kill ${ql_pid}) > /dev/null 2>&1
}
# Display any filetype as plain text
function qlt ()
{
(qlmanage -p -c public.plain-text “$@” > /dev/null 2>&1 &
local ql_pid=$!
read -sn 1
kill ${ql_pid}) > /dev/null 2>&1
}
@Graeme Mathieson:
I tried your version, and “local” only works in a function. Here’s what I have now:
#!/bin/bash
$(qlmanage -p "$@" > /dev/null 2>&1 &
ql_pid=$!
read -sn 1
kill ${ql_pid}) > /dev/null 2>&1
I always used to simply do
open «filename». This is basically equivalent to double-clicking the file in Finder, so images or PDFs will open in Preview. Then you can just hit ?Q to quit when you’re done.You navigate in finder!?
open .
Voila!
Speaking of
open, I added the following to my .bashrc:function open () {if [ -z "$*" ]; then
/usr/bin/open .
else
/usr/bin/open “$@”
fi
}
Now instead of typing
open ., I can just typeopen.Actually, save yourself the navigation in the Finder. Just do
> open .
and the Finder will open a new window with that path.
Is there a way to export/capture the quicklook thumbnail as an image? It seems this should be basic or at least easy to do, but I can’t find anything on Google.
Hrm…
If not, what’s the easiest way to automate turning HTML links to PDFs into the thumbnails of those PDFs?
Rimsky went look closer buy cytotec then announced daughters.