zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: directory alias
Date: Tue, 03 Dec 2013 23:21:24 -0800	[thread overview]
Message-ID: <131203232124.ZM30287@torch.brasslantern.com> (raw)
In-Reply-To: <CAH_OBidDmzECfgLVcEUmRj2+bean+WjuBvgiPV6SoE9i0kPfKA@mail.gmail.com>
In-Reply-To: <CAH_OBicWiE0d+YVBu_+nR+X8Lmc_SX4+_K0xRzh=b+o9GygUNg@mail.gmail.com>

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@gmail.com> 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.


  reply	other threads:[~2013-12-04  7:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-04  3:21 shawn wilson
2013-12-04  4:41 ` TJ Luoma
2013-12-04  5:22   ` shawn wilson
2013-12-04  7:21     ` Bart Schaefer [this message]
2013-12-04 15:25   ` Christoph (Stucki) von Stuckrad
2013-12-06  7:25     ` shawn wilson
2013-12-04  7:06 ` Dominik Vogt

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=131203232124.ZM30287@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).