zsh-users
 help / color / mirror / code / Atom feed
* global aliases substituting *within* a path
@ 2012-05-16 11:43 Ronald Fischer
  2012-05-16 12:17 ` Valodim Skywalker
  2012-05-16 12:40 ` Manuel Presnitz
  0 siblings, 2 replies; 6+ messages in thread
From: Ronald Fischer @ 2012-05-16 11:43 UTC (permalink / raw)
  To: zsh-users

I have a set of directory structures like this:

aaa/foo/bar/baz/xxx
bbb/foo/bar/baz/yyy
ccc/foo/bar/baz/zzz
etc.

I'm looking for a way to make typing easier on the command line, in
order to not have to type foo/bar/baz all the time.

Of course I can achieve this by setting a shell variable in my .zshrc:

X=foo/bar/baz

Then I can do for instance

  ls aaa/$X/xxx

Now I recently learned about global aliases, which permit alias
substitution to be done within the command line, and I thought that I
maybe could use this. Here was my (failed) attempt:

alias -g X=foo/bar/baz

# Does NOT work at hoped
ls aaa/X/xxx

X is not substituted, because it is not a word on its own (not
surrounded by spaces).

My question: For my problem, do I have to stick with my original
solution (shell variable), or is it a way to do it with aliases, or is
there maybe an even more clever way to achieve my goal?

Ronald
-- 
Ronald Fischer <ronaldf@eml.cc>
+  If a packet hits a pocket on a socket on a port, 
+  and the bus is interrupted and the interrupt's not caught,
+  then the socket packet pocket has an error to report.
+		(cited after Peter van der Linden)


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

* Re: global aliases substituting *within* a path
  2012-05-16 11:43 global aliases substituting *within* a path Ronald Fischer
@ 2012-05-16 12:17 ` Valodim Skywalker
  2012-05-16 12:40 ` Manuel Presnitz
  1 sibling, 0 replies; 6+ messages in thread
From: Valodim Skywalker @ 2012-05-16 12:17 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 26 bytes --]

echo a/f/b/b/x<TAB>

 - V

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: global aliases substituting *within* a path
  2012-05-16 11:43 global aliases substituting *within* a path Ronald Fischer
  2012-05-16 12:17 ` Valodim Skywalker
@ 2012-05-16 12:40 ` Manuel Presnitz
  1 sibling, 0 replies; 6+ messages in thread
From: Manuel Presnitz @ 2012-05-16 12:40 UTC (permalink / raw)
  To: Ronald Fischer, zsh-users

Not an answer to your questions, but also quite handy sometimes:

$ ls aaa/**/xxx

Manuel.
 
 
 
Ronald Fischer hat am 16.05.2012 um 13:43 folgendes geschrieben:

> I have a set of directory structures like this:
> 
> aaa/foo/bar/baz/xxx
> bbb/foo/bar/baz/yyy
> ccc/foo/bar/baz/zzz
> etc.
> 
> I'm looking for a way to make typing easier on the command line, in
> order to not have to type foo/bar/baz all the time.
> 
> Of course I can achieve this by setting a shell variable in my .zshrc:
> 
> X=foo/bar/baz
> 
> Then I can do for instance
> 
>   ls aaa/$X/xxx
> 
> Now I recently learned about global aliases, which permit alias
> substitution to be done within the command line, and I thought that I
> maybe could use this. Here was my (failed) attempt:
> 
> alias -g X=foo/bar/baz
> 
> # Does NOT work at hoped
> ls aaa/X/xxx
> 
> X is not substituted, because it is not a word on its own (not
> surrounded by spaces).
> 
> My question: For my problem, do I have to stick with my original
> solution (shell variable), or is it a way to do it with aliases, or is
> there maybe an even more clever way to achieve my goal?
> 
> Ronald
> -- 
> Ronald Fischer <ronaldf@eml.cc>
> +  If a packet hits a pocket on a socket on a port, 
> +  and the bus is interrupted and the interrupt's not caught,
> +  then the socket packet pocket has an error to report.
> +		(cited after Peter van der Linden)




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

* Re: global aliases substituting *within* a path
  2012-05-18 13:08 Ronald Fischer
@ 2012-05-21 17:54 ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2012-05-21 17:54 UTC (permalink / raw)
  To: zsh-users

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


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

* Re: global aliases substituting *within* a path
@ 2012-05-18 13:08 Ronald Fischer
  2012-05-21 17:54 ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Ronald Fischer @ 2012-05-18 13:08 UTC (permalink / raw)
  To: zsh-users; +Cc: valodim

> 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). Instead I have to write them out explicitly
(foo/bar/baz), i.e. this is exactly what I wanted to avoid.

(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.

:-(

Ronald
-- 
Ronald Fischer <ronaldf@eml.cc>
+  If a packet hits a pocket on a socket on a port, 
+  and the bus is interrupted and the interrupt's not caught,
+  then the socket packet pocket has an error to report.
+		(cited after Peter van der Linden)


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

* global aliases substituting *within* a path
@ 2012-05-16 11:41 Ronald of Steiermark
  0 siblings, 0 replies; 6+ messages in thread
From: Ronald of Steiermark @ 2012-05-16 11:41 UTC (permalink / raw)
  To: zsh-users

I have a set of directory structures like this:

aaa/foo/bar/baz/xxx
bbb/foo/bar/baz/yyy
ccc/foo/bar/baz/zzz
etc.

I'm looking for a way to make typing easier on the command line, in
order to not have to type foo/bar/baz all the time.

Of course I can achieve this by setting a shell variable in my .zshrc:

X=foo/bar/baz

Then I can do for instance

  ls aaa/$X/xxx

Now I recently learned about global aliases, which permit alias
substitution to be done within the command line, and I thought that I
maybe could use this. Here was my (failed) attempt:

alias -g X=foo/bar/baz

# Does NOT work at hoped
ls aaa/X/xxx

X is not substituted, because it is not a word on its own (not
surrounded by spaces).

My question: For my problem, do I have to stick with my original
solution (shell variable), or is it a way to do it with aliases, or is
there maybe an even more clever way to achieve my goal?

Ronald
-- 
Ronald Fischer <austria_rules@yepmail.net>

There are 10 types of people in the world: those who understand binary and those who don't.


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

end of thread, other threads:[~2012-05-21 17:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-16 11:43 global aliases substituting *within* a path Ronald Fischer
2012-05-16 12:17 ` Valodim Skywalker
2012-05-16 12:40 ` Manuel Presnitz
  -- strict thread matches above, loose matches on Subject: below --
2012-05-18 13:08 Ronald Fischer
2012-05-21 17:54 ` Bart Schaefer
2012-05-16 11:41 Ronald of Steiermark

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).