Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Collapse a file path to the shortest representation



On 5/13/22, Zach Riggle <zachriggle@xxxxxxxxx> 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




Messages sorted by: Reverse Date, Date, Thread, Author