Quick Look from the command line!

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.

Download it here.

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].

ql_on

Using the ql command in Terminal

ql_off

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 &

28 Responses to “Quick Look from the command line!”


  1. 1 Jannik Nielsen

    When in quicklook you can also just hit the escape key to close the ql window.

  2. 2 Jannik Nielsen

    oh… except when using it from the command line :-) sorry about that.

  3. 3 Stan Seibert

    “open .” will pop up a Finder window for the current directory. It is very handy. (In fact, the “open” command itself is fantastically useful.)

  4. 4 Jonathan Lehr

    Or, just run qlmanage -p in the foreground and type ^C to kill it.

  5. 5 Nick Snell

    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….

  6. 6 Kurt Tappe

    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!

  7. 7 Derek Remund

    @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.

  8. 8 Andy Matuschak

    In the script, you need quotes around $1 like to make it work for files with characters like spaces and hyphens.

  9. 9 srhaber

    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 &

  10. 10 mistersquid

    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 &

  11. 11 James Lawton

    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 &

  12. 12 mistersquid

    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?

  13. 13 srhaber

    @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;

  14. 14 Jamie Poitra

    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.

  15. 15 Will

    @srhaber

    No need for grep and awk - there is already a unix command to do this:

    killall “qlmanage -p”

  16. 16 daniel

    instead of
    read
    using
    read -n 1
    will allow you to press any key, spacebar, return etc. and have the command return.

  17. 17 daniel

    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

  18. 18 Graeme Mathieson

    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.

  19. 19 srhaber

    @Will

    Thanks for the tip. “killall qlmanage -p” works just as well, and is much less verbose.

  20. 20 Thomas

    To force quicklook to display a file as plaintext you can use the -c switch:

    qlmanage -p -c public.plain-text “$2″ >& /dev/null &

  21. 21 Alden

    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
    }

  22. 22 Ed Brannin

    @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

  23. 23 Ciarán Walsh

    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.

  24. 24 kL

    You navigate in finder!?

    open .

    Voila!

  25. 25 Kevin Bullock

    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 type open.

  26. 26 Pecos Bill

    Actually, save yourself the navigation in the Finder. Just do
    > open .

    and the Finder will open a new window with that path.

  27. 27 David

    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?

  28. 28 Pwhndvve

    Rimsky went look closer buy cytotec then announced daughters.

Leave a Reply