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

Re: directory alias



On Dec 3, 10:21pm, shawn wilson wrote:
> Subject: directory alias
>
> not sure if this is really a 'zsh thing' but I'm looking for a way to
> create aliases for a command. I don't want a bunch of symlinks in my
> home directory, and I don't want a universal alias for each directory
> I commonly cd into.

Er, perhaps what you want is $CDPATH?

% CDPATH=$HOME/some/deep/directory/tree:/usr/local/some/path
% cd foo

In zsh you can maintain this as an array using lower-case $cdpath, but
both bash and zsh support the colon-separated-string form as $CDPATH
and zsh automatically ties that to the array.

On Dec 4, 12:22am, shawn wilson wrote:
>
> On Tue, Dec 3, 2013 at 11:41 PM, TJ Luoma <luomat@xxxxxxxxx> wrote:
> >
> 
> > function cd {
> >
> > if [ "$#" = "0" ]
> > then
> > # if no arg, go to $HOME
> > chdir "$HOME"
> > else
> > if [ -d "$@" ]
> > then
> > # if the arg is a valid directory, go there
> > chdir "$@"
> 
> elsif [ -n "${$@}" ] && [ -d "${$@}" ]

[ -n "${@}" ] is almost always going to be true here because you've
already started with [ $# = 0 ] up at the top.  You'd have to have
intentionally done

% cd ""

to get $# != 0 and "${@}" empty.  (Also, "elsif" is perl, it's "elif"
in shell.)

} If I could only inject variables into the function like:
} local $cd::foo="/usr/local/foo"

This is exactly what the zstyle mechanism was created for, to simulate
scopes.  The completion system happens to use zstyle in a particular
manner but the mechanism is completely general; you can make up your
own style rules and use them in your own functions.  If you happen to
like the perl double-colon:

zstyle cd::foo target ~/some/deep/directory/tree/foo
zstyle cd::bar target /usr/local/some/path/bar

cd() {
  local dest
  if zstyle -s "cd::$1" target dest && [[ -d $dest ]]
  then chdir $dest
  else chdir "$@"
  fi
}

One interesting thing about zstyle is that you assign values to a
pattern and then look them up with a fixed context (like a package
scope) that is matched against the pattern in order of decreasing
specificity, so you can make a fallback case with (in this example)
the pattern "cd::*".

Another other interesting thing is that the value can be eval'd in
the context where the style is looked up, so you can refer to the
local variables of the calling function.  Combining these gives us
a nifty fail-safe for the function above:

zstyle -e 'cd::*' target 'reply=( $HOME/${1-.} )'

(The single-quoting is important.)

In this example, this means that any argument to "cd" (forming the
context "cd::$1") for which you have not defined a specific "target"
zstyle, has $HOME prefixed to it, and when $1 is empty the result
is "$HOME/." so "cd" with no argument changes to the home directory.

Of course you get nearly all of this for free with $CDPATH, so you
probably don't need to go to all that trouble, especially if you want
something that also works in bash.



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