From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4202 invoked by alias); 4 Dec 2013 07:21:19 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 18195 Received: (qmail 432 invoked from network); 4 Dec 2013 07:21:13 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 From: Bart Schaefer Message-id: <131203232124.ZM30287@torch.brasslantern.com> Date: Tue, 03 Dec 2013 23:21:24 -0800 In-reply-to: Comments: In reply to shawn wilson "directory alias" (Dec 3, 10:21pm) References: In-reply-to: Comments: In reply to shawn wilson "Re: directory alias" (Dec 4, 12:22am) X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: Zsh Users Subject: Re: directory alias MIME-version: 1.0 Content-type: text/plain; charset=us-ascii 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 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.