Last Updated: February 25, 2016
· losinggeneration

Sort apt-cache search with a shell function

I don't know about you, but the order in which apt-cache shows results makes it harder to find what you're looking for. What I tend to do is pipe the results through either sort or grep. To automate this I use a function in the shell.

Add the following to your $HOME/.bashrc (or whatever your shell is, zsh with $HOME/.zshrc is my default shell on most of my machines for instance)

function apt-cache()
    local search=""
    # Initial space will be optional if no options are given
    [ "$(echo " $*" | grep -E '[[:space:]]*search[[:space:]]')" ] && search=true

    if [ "$search" ]; then
            command apt-cache $* | sort
            command apt-cache $*

Then things like:

apt-cache search kate

are now sorted alphabetically.

Note things like:

apt-cache search -h

will not work as expected. Also, if the package is just called search it might do strange things (currently not an issue.)