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

Re: Widget of the day: expand absolute path



On 02/28/2014 04:56 AM, Peter Stephenson wrote:


Peter,

It's probably obvious to most, but not at my level. How does one 'use' this? Where does the code go? What activates it?
Sometimes I want to expand the name of a file on the command line to its
absolute path with all symbolic links removed, e.g. because I know the
symbolic link is volatile.  I can do that with glob qualifiers, but if
I'm likely to want to refer to the file in the ZLE history, or I want to
edit the path to refer to a related file in the resulting target path,
it's quite convenient to expand it immediately. This widget does that.

You can get some of this effect by adding glob qualifiers and using
expansion, but there's no glob qualifier to insert directory names,
which I wanted for neatness.  It doesn't really make sense to have
one, in fact, because the result of a glob is usually passed directly
to a programme which wouldn't be able to re-expand the name.

It also serves as a model for how to write a front-end to
modify-current-argument to do similar file-related tricks.  I should
probably try to enhance modify-current-argument in some way to do this
without needing an auxiliary function.


#START
# expand-absolute-path
# This is a ZLE widget to expand the absolute path to a file,
# using directory naming to shorten the path where possible.

emulate -L zsh
setopt extendedglob cbases

autoload -Uz modify-current-argument

if (( ! ${+functions[glob-expand-absolute-path]} )); then
   glob-expand-absolute-path() {
     local -a files
     files=(${~1}(:A))
     REPLY=${(D)files[1]}
   }
fi

modify-current-argument glob-expand-absolute-path
#END


pws




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