You are not logged in.

#1 2021-05-17 16:29:21

Flemur
Member
Registered: 2012-05-24
Posts: 72

zsh function alias issue [SOLVED]

Edit: Solution from Seth: https://bbs.archlinux.org/viewtopic.php … 4#p1973484 (this thread)

Hi, I have some zsh aliases (e.g. 'data') to speed up 'cd'-ing to common directories, but they produce what looks like an incorrect error message:

(anon):cd: no such file or directory: /mnt/DATA/down*

when the directory /mnt/DATA/download exists

It seems like zsh won't expand the * ... sometimes.

Prompts below are B=bash, Z=zsh followed by $PWD - some blank lines inserted for readability.

Z:/home/username : alias data 
data='(){ cd /mnt/DATA/$1}'

Z:/home/username : data        # works
Z:/mnt/DATA : data download  # works

Z:/mnt/DATA/download : cd
Z:/home/username : data down*           # doesn't work
(anon):cd: no such file or directory: /mnt/DATA/down*  

Z:/home/username : cd /mnt/DATA/down*  # I can cd to the 'no such directory'...
Z:/mnt/DATA/download : 

Now switch to bash, where 'data' is defined differently but '*' works:

Z:/home/username : bash
B:/home/username : alias data
alias data='. ddata'

B:/home/username : cat `which ddata`
#!/bin/bash
cd:/mnt/DATA/$1

B:/home/username : data down*
B:/mnt/DATA/download : 
B:/mnt/DATA/download : data down*/tem*
B /mnt/DATA/download/temp : 

Aliasing 'data' to the same thing in zsh didn't work, with either '#!/bin/bash' or '#!/bin/zsh', giving the same
'no such file or directory: /mnt/DATA/down*' error.

zsh 5.8 (x86_64-pc-linux-gnu)

Last edited by Flemur (2021-05-18 11:41:48)


"If you do not change direction, you may end up where you are heading." -- L.T.

Offline

#2 2021-05-17 16:36:07

Scimmia
Fellow
Registered: 2012-09-01
Posts: 13,729

Re: zsh function alias issue [SOLVED]

That appears to be a function, not an alias. The shell would expand the glob before passing it to the function.

Edit: Actually, I'm wrong. It's passing it just fine, but the zsh one is in single quotes, so it won't expand.

Last edited by Scimmia (2021-05-17 16:39:40)

Offline

#3 2021-05-17 17:15:33

Flemur
Member
Registered: 2012-05-24
Posts: 72

Re: zsh function alias issue [SOLVED]

Scimmia wrote:

That appears to be a function, not an alias. The shell would expand the glob before passing it to the function.

Edit: Actually, I'm wrong. It's passing it just fine, but the zsh one is in single quotes, so it won't expand.

With similar aliases, zsh won't do what I can do in bash:

B /home/username : alias data
alias data='. ddata'

B /home/username : cat `which ddata`
#!/bin/bash
cd /mnt/DATA/$1

B /home/username : data down*
B /mnt/DATA/download : data dow*/te*
B /mnt/DATA/download/temp : 

In zsh, change 'data' alias so it's like in bash, above, but get the same error:

Z:/home/username : alias data
data='. zddata'

Z:/home/username : cat `which zddata`
#!/bin/ksh
cd /mnt/DATA/$1

Z:/home/username : data down*
/home/username/bin/zddata:cd:2: no such file or directory: /mnt/DATA/down*
Z:/home/username :

Is there a way to make this work in zsh?

This works, but I'm trying to avoid typing /mnt/DATA all the time:

Z:/home/username : cd /mnt/DATA/do*/te*
Z:/mnt/DATA/download/temp : 

"If you do not change direction, you may end up where you are heading." -- L.T.

Offline

#4 2021-05-17 18:01:26

Flemur
Member
Registered: 2012-05-24
Posts: 72

Re: zsh function alias issue [SOLVED]

Semi-solution: make smarter use of zsh...

Z:/home/username : alias data
data='(){ cd /mnt/DATA/$1}'

Z:/home/username : data;do*/te*
Z:/mnt/DATA/download/temp : 

I'll probably cuss a bunch until I get used to typing the ';'

You can also enter the full dir names:
$ data download/temp
Z:/mnt/DATA/download/temp :

Last edited by Flemur (2021-05-17 18:04:40)


"If you do not change direction, you may end up where you are heading." -- L.T.

Offline

#5 2021-05-17 18:22:14

CarbonChauvinist
Member
Registered: 2012-06-16
Posts: 413
Website

Re: zsh function alias issue [SOLVED]

Maybe also look into fzf and its builtin key-bindings (alt-c) for quick fuzzy-search assisted directory jumping?


"the wind-blown way, wanna win? don't play"

Offline

#6 2021-05-18 10:45:54

lmn
Member
Registered: 2021-05-09
Posts: 93
Website

Re: zsh function alias issue [SOLVED]

Flemur wrote:

I'll probably cuss a bunch until I get used to typing the ';'

Maybe looking into the CDPATH environment variable can save you a lot of swearing.

Offline

#7 2021-05-18 11:29:49

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,527

Re: zsh function alias issue [SOLVED]

alias data='cd /mnt/DATA; HOME=. cd'

You move to the target directory first, then handle the globbing as usual.
Setting the HOME environment to "cd" takes care of the emtpy case (otherwise "cd" would change to ~)

Offline

#8 2021-05-18 11:44:56

Flemur
Member
Registered: 2012-05-24
Posts: 72

Re: zsh function alias issue [SOLVED]

seth wrote:
alias data='cd /mnt/DATA; HOME=. cd'

You move to the target directory first, then handle the globbing as usual.
Setting the HOME environment to "cd" takes care of the emtpy case (otherwise "cd" would change to ~)

That works great, thanks!  SOLVED.

Edit: also works in bash & ksh.

Last edited by Flemur (2021-05-18 15:54:13)


"If you do not change direction, you may end up where you are heading." -- L.T.

Offline

Board footer

Powered by FluxBB