You're right. I found this way of solving the problem that was successful on my system, but for sure there are more efficient and robust solutions. I was not aware of `realpath` and `basename`: very useful!
So, I'll leave this issue (and maybe the overhaul of the `edit` function, as Stefan said in #4728) in more expert hands like yours. Thanks.
P.S. How to address the problem of the order in the `$PATH` variable?