* directory alias
@ 2013-12-04 3:21 shawn wilson
2013-12-04 4:41 ` TJ Luoma
2013-12-04 7:06 ` Dominik Vogt
0 siblings, 2 replies; 7+ messages in thread
From: shawn wilson @ 2013-12-04 3:21 UTC (permalink / raw)
To: Zsh Users
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. What I want is a way to do:
cd foo
and it go to ~/some/deep/directory/tree/foo
and
cd bar
and it go to /usr/local/some/path/bar
Is there some zsh-ism (or better bash-ism that also works in zsh so
that this works on systems I maintain without zsh) to do this without
symlinks?
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: directory alias
2013-12-04 3:21 directory alias shawn wilson
@ 2013-12-04 4:41 ` TJ Luoma
2013-12-04 5:22 ` shawn wilson
2013-12-04 15:25 ` Christoph (Stucki) von Stuckrad
2013-12-04 7:06 ` Dominik Vogt
1 sibling, 2 replies; 7+ messages in thread
From: TJ Luoma @ 2013-12-04 4:41 UTC (permalink / raw)
To: shawn wilson; +Cc: Zsh Users
[-- Attachment #1: Type: text/plain, Size: 1293 bytes --]
I make variables for directories I use a lot
db="$HOME/Dropbox"
bin="$db/bin"
and then I can use that to either move files (mv foo $bin) or 'cd' to them
just by typing the variable "$db" and hitting enter.
But if that's what you mean by a "universal alias", then you'd have to do
something else.
You could make your own `cd` function
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 "$@"
else
case "$@" in
bar)
chdir /usr/local/some/path/bar
;;
foo)
chdir ~/some/deep/directory/tree/foo
;;
*)
echo "chdir: no such file or directory: $@"
return 1
;;
esac
fi
fi
}
On Tue, Dec 3, 2013 at 10:21 PM, shawn wilson <ag4ve.us@gmail.com> wrote:
> 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. What I want is a way to do:
> cd foo
> and it go to ~/some/deep/directory/tree/foo
> and
> cd bar
> and it go to /usr/local/some/path/bar
>
> Is there some zsh-ism (or better bash-ism that also works in zsh so
> that this works on systems I maintain without zsh) to do this without
> symlinks?
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: directory alias
2013-12-04 4:41 ` TJ Luoma
@ 2013-12-04 5:22 ` shawn wilson
2013-12-04 7:21 ` Bart Schaefer
2013-12-04 15:25 ` Christoph (Stucki) von Stuckrad
1 sibling, 1 reply; 7+ messages in thread
From: shawn wilson @ 2013-12-04 5:22 UTC (permalink / raw)
To: TJ Luoma; +Cc: Zsh Users
Ah, of course. Thanks for the idea... I feel stupid.
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 "${$@}" ]
then
chdir "${$@}"
else
> echo "chdir: no such file or directory: $@"
> return 1
> ;;
> esac
> fi
> }
If I could only inject variables into the function like:
local $cd::foo="/usr/local/foo"
so I could do that from the command line and wouldn't have any
namespace conflicts, that'd be perfect. But, I can live with global I
guess....
>
>
>
>
> On Tue, Dec 3, 2013 at 10:21 PM, shawn wilson <ag4ve.us@gmail.com> wrote:
>>
>> 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. What I want is a way to do:
>> cd foo
>> and it go to ~/some/deep/directory/tree/foo
>> and
>> cd bar
>> and it go to /usr/local/some/path/bar
>>
>> Is there some zsh-ism (or better bash-ism that also works in zsh so
>> that this works on systems I maintain without zsh) to do this without
>> symlinks?
>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: directory alias
2013-12-04 3:21 directory alias shawn wilson
2013-12-04 4:41 ` TJ Luoma
@ 2013-12-04 7:06 ` Dominik Vogt
1 sibling, 0 replies; 7+ messages in thread
From: Dominik Vogt @ 2013-12-04 7:06 UTC (permalink / raw)
To: zsh-users
On Tue, Dec 03, 2013 at 10:21:53PM -0500, shawn wilson wrote:
> What I want is a way to do:
> cd foo
> and it go to ~/some/deep/directory/tree/foo
> and
> cd bar
> and it go to /usr/local/some/path/bar
>
> Is there some zsh-ism (or better bash-ism that also works in zsh so
> that this works on systems I maintain without zsh) to do this without
> symlinks?
Named directories.
$ hash -d foo=~/some/deep/directory/tree/foo
$ hash -d bar=/usr/local/some/path/bar
cd ~foo
cd ~bar
Personally I prefer aliases like
alias cfoo "cd ~/some/deep/directory/tree/foo"
as I don't like to have the shortcuts for manually named dirs in
the prompt. And actually finding these cd commands from the
history works automatically without having to define aliases or
hash table entries manually.
Ciao
Dominik ^_^ ^_^
--
Dominik Vogt
IBM Germany
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: directory alias
2013-12-04 5:22 ` shawn wilson
@ 2013-12-04 7:21 ` Bart Schaefer
0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2013-12-04 7:21 UTC (permalink / raw)
To: Zsh Users
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.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: directory alias
2013-12-04 4:41 ` TJ Luoma
2013-12-04 5:22 ` shawn wilson
@ 2013-12-04 15:25 ` Christoph (Stucki) von Stuckrad
2013-12-06 7:25 ` shawn wilson
1 sibling, 1 reply; 7+ messages in thread
From: Christoph (Stucki) von Stuckrad @ 2013-12-04 15:25 UTC (permalink / raw)
To: zsh-users
On Tue, 03 Dec 2013, TJ Luoma wrote:
> > I commonly cd into. What I want is a way to do:
> > cd foo
> > and it go to ~/some/deep/directory/tree/foo
> > and
> > cd bar
> > and it go to /usr/local/some/path/bar
IF all the dirs you "cd into" are different, simply
mention all their parents in CDPATH=/.../...1:/.../...2:...:...
and 'cd' will walk these until it finds the given dir.
If you have names more than once, left to right will win.
Stucki
--
Christoph von Stuckrad * * |nickname |Mail <stucki@mi.fu-berlin.de> \
Freie Universitaet Berlin |/_*|'stucki' |Tel(Mo.,Mi.):+49 30 838-75 459|
Mathematik & Informatik EDV |\ *|if online| (Di,Do,Fr):+49 30 77 39 6600|
Takustr. 9 / 14195 Berlin * * |on IRCnet|Fax(home): +49 30 77 39 6601/
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: directory alias
2013-12-04 15:25 ` Christoph (Stucki) von Stuckrad
@ 2013-12-06 7:25 ` shawn wilson
0 siblings, 0 replies; 7+ messages in thread
From: shawn wilson @ 2013-12-06 7:25 UTC (permalink / raw)
To: Zsh Users
Thank y'all for CDPATH - I didn't know about that and it's real cool.
However, after using it for a few days, I realize I want something
like command-t (vim) for the command line.
I just found two promising projects:
https://github.com/clvv/fasd
https://github.com/joelthelion/autojump
The later is probably not what I'm looking for (a wrapper around cd).
However, the former (fasd) is promising. It works with both bash and
zsh and works with other commands. It does not, however, seem to have
been maintained for the past year (and has newer issues).
I also need to look farther into zstyle. However, since I spend a good
portion of my day on boxes in accounts where I'd probably get shot (or
more like yelled at :) ) if i changed the shell to zsh, if I went that
route, I'd still want a solution for bash (sorta OT here, but a
concern none the less).
So, that's where I'm at. And again, thanks for the help.
On Wed, Dec 4, 2013 at 10:25 AM, Christoph (Stucki) von Stuckrad
<stucki@mi.fu-berlin.de> wrote:
> On Tue, 03 Dec 2013, TJ Luoma wrote:
>
>> > I commonly cd into. What I want is a way to do:
>> > cd foo
>> > and it go to ~/some/deep/directory/tree/foo
>> > and
>> > cd bar
>> > and it go to /usr/local/some/path/bar
>
> IF all the dirs you "cd into" are different, simply
> mention all their parents in CDPATH=/.../...1:/.../...2:...:...
> and 'cd' will walk these until it finds the given dir.
> If you have names more than once, left to right will win.
>
> Stucki
>
> --
> Christoph von Stuckrad * * |nickname |Mail <stucki@mi.fu-berlin.de> \
> Freie Universitaet Berlin |/_*|'stucki' |Tel(Mo.,Mi.):+49 30 838-75 459|
> Mathematik & Informatik EDV |\ *|if online| (Di,Do,Fr):+49 30 77 39 6600|
> Takustr. 9 / 14195 Berlin * * |on IRCnet|Fax(home): +49 30 77 39 6601/
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2013-12-06 7:25 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-04 3:21 directory alias shawn wilson
2013-12-04 4:41 ` TJ Luoma
2013-12-04 5:22 ` shawn wilson
2013-12-04 7:21 ` Bart Schaefer
2013-12-04 15:25 ` Christoph (Stucki) von Stuckrad
2013-12-06 7:25 ` shawn wilson
2013-12-04 7:06 ` Dominik Vogt
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).