zsh-users
 help / color / mirror / code / Atom feed
* Mysterious completion of variables
@ 2005-11-17 20:36 Ian Langworth
  2005-11-18 10:21 ` Peter Stephenson
  2005-11-18 11:32 ` Hannu Koivisto
  0 siblings, 2 replies; 6+ messages in thread
From: Ian Langworth @ 2005-11-17 20:36 UTC (permalink / raw)
  To: zsh-users

I have a few variables for hosts that I shell into frequently:

  aspen='me@aspen.mydomain.com'
  alias aspen="ssh $aspen"

Thus, I'm usually able to do the following:

  scp $aspen:temp .

This usually works, but there are occasions where I get strange
completion after I hit <Enter>:

  cp: cannot stat `me@aspen.mydomain.comemp': No such file or directory

Sounds like it's interpolating ':t', but I'm not sure why. Ideas?

--
Ian Langworth


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Mysterious completion of variables
  2005-11-17 20:36 Mysterious completion of variables Ian Langworth
@ 2005-11-18 10:21 ` Peter Stephenson
  2005-11-18 11:32 ` Hannu Koivisto
  1 sibling, 0 replies; 6+ messages in thread
From: Peter Stephenson @ 2005-11-18 10:21 UTC (permalink / raw)
  To: zsh-users

Ian Langworth wrote:
> I have a few variables for hosts that I shell into frequently:
> 
>   aspen='me@aspen.mydomain.com'
>   alias aspen="ssh $aspen"
> 
> Thus, I'm usually able to do the following:
> 
>   scp $aspen:temp .
> 
> This usually works, but there are occasions where I get strange
> completion after I hit <Enter>:
> 
>   cp: cannot stat `me@aspen.mydomain.comemp': No such file or directory
> 
> Sounds like it's interpolating ':t', but I'm not sure why. Ideas?

Yes, but it's not actually completion... the syntax $aspen:t takes the
tail end of a path.  It's borrowed from history substitution.  Since
there isn't a slash in the variable it has no effect except that the :t
is swallowed up and the emp treated as normal text (because another
modifier would need another colon in front).

You need to use ${aspen}:temp or $aspen\:t to protect the :.

This feature isn't compatible with other shells.  It's turned off when
you set the KSH_ARRAYS option, but that has numerous other effects
so I wouldn't recommend that unless you really know what you're doing.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Mysterious completion of variables
  2005-11-17 20:36 Mysterious completion of variables Ian Langworth
  2005-11-18 10:21 ` Peter Stephenson
@ 2005-11-18 11:32 ` Hannu Koivisto
  2005-11-18 11:47   ` Peter Stephenson
  1 sibling, 1 reply; 6+ messages in thread
From: Hannu Koivisto @ 2005-11-18 11:32 UTC (permalink / raw)
  To: Ian Langworth; +Cc: zsh-users

Ian Langworth <ian.langworth@gmail.com> writes:

> I have a few variables for hosts that I shell into frequently:
>
>   aspen='me@aspen.mydomain.com'
>   alias aspen="ssh $aspen"
>
> Thus, I'm usually able to do the following:
>
>   scp $aspen:temp .

Since Peter explained that a more complex form is required, I would
like to suggest another approach that avoids such a variable to
begin with.

Add...

Host aspen
Hostname aspen.mydomain.com
User me

...to your ~/.ssh/config file.  This allows you to write 
'scp aspen:temp .' and 'ssh aspen'.

You might also want to replace _ssh_hosts in _ssh with the function
I provided in a mail sent earlier to this list.  I reproduce it
here in order to save you the trouble of finding it from the
archives:

-----8<------------------------------------------------------------
I modified ssh completion to dig host aliases from the
configuration file.  I don't use wildcards in host patterns or give
multiple patterns, so this doesn't attempt to handle such cases.  I
also have no idea whether this modification is otherwise well
written or not; I'm still a beginner in completion hacking.  All
comments appreciated.

I think that a well written version (one that handles multiple
patterns and patterns with wildcards, among the other issues) of
the same subject would be nice as a standard feature.  One might
also want to utilize hosts from the known_hosts file.

_ssh_hosts () {
  if [[ "$IPREFIX" == *@ ]]; then
    _combination -s '[:@]' my-accounts users-hosts "users=${IPREFIX/@}" hosts "$@"
  else
    _combination -s '[:@]' my-accounts users-hosts \
      ${opt_args[-l]:+"users=${opt_args[-l]:q}"} hosts "$@"
  fi
  if [[ -r "$HOME/.ssh/config" ]]; then
      local IFS=" 	" key host
      while read key host; do
          if [[ "$key" == (#i)host ]]; then
              _wanted hosts expl host \
                  compadd -M 'm:{a-zA-Z}={A-Za-z} r:|.=* r:|=*' "$@" "$host"
          fi
      done < "$HOME/.ssh/config"
  fi
}
-----8<------------------------------------------------------------

This allows you to say 'scp a<tab>' and get 'scp aspen:' assuming
you don't have other hosts beginning with 'a'.  I hope it works
with _ssh of recent zsh versions, I haven't tested; I made a copy
of _ssh from an older version to a personal function directory with
higher priority and modified that.

-- 
Hannu


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Mysterious completion of variables
  2005-11-18 11:32 ` Hannu Koivisto
