symlinked mvim script can't find original (with solution)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

symlinked mvim script can't find original (with solution)

Lance E Sloan
The problem:  If MacVim is not installed in one of the common directories and the mvim script called from the shell prompt is actually a symbolic link (symlink) to the original mvim script, the script will probably fail with the error message:

  Sorry, cannot find MacVim.app.  Try setting the VIM_APP_DIR
  environment variable to the directory containing MacVim.app.

As the error message says, one could set the VIM_APP_DIR environment variable to resolve this, but I've noticed a small change to the mvim script that can eliminate the need for that variable in many cases.

The background:  I don't like to install non-Apple applications in /Applications if I can avoid it.  I like to install them in /Local/Applications instead.  When I installed MacVim, I did that.   The app and the mvim script are in a MacVim folder, so that their full paths are:

  /Local/Applications/MacVim/MacVim.app
  /Local/Applications/MacVim/mvim

I have a "bin" directory in my home directory and $HOME/bin is in my path.  To add the mvim script to my path, I created a symlink to the mvim script in its installation directory.  That is:

  $HOME/bin/mvim -> /Local/Applications/MacVim/mvim

When I call the mvim script, I get the error mentioned above because the correct directory is not returned from this link of the mvim script:

  myDir="`dirname "$0"`"

myDir ends up with the value of $HOME/bin, which is not where MacVim.app is installed.  A possible solution would be to also add a symlink to MacVim.app in $HOME/bin, but that doesn't feel "right".

My solution:  Replace the setting of myDir in the mvim script with this:

  myDir=$(dirname "$(readlink "$0")")
  if [ -z "$myDir" ]
  then
    myDir="`dirname "$0"`"
  fi

This uses the "readlink" utility to try to read the original file to which the current script name links.  If the result is an empty string (probably because the script name probably wasn't a symlink), fall back to using the current script name, as before

This is a good solution, because if these additional lines were added to the distributed mvim script, it would eliminate the need for more users to edit the script or set the VIM_APP_DIR environment variable.

I thought of posting this as a bug to the MacVim issue tracker, but since it feels more like an enhancement or feature request, I followed the instructions to post about it here first.

--
--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_mac" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
Reply | Threaded
Open this post in threaded view
|

Re: symlinked mvim script can't find original (with solution)

Tony Mechelynck
On Friday, August 23, 2013 9:35:48 PM UTC+2, Lance E Sloan wrote:

> The problem:  If MacVim is not installed in one of the common directories and the mvim script called from the shell prompt is actually a symbolic link (symlink) to the original mvim script, the script will probably fail with the error message:
>
>   Sorry, cannot find MacVim.app.  Try setting the VIM_APP_DIR
>   environment variable to the directory containing MacVim.app.
>
> As the error message says, one could set the VIM_APP_DIR environment variable to resolve this, but I've noticed a small change to the mvim script that can eliminate the need for that variable in many cases.
>
> The background:  I don't like to install non-Apple applications in /Applications if I can avoid it.  I like to install them in /Local/Applications instead.  When I installed MacVim, I did that.   The app and the mvim script are in a MacVim folder, so that their full paths are:
>
>   /Local/Applications/MacVim/MacVim.app
>   /Local/Applications/MacVim/mvim
>
> I have a "bin" directory in my home directory and $HOME/bin is in my path.  To add the mvim script to my path, I created a symlink to the mvim script in its installation directory.  That is:
>
>   $HOME/bin/mvim -> /Local/Applications/MacVim/mvim
>
> When I call the mvim script, I get the error mentioned above because the correct directory is not returned from this link of the mvim script:
>
>   myDir="`dirname "$0"`"
>
> myDir ends up with the value of $HOME/bin, which is not where MacVim.app is installed.  A possible solution would be to also add a symlink to MacVim.app in $HOME/bin, but that doesn't feel "right".
>
> My solution:  Replace the setting of myDir in the mvim script with this:
>
>   myDir=$(dirname "$(readlink "$0")")
>   if [ -z "$myDir" ]
>   then
>     myDir="`dirname "$0"`"
>   fi
>
> This uses the "readlink" utility to try to read the original file to which the current script name links.  If the result is an empty string (probably because the script name probably wasn't a symlink), fall back to using the current script name, as before
>
> This is a good solution, because if these additional lines were added to the distributed mvim script, it would eliminate the need for more users to edit the script or set the VIM_APP_DIR environment variable.
>
> I thought of posting this as a bug to the MacVim issue tracker, but since it feels more like an enhancement or feature request, I followed the instructions to post about it here first.

Instead of a softlink ~/mvim -> /Local/Applications/MacVim/mvim, try adding /Local/Applications/MacVim itself to your $PATH (at the end, and with a colon separator, I suppose). The shell will then find the original mvim script at its actual location without the need for a symlink.


Best regards,
Tony.
--
If a man had a child who'd gone anti-social, killed perhaps, he'd still
tend to protect that child.
                -- McCoy, "The Ultimate Computer", stardate 4731.3

--
--
You received this message from the "vim_mac" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_mac" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.