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

Re: Converting relative paths to full



On Tue, Aug 10, 2021 at 2:53 PM Gürkan <seqizz@xxxxxxxxx> wrote:
>
>
> function _edit_command_to_register_full_path {
> emulate -L zsh
>
> REPLACED="$1"
> for element in `echo "$1"`; do
> # Don't care about parameters
> if [[ "$element" == -* ]]; then
> continue
> fi
> FULLPATH="${element:P}"
> if [[ -f $FULLPATH ]] || [[ -d $FULLPATH ]]; then
> REPLACED=`echo "$REPLACED" | sed "s!$element!$FULLPATH!"`
> fi
> done
>  print -Sr -- ${REPLACED%%$'\n'}
>  return 1
> }

That's far more complicated than necessary.

function _edit_command_to_register_full_path {
  emulate -L zsh
  for element in ${(z)1}; do
    # Don't care about parameters
    [[ $element == -* ]] && continue
    FULLPATH="${element:P}"
    # Don't replace when already the full path
    [[ $element == $FULLPATH ]] && continue
    # Don't replace when path doesn't exist
    [[ -f $FULLPATH || -d $FULLPATH ]] || continue
    # [[:space:]] to protect previous replacements
    # (this is still not foolproof for path prefixes)
    set -- "${1/[[:space:]]$element/ $FULLPATH}"
  done
  print -Sr -- ${1%%$'\n'}
  return 1
}




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