From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19734 invoked from network); 17 Jul 2004 18:49:42 -0000 Received: from unknown (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 17 Jul 2004 18:49:42 -0000 Received: (qmail 69369 invoked from network); 17 Jul 2004 18:49:35 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 17 Jul 2004 18:49:35 -0000 Received: (qmail 18292 invoked by alias); 17 Jul 2004 18:48:51 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 7706 Received: (qmail 18282 invoked from network); 17 Jul 2004 18:48:51 -0000 Received: from unknown (HELO a.mx.sunsite.dk) (130.225.247.88) by 130.225.247.90 with SMTP; 17 Jul 2004 18:48:51 -0000 Received: (qmail 67011 invoked from network); 17 Jul 2004 18:44:59 -0000 Received: from unknown (HELO moonbase.zanshin.com) (167.160.213.139) by a.mx.sunsite.dk with SMTP; 17 Jul 2004 18:44:56 -0000 Received: from toltec.zanshin.com (toltec.zanshin.com [64.84.47.166]) by moonbase.zanshin.com (8.12.11/8.12.11) with ESMTP id i6HIitqj004078 for ; Sat, 17 Jul 2004 11:44:55 -0700 Date: Sat, 17 Jul 2004 11:44:55 -0700 (PDT) From: Bart Schaefer Reply-To: zsh-users@sunsite.dk To: zsh-users Subject: Re: [: Re: ssh scp completion] sorry wrong key pressed :/ In-Reply-To: <20040717064207.GA7673@xover.htu.tuwien.ac.at> Message-ID: References: <20040717064207.GA7673@xover.htu.tuwien.ac.at> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Checker-Version: SpamAssassin 2.63 on a.mx.sunsite.dk X-Spam-Level: X-Spam-Status: No, hits=0.0 required=6.0 tests=none autolearn=no version=2.63 X-Spam-Hits: 0.0 On Sat, 17 Jul 2004, Martin Marcher wrote: > On Fri, Jul 16, 2004 at 05:05:48PM +0200, Laurent Rineau wrote: > > You can set up directly the style hosts: > > > > zstyle ':completion:*:*:*' hosts foo bar > > > > or use something like what I have in my .zshrc: > > > > [ -f ~/.ssh/config ] && : ${(A)ssh_config_hosts:=${${${${(@M)${(f)"$(<~/.ssh/config)"}:#Host *}#Host }:#*\**}:#*\?*}} > > zstyle ':completion:*:*:*' hosts $ssh_config_hosts $ssh_known_hosts > > some explanation of this stuff would be more of what i thought, because > from what i found out by now it's not only expansion there's a lot of > stuff going on there. It's hard to know what to explain ... a good place to start is probably to run the "compinstall" function, create a few settings with it and save them, then look at the results. (Hey, PWS -- it might be nice if there were a way to tell compinstall "show me the result of the settings I just made" so that it could be used as sort of an interactive tutorial. Even if it could simply print to the screen the whole collection of styles composed so far, that'd be a reasonable first step.) The zstyle mechanism used for completion "context" can take a little getting used to, but it's quite handy once you understand it. In a variable reference, like $ssh_config_hosts, there's exactly one name/value pair that can satisfy the lookup. A zstyle name, on the other hand, includes a pattern, and so when a zstyle is looked up there may be more than one name/value pair found. The clever bit is that zstyle makes a judgement as to how "specific" the pattern is, and answers the lookup with the name/value pair for the most specific pattern that matches. The completion system makes use of this by building up an increasingly detailed summary of its understanding of the command line. That summary is the context, and is used as the lookup key against which the zstyle patterns are matched to find the best fit. When you hit TAB, this summary starts out essentially empty, so only a very nonspecific style pattern will match it. As the various completion functions are called to analyze the commenad and the current cursor position, they add to the context so the more-specific zstyles will begin to match when settings are looked up. So, depending on how early in the analysis a particular setting is needed, it may not be useful to configure it differently for different commands or situations. (The zstyle mechanism is generic and not tied to completion, so it won't stop you from using any pattern you want, but some patterns that might seem sensible are useless because the setting is never looked up with a specific-enough context to be matched by the pattern.) Having just wandered off on that tangent, perhaps I should have been addressing this specific example. Laurent's style is a little unusual. I would have written it this way: [[ -f ~/.ssh/config && -z $ssh_config_hosts ]] && ssh_config_hosts=(...) I'm not even certain that Laurent's example accomplishes what he meant, because the stuff to the right in ${...:=...} is assigned as a string even if you use the (A) flag; it's just that the name to the left is created as a one-element array instead of as a scalar. > and another question: why is it that i dont simply use some grep/awk > combination? Because it's frequently faster and less CPU-intensive to handle everything inside zsh, rather than forking off a subshell, awk, grep, etc. and then reading back the standard output of the pipeline. In this specific case it only happens once, so it doesn't make as much difference, but for some completions that sort of transformation is done every time you press TAB. Most of the time these sorts of things get written first as a $(...) of a complex shell pipeline and then, after the completion is working properly, as much of the pipeline as possible is folded back into zsh parameter expressions for efficiency. After you've done this several times, though, it becomes possible to write the parameter expressions directly for the more common sorts of transformations (in particular, "cut" is unnecessary for someone really familiar with zsh parameter expansion, except when processing very large files).