zsh-users
 help / color / mirror / code / Atom feed
* Collapse a file path to the shortest representation
@ 2022-05-13 17:53 Zach Riggle
  2022-05-13 18:03 ` Mikael Magnusson
  0 siblings, 1 reply; 3+ messages in thread
From: Zach Riggle @ 2022-05-13 17:53 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1078 bytes --]

Is there a ZSH standard way to collapse a file path to its shortest
representation that would be idempotent per Chapter 14.7?

For example, one might

echo "$filepath" | sed -E "s|$HOME/?|~/|"
echo "$filepath" | sed -E "s|$HOME/?|\$HOME/|"


In order to turn

/home/username/bar/baz → ~/bar/baz
/home/username/bar/baz → $HOME/bar/baz


To achieve this effect.

From the documentation, "14.7 Filename Expansion" has a lot of ways that
paths can be expanded, including...

14.7.2
... the path is checked to see if it has a named directory as its prefix.
If so, then the prefix portion is replaced with a ‘~’ followed by the name
of the directory ....


Is there a standardized way to access this expansion with e.g.
/path/to/somedir → ~SOMEDIR/foo?

I expect this can be implemented manually by parsing the output of "hash
-d" or similar, but I figured it was worth asking if there's an easy
interface to this.

I'm not concerned as much about "14.7.1 Dynamic named directories" but for
completeness it would be neat.

*Zach Riggle*

[-- Attachment #2: Type: text/html, Size: 2197 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Collapse a file path to the shortest representation
  2022-05-13 17:53 Collapse a file path to the shortest representation Zach Riggle
@ 2022-05-13 18:03 ` Mikael Magnusson
  2022-05-18 22:42   ` Zach Riggle
  0 siblings, 1 reply; 3+ messages in thread
From: Mikael Magnusson @ 2022-05-13 18:03 UTC (permalink / raw)
  To: Zach Riggle; +Cc: Zsh Users

On 5/13/22, Zach Riggle <zachriggle@gmail.com> wrote:
> Is there a ZSH standard way to collapse a file path to its shortest
> representation that would be idempotent per Chapter 14.7?
>
> For example, one might
>
> echo "$filepath" | sed -E "s|$HOME/?|~/|"
> echo "$filepath" | sed -E "s|$HOME/?|\$HOME/|"
>
>
> In order to turn
>
> /home/username/bar/baz → ~/bar/baz
> /home/username/bar/baz → $HOME/bar/baz
>
>
> To achieve this effect.
>
> From the documentation, "14.7 Filename Expansion" has a lot of ways that
> paths can be expanded, including...
>
> 14.7.2
> ... the path is checked to see if it has a named directory as its prefix.
> If so, then the prefix portion is replaced with a ‘~’ followed by the name
> of the directory ....
>
>
> Is there a standardized way to access this expansion with e.g.
> /path/to/somedir → ~SOMEDIR/foo?

The D parameter expansion flag does this.

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Collapse a file path to the shortest representation
  2022-05-13 18:03 ` Mikael Magnusson
@ 2022-05-18 22:42   ` Zach Riggle
  0 siblings, 0 replies; 3+ messages in thread
From: Zach Riggle @ 2022-05-18 22:42 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1270 bytes --]

Thanks! It does indeed.

Is there a way to specify appearance?  ~ENV vs $ENV

        ~SOME_ENV_VAR/foo/bar
        $SOME_ENV_VAR/foo/bar

*Zach Riggle*


On Fri, May 13, 2022 at 1:03 PM Mikael Magnusson <mikachu@gmail.com> wrote:

> On 5/13/22, Zach Riggle <zachriggle@gmail.com> wrote:
> > Is there a ZSH standard way to collapse a file path to its shortest
> > representation that would be idempotent per Chapter 14.7?
> >
> > For example, one might
> >
> > echo "$filepath" | sed -E "s|$HOME/?|~/|"
> > echo "$filepath" | sed -E "s|$HOME/?|\$HOME/|"
> >
> >
> > In order to turn
> >
> > /home/username/bar/baz → ~/bar/baz
> > /home/username/bar/baz → $HOME/bar/baz
> >
> >
> > To achieve this effect.
> >
> > From the documentation, "14.7 Filename Expansion" has a lot of ways that
> > paths can be expanded, including...
> >
> > 14.7.2
> > ... the path is checked to see if it has a named directory as its prefix.
> > If so, then the prefix portion is replaced with a ‘~’ followed by the
> name
> > of the directory ....
> >
> >
> > Is there a standardized way to access this expansion with e.g.
> > /path/to/somedir → ~SOMEDIR/foo?
>
> The D parameter expansion flag does this.
>
> --
> Mikael Magnusson
>

[-- Attachment #2: Type: text/html, Size: 2049 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-05-18 22:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-13 17:53 Collapse a file path to the shortest representation Zach Riggle
2022-05-13 18:03 ` Mikael Magnusson
2022-05-18 22:42   ` Zach Riggle

Code repositories for project(s) associated with this 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).