Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

using precmd() to get hostnames out of commands



Right, a while ago I decided that I wanted my hostname completion to
include any hostnames that I typed on the command line during a
session so I could do things like:


grover:/home/alan>ping  penglet.org
<snip>
grover:/home/alan>ssh pen<tab>

So I came up with this:


iplegal()
{
if [[ $1 == ([1-9]||[1-9][0-9]||1[0-9][0-9]||[2[0-4][0-9]||25[0-5]).\
([0-9]||[1-9][0-9]||1[0-9][0-9]||[2[0-4][0-9]||25[0-5]).\
([0-9]||[1-9][0-9]||1[0-9][0-9]||[2[0-4][0-9]||25[0-5]).\
([0-9]||[1-9][0-9]||1[0-9][0-9]||[2[0-4][0-9]||25[0-5]) ]]
then
        echo legal
else
        echo illegal
fi
}

precmd()
{
        # add any hostnames from the last command to the 
        # hostname completion array
        lastargs=(`fc -ln -1`)

        for stuff in $lastargs
        do
                if [[ $stuff == (*.org||*.com||*.net||*.edu||*.??~*.\
[[:digit:]][[:digit:]]) || `iplegal $stuff` == legal ]]
                then
                        hosts=($hosts $stuff)
                        zstyle ':completion:*:hosts' hosts $hosts
                fi
        done
}

It also recognises IPs and sticks them in too.

Is there a better way to do this? 

Thanks.
-- 
Alan Third

Cows go moo!



Messages sorted by: Reverse Date, Date, Thread, Author