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

Re: [PATCH] feat(prompt): %p for percent encoded pwd



On Sun, Aug 11, 2024 at 10:20 AM Liao Junxuan <mikeljx@xxxxxxx> wrote:
>
> This adds a new escape %p to allow the user to set an osc 8 sequence [1] in
> their prompt.

I use this function to URL-encode things in zsh:

    function url-encode() {
      emulate -L zsh -o extended_glob
      local MATCH MBEGIN MEND
      typeset -g
REPLY=${1//(#m)[^a-zA-Z0-9"\/:_.-!'()~"]/%${(l:2::0:)$(([##16]#MATCH))}}
    }

(Please forgive a forced line break before "REPLY".)

Example:

    % pwd
    /home/romka/hello world

    % url-encode $PWD && print -r -- $REPLY
    /home/romka/hello%20world

It's suitable for generating OSC 8 and the like. I don't think I ever
wished zsh had a dedicated escape for URL-encoded $PWD.

Roman




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