@ 2005-11-18 11:47   ` Peter Stephenson
  2005-11-18 14:20     ` Ian Langworth
  2005-11-18 17:38     ` Wayne Davison
  0 siblings, 2 replies; 6+ messages in thread
From: Peter Stephenson @ 2005-11-18 11:47 UTC (permalink / raw)
  To: zsh-users

Hannu Koivisto <azure@iki.fi> wrote:
> I modified ssh completion to dig host aliases from the
> configuration file.

I'll commit it in the following form.

The only point of changing "host" to "remote host name" everywhere is that
otherwise the hosts appear under two separate names (if you have the
descriptions turned on).  I'm not attached to the latter rather than the
form, it just has to be consistent throughout.

I'm not sure whether the extra _wanted in _ssh_hosts is needed, given how
_ssh_hosts is called, but I think it's at least harmless.

Index: Completion/Unix/Command/_ssh
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Command/_ssh,v
retrieving revision 1.24
diff -u -r1.24 _ssh
--- Completion/Unix/Command/_ssh	9 May 2005 02:38:35 -0000	1.24
+++ Completion/Unix/Command/_ssh	18 Nov 2005 11:44:47 -0000
@@ -283,11 +283,11 @@
       elif compset -P '*@'; then
         suf=( -S '' )
         compset -S ':*' || suf=( -S : )
-        _wanted hosts expl host _ssh_hosts $suf && ret=0
+        _wanted hosts expl 'remote host name' _ssh_hosts $suf && ret=0
       else
         _alternative \
 	    'files:: _files' \
-	    'hosts:host:_ssh_hosts -S:' \
+	    'hosts:remote host name:_ssh_hosts -S:' \
 	    'users:user:_ssh_users -qS@' && ret=0
       fi
       ;;
@@ -298,7 +298,7 @@
         _wanted hosts expl host _ssh_hosts -S: && ret=0
       else
         _alternative \
-	    'hosts:host:_ssh_hosts -S:' \
+	    'hosts:remote host name:_ssh_hosts -S:' \
 	    'users:user:_ssh_users -qS@' && ret=0
       fi
       ;;
@@ -311,12 +311,26 @@
 }
 
 _ssh_hosts () {
+  local -a config_hosts
+
   if [[ "$IPREFIX" == *@ ]]; then
     _combination -s '[:@]' my-accounts users-hosts "users=${IPREFIX/@}" hosts "$@"
   else
     _combination -s '[:@]' my-accounts users-hosts \
       ${opt_args[-l]:+"users=${opt_args[-l]:q}"} hosts "$@"
   fi
+  if [[ -r "$HOME/.ssh/config" ]]; then
+    local IFS=$'\t ' key host
+    while read key host; do
+      if [[ "$key" == (#i)host ]]; then
+	 config_hosts+=("$host")
+      fi
+    done < "$HOME/.ssh/config"
+    if (( ${#config_hosts} )); then
+      _wanted hosts expl 'remote host name' \
+	compadd -M 'm:{a-zA-Z}={A-Za-z} r:|.=* r:|=*' "$@" $config_hosts
+    fi
+  fi
 }
 
 _ssh "$@"


This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Mysterious completion of variables
  2005-11-18 11:47   ` Peter Stephenson
@ 2005-11-18 14:20     ` Ian Langworth
  2005-11-18 17:38     ` Wayne Davison
  1 sibling, 0 replies; 6+ messages in thread
From: Ian Langworth @ 2005-11-18 14:20 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

Not only was that an informative answer, but I've learned a bit about
SSH and completion configuration as well. Thank you both.

--
Ian Langworth


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Mysterious completion of variables
  2005-11-18 11:47   ` Peter Stephenson
  2005-11-18 14:20     ` Ian Langworth
@ 2005-11-18 17:38     ` Wayne Davison
  1 sibling, 0 replies; 6+ messages in thread
From: Wayne Davison @ 2005-11-18 17:38 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

On Fri, Nov 18, 2005 at 11:47:05AM +0000, Peter Stephenson wrote:
> +      if [[ "$key" == (#i)host ]]; then
> +         config_hosts+=("$host")
> +      fi

There's two problems with this for general use (as pointed out by Ian
himself):  it puts multiple hostnames into a single completion item, and
it includes wild-carded items as hostnames.  I patched the function like
this:

--- Completion/Unix/Command/_ssh	18 Nov 2005 14:53:44 -0000	1.25
+++ Completion/Unix/Command/_ssh	18 Nov 2005 17:22:39 -0000
@@ -320,10 +320,15 @@ _ssh_hosts () {
       ${opt_args[-l]:+"users=${opt_args[-l]:q}"} hosts "$@"
   fi
   if [[ -r "$HOME/.ssh/config" ]]; then
-    local IFS=$'\t ' key host
-    while read key host; do
+    local IFS=$'\t ' key hosts host
+    while read key hosts; do
       if [[ "$key" == (#i)host ]]; then
-	 config_hosts+=("$host")
+	 for host in ${(z)hosts}; do
+	    case $host in
+	    (*[*?]*) ;;
+	    (*) config_hosts+=("$host") ;;
+	    esac
+	 done
       fi
     done < "$HOME/.ssh/config"
     if (( ${#config_hosts} )); then

There may well be a non-looping way to do that, so feel free to
enlighten me if there is.

..wayne..


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-11-18 17:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-17 20:36 Mysterious completion of variables Ian Langworth
2005-11-18 10:21 ` Peter Stephenson
2005-11-18 11:32 ` Hannu Koivisto
2005-11-18 11:47   ` Peter Stephenson
2005-11-18 14:20     ` Ian Langworth
2005-11-18 17:38     ` Wayne Davison

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).