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

zsh widget to resolve symlinks



Here's a working version of the widget to expand a shell word into its
canonical path.

It turns out copy-region-as-kill doesn't use killring but rather
CUTBUFFER.  This is a little confusing!

I'm also surprised that CUTBUFFER persists invocations of zle, yet it
isn't set in the top-level interactive shell.  If I don't reset
CUTBUFFER as the first thing I do, invoking the widget for a second time
on an empty command line inserts the result of the previous invocation,
for example:
> ls .zshrc<^X />
expands to
> ls /home/michael/etc/zshrc

but then
> <^X />
expands to
> /home/michael/etc/zshrc

The only problem I'm aware of with this version is where the file name
contains spaces.  I can't think of a way to easily do this, since we
have the copy-prev-shell-word widget, but no equivalent
kill-prev-shell-word that I would need to update the display.  (I could
get the word, but how would I overwrite it with its real path?)

expand-word-path ()
{
        CUTBUFFER=
        zle backward-word
        zle set-mark-command
        zle forward-word
        zle copy-region-as-kill
        local word=$CUTBUFFER

        local realpath=$(realpath $word 2>/dev/null)
        if test -n "$realpath"
        then
                zle backward-kill-word
                zle -U "$realpath"
        fi
}



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