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

Re: Fun zsh trick for today



> I'm no Bart-guru, but here's an application of his trick that I find useful
> for the new completion system:
> 
> zmodload -i zsh/mapfile
> hosts=(${(@)${(@)${(M)${(f)mapfile[$HOME/.ssh/known_hosts]}:#[^0-9]*}%%\ *}%%
> ,*})
> zstyle ':completion:*:hosts' hosts $hosts

In something like this there's really no gain over doing it using more
ordinary zsh methods, i.e. with $(<...), which doesn't require a module.
Plus you don't need all those (@)'s once parameter substitution already has
arrays, which (f) provides.  The only trick here is using double quotes to
get whole lines for splitting.  I think the following should work in 3.0.8
(can't quite remember if the trick of putting double quotes in the middle
made its way back --- if not, put quotes around the lot and put (@)'s at
the front of all the parameter substitutions).

hosts=(${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[^0-9]*}%%\ *}%%,*
})

or

hosts=("${(@)${(@)${(@M)${(@f)"$(<$HOME/.ssh/known_hosts)"}:#[^0-9]*}%%\ *}%%,*
}")

-- 
Peter Stephenson <pws@xxxxxxxxxxxxxxxxxxxxxxxxx>
Cambridge Silicon Radio, Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070



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