Searching All Your Clipboard History EVER Via Alfred In The Terminal

.

We have written in the past about auto suggestions as you type using your shell history. A recent tip from @mrw led us to learn that Alfred actually stores ALL of your clipboard history in a file. So if you’re an Alfred user, you can access your clipboard history data in a sqlite table. Leveraging that you can actually search your clipboard history in the terminal by connecting to that sqlite database and querying against it. Let’s check it out!

Inspiration

As mentioned in the auto suggestion post we leverage zsh-histdb their to be able to query from a sqlite table. We use similar logic to pull in the Alfred sqlite file and query that.

Let’s Get To The Code

The first thing we need to do is to load in the sqlite file, so we set that to a variable to reference later:

CLIPBOARDDB_FILE="${HOME}/Library/Application Support/Alfred/Databases/clipboard.alfdb"

Next we need to make a function that uses that sqlite file location and passes any arguments to it. The arguments in this instance will be the query which we will get to later:

clipboard_query () {
    sqlite3 ${CLIPBOARDDB_FILE} "$@"
    [[ "$?" -ne 0 ]] && echo "error in $@"
}

Now let’s write the actual query logic to select from our alfred clipboard history:

search_clipboard_history() {
    local query="
        select Item
        from clipboard
        where Item LIKE '$(sql_escape $1)%'
        order by ts desc
        limit 10
    "
    results=$(clipboard_query "$query")
    echo "$results"
}

And that’s it! We have a function that can now take in a search query and that runs the query against our Alfred sqlite database file. The entire script looks like this:

which sqlite3 >/dev/null 2>&1 || return;

CLIPBOARDDB_FILE="${HOME}/Library/Application Support/Alfred/Databases/clipboard.alfdb"

clipboard_query () {
    sqlite3 ${CLIPBOARDDB_FILE} "$@"
    [[ "$?" -ne 0 ]] && echo "error in $@"
}

search_clipboard_history() {
    local query="
        select Item
        from clipboard
        where Item LIKE '$(sql_escape $1)%'
        order by ts desc
        limit 10
    "
    results=$(clipboard_query "$query")
    echo "$results"
}

This is what the usage looks like:

search-history

Phew! That should be it! Feel free to tweet at us or DM us on instagram with questions!