zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: global aliases substituting *within* a path
Date: Mon, 21 May 2012 10:54:48 -0700	[thread overview]
Message-ID: <120521105448.ZM32691@torch.brasslantern.com> (raw)
In-Reply-To: <1337346499.24458.140661077281237.140CF9BF@webmail.messagingengine.com>

On May 18,  3:08pm, Ronald Fischer wrote:
}
} > echo a/f/b/b/x<TAB>
} 
} Doesn't help here, for two reason:
} 
} (1) At least in my version of zsh (4.3.12), this would not expand the
} interim directories (f,b,b).

Does your zsh configuration include installing the completion system
(the "compinit" command)?

} (2) Even if I could do TAB completion on directories within the path,
} this is not what I'm looking for, because it is still cumbersome to
} type.

You might want to look at named directories:

    hash -d aaa=aaa/foo/bar/baz
    hash -d bbb=bbb/foo/bar/baz
    hash -d ccc=ccc/foo/bar/baz

Then you can write

    ls ~aaa/xxx
    ls ~bbb/yyy
    ls ~ccc/zzz

I don't know how much variation there is in the "foo/bar/baz" part of
your structure.  You can try using dynamic named directories:

    X=foo/bar/baz
    ls ~[aaa/X]/xxx

That's not saving any keystrokes over aaa/$X/xxx but might have some
aesthetic advantages.  It's implemented with something like this:

    stem_name_hook() {
	case $1 in
        (n) local root=$2:h stem=$2:t
            reply=( $root/${(P)stem} )
	    ;;
	(*) return 1;
	esac
    }

    autoload -Uz add-zsh-hook
    add-zsh-hook zsh_directory_name stem_name_hook

The above allows ANY leading path (the "root") and ANY variable name (the
"stem") after the slash to be the "middle of the path", e.g.

    Y=fare/thee/well
    ls ~[/usr/local/Y]/yyy

You could of course apply a similar transformation on the root instead
(or as well) but that's not much different than ordinary "hash -d" with
perhaps auto_name_dirs enabled.

If you want to be able to use it in completions and other automatic path
contractions, you need a reverse mapping that provides a sensible set of
root and stem names.  Here's one that uses ONLY aaa, bbb, ccc for roots
and ONLY $X for the stem:

    stem_name_hook() {
	case $1 in 
	(n) reply=( $2:h/$X )
	    ;;
	(d) reply=( ${2/\/$X(\/*|)/\/X})
	    reply=($reply ${#reply[1]} )
	    ;;
	(c) local dirs
	    dirs=( (aaa|bbb|ccc)/$X )
	    if (( $#dirs )); then
		dirs=( ${^dirs%$X}X )
		_wanted dynamic-dirs expl 'dynamic directory' \
		    compadd -S \] -a dirs
	    else
	        return 1
	    fi
	    ;;
	(*) return 1;
	esac
    }

I'm not entirely sure about that (c) branch, which is for compsys [cf.
my response to (1)].  PWS is the expert on dynamic directory hooks.

-- 
Barton E. Schaefer


  reply	other threads:[~2012-05-21 17:55 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-18 13:08 Ronald Fischer
2012-05-21 17:54 ` Bart Schaefer [this message]
  -- strict thread matches above, loose matches on Subject: below --
2012-05-16 11:43 Ronald Fischer
2012-05-16 12:17 ` Valodim Skywalker
2012-05-16 12:40 ` Manuel Presnitz
2012-05-16 11:41 Ronald of Steiermark

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=120521105448.ZM32691@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).