dackz | 22 Jul 00:17

Tilde (%~) prompt expansion inconsistent with named directories?

After starting up zsh, if I cd into another home folder, it prints out  
the full path. If I then do "cd ~<tab>", it starts printing out the  
named directory for the home folder.

For example:

$ cd /home/ftp
$ print -P '%~'
/home/ftp
$ cd ~<tab>
$ print -P '%~'
~ftp

I'm guessing this has to do with the named directory cache not being  
populated. Shouldn't the expansion of %~ populate it automatically?

Also, how is a directory determined to be named in prompt expansion?  
If I run "cd ~www-data", "print -P '%~'" prints /var/www instead of  
~www-data. I actually prefer this behavior, but I'm wondering how zsh  
makes the distinction in that case.

Phil Pennock | 22 Jul 00:41

Re: Tilde (%~) prompt expansion inconsistent with named directories?

On 2008-07-21 at 18:18 -0400, dackz wrote:
> I'm guessing this has to do with the named directory cache not being  
> populated. Shouldn't the expansion of %~ populate it automatically?

Debatable.  Arguably the setting of a prompt shouldn't trigger
potentially large amounts of work (reading in the list of users from
LDAP, for instance, with >10k users).

There are arguments either way.

> Also, how is a directory determined to be named in prompt expansion?  

The shortest match wins.

> If I run "cd ~www-data", "print -P '%~'" prints /var/www instead of  
> ~www-data. I actually prefer this behavior, but I'm wondering how zsh  
> makes the distinction in that case.

/var/www is the same length as www-data and shorter than ~www-data; zsh
will take the shortest match.  If ~name is the same length or shorter
than /path then ~name will be used.

It's actually a complete scan through the entire list of all known
names, searching for the one with the "best" difference in length.

-Phil

dackz | 22 Jul 01:18

Re: Tilde (%~) prompt expansion inconsistent with named directories?

On Jul 21, 2008, at 6:41 PM, Phil Pennock wrote:

> On 2008-07-21 at 18:18 -0400, dackz wrote:
>> I'm guessing this has to do with the named directory cache not being
>> populated. Shouldn't the expansion of %~ populate it automatically?
>
> Debatable.  Arguably the setting of a prompt shouldn't trigger
> potentially large amounts of work (reading in the list of users from
> LDAP, for instance, with >10k users).
>
> There are arguments either way.

I think what I really meant to say is that I think it should be  
consistent. I.e., it should either never do it, or always do it  
(perhaps through a setting/option).

At least in my case I had been using my own custom prompt function  
that used $HOME (the abbreviation code stolen from fish) and I noticed  
I could use print -P '%~' instead of doing the tilde replacement  
myself. However, after switching to that, I noticed what seemed to be  
the random behavior of using a named directory. After looking at zsh's  
source code I realized it was using a cache and I had been populating  
the cache in some cases without realizing it.

Anyway, regardless of that, is there a way to populate the cache when  
I load zsh, and is there a way to disable named directory expansion  
for %~? Though I suppose I could just as well use "${PWD/$HOME/~}" in  
the latter case.

(Also, sorry for sending this twice. I meant to reply to the list as  
(Continue reading)

Phil Pennock | 22 Jul 02:00

Re: Tilde (%~) prompt expansion inconsistent with named directories?

On 2008-07-21 at 19:18 -0400, dackz wrote:
> I think what I really meant to say is that I think it should be  
> consistent. I.e., it should either never do it, or always do it  
> (perhaps through a setting/option).
> 
> At least in my case I had been using my own custom prompt function  

In your init function for setting the prompt:
  zmodload -i zsh/parameter
  print ${(k)userdirs} >/dev/null

>             and is there a way to disable named directory expansion  
> for %~? Though I suppose I could just as well use "${PWD/$HOME/~}" in  
> the latter case.

I think ${PWD/$HOME/~} is your best bet.

-Phil

Oliver Kiddle | 22 Jul 07:28

Re: Tilde (%~) prompt expansion inconsistent with named directories?

>   print ${(k)userdirs} >/dev/null

No need to print it to /dev/null. Just use the : command:
   : ${(k)userdirs}

Oliver

Bart Schaefer | 22 Jul 07:44
Gravatar

Re: Tilde (%~) prompt expansion inconsistent with named directories?

On Jul 21,  5:00pm, Phil Pennock wrote:
}
} In your init function for setting the prompt:
}   zmodload -i zsh/parameter
}   print ${(k)userdirs} >/dev/null

The usual idiom would probably be more like

   zmodload -i zsh/parameter && : $userdirs


Gmane