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

Re: using precmd() to get hostnames out of commands



[Apologies if this shows up twice.  I'm concerned that the list server
is silently discarding mail from the brasslantern.com domain for some
reason, so I'm resending from another account as a test.]

On Jul 29,  2:57pm, Alan Third wrote:
} Subject: 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
}
} Is there a better way to do this?

  histhosts() {
    if (( $+_histhosts_cache ))
    then
      set -- ${(z)history[$[${(%):-%h}-1]]}
    else
      typeset -ag _histhosts_cache
      set -- $historywords
    fi
    while (( $# ))
    do
      [[ $1 == (<1-255>.<0-255>.<0-255>.<0-255>|*.org|*.com|*.net|*.edu) ||
	 $1 == *.??~*.<-> ]] && _histhosts_cache[1,0]=$1
      shift
    done
    reply=( $_histhosts_cache )
  }

  zstyle -e ':completion:*:hosts' hosts histhosts

You don't usually want `||' in a glob pattern.  That means "or empty or".
Just one `|'.

About that `_histhosts_cache[1,0]' -- the subscript [1,0] refers to the
non-existent element to the left of element 1, so assigning to that
element is roughly equivalent to perl `unshift'.  This only works in
some late versions of 3.1.9-dev and later.

About `*.??~*.<->' -- that means any two characters that are not digits.
But I think you might be better off with `[^/]#.[[:alpha:]][[:alpha:]]',
which at least omits file paths although `foobar.tar.gz' sneaks past.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net



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