$PATH variable not properly set in gvim/MacVim when it is opened from the finder
Asked Answered
D

4

18

I am using MacVim (basically gvim for the mac).

If I open macvim from the command line then my $PATH variable will be properly set.

If I open macvim via point and click with the finder, the $PATH variable will NOT be properly set.

Can anyone give me some insight?

Note: I know at least part of my path is set in ~/.bashrc, but I am not sure where the rest of it is set.


Examples:

If I open macvim from the terminal:

% gvim basic.tex 

And then in MacVim I go:

:!echo $PATH
/opt/local/bin:/opt/local/sbin:/sw/bin:/sw/sbin:/Applications/MacVim.app/Contents/M
acOS:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/texbin:/usr/X11R6/bin 

This is the right path.


When I open the file with the mouse (in finder)

When I go:

:!echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin  

It gives me a little path. Why?

Diver answered 27/7, 2010 at 14:35 Comment(0)
D
8

The place to set environment variables on the Mac for GUI applications (those started via loginwindow, the Finder, etc.) is ~/.MacOSX/environment.plist

Alternately in MacVim you can choose to launch vim processes in a login-shell (look in the preferences).

For more info see this post.

Diver answered 27/7, 2010 at 15:0 Comment(2)
The option to launch vim processes in a login-shell is not available in MacVim 7.3 anymore.Secluded
For an answer that works in 2021, see this approach apple.stackexchange.com/a/289062/33737 .Lilly
O
16

I had this same issue but it only appeared after setting my default shell to zsh like so

export SHELL=/bin/zsh

It seems that there is a bug in the OS X zsh setup. The work around in brief is to merge /etc/zshenv into /etc/zprofile. In my case I didn't have a /etc/zprofile so just moving over the file did the trick:

sudo mv /etc/zshenv /etc/zprofile

This post describes the solution in more detail.

Odoriferous answered 11/9, 2011 at 23:34 Comment(7)
This is the solution mentioned on the official MacVim github wiki: github.com/b4winckler/macvim/wiki/Troubleshooting (under the "For zsh users" heading)Citystate
I think maybe the key is getting the eval /usr/libexec/path_helper -s command out of /etc/zshenv. I find the word "merge" in the answer confusing. Still, very helpful, Thx. Upvote!Scaphoid
Instead of using the system /etc/zprofile you can use your own ~/.zprofileCourtroom
in my case I just moved all my export PATH things from ~/.zshrc into ~/.zprofile the doc says to copy them and do a typeset but there is no needGlutinous
The only thing that worked for me was ln -s ~/.zshrc ~/.zprofileSmart
Since the question mentioned the user's ~/.bashrc file, this answer about zsh doesn't seem very helpful.Lefevre
is it bad if I put this answers code in zprofile and change bashrc to zshrc? it seems to work... but im not sure if this is wrong. I want the path to update dynamically https://mcmap.net/q/655308/-path-variable-not-properly-set-in-gvim-macvim-when-it-is-opened-from-the-finderInterplanetary
E
11

For me, simply creating a new symbolic link from .zprofile to .zshrc did the trick:

ln -s ~/.zshrc ~/.zprofile
Enucleate answered 2/7, 2014 at 23:53 Comment(2)
this works for me. it's a simplest workaround. just change from .zsrrc to .zshrcKetty
Or just define your paths in .zprofile instead of .zshrc.Enucleate
D
8

The place to set environment variables on the Mac for GUI applications (those started via loginwindow, the Finder, etc.) is ~/.MacOSX/environment.plist

Alternately in MacVim you can choose to launch vim processes in a login-shell (look in the preferences).

For more info see this post.

Diver answered 27/7, 2010 at 15:0 Comment(2)
The option to launch vim processes in a login-shell is not available in MacVim 7.3 anymore.Secluded
For an answer that works in 2021, see this approach apple.stackexchange.com/a/289062/33737 .Lilly
V
3

The difference in the PATHs probably has something to do with the difference between a login shell (logging in) and a non-login shell (bringing up a console).

From the bash man page:

When bash is invoked as an interactive login shell ... it looks for ~/.bash_profile...

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc...

What I did to get around this issue was to add the following code to my ~/.bash_profile, telling it to source my ~/.bashrc if it exists:

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi
Valiancy answered 27/7, 2010 at 15:40 Comment(4)
Won't this result in bashrc being loaded twice when it is an interactive shell?Diver
I don't believe that it will get loaded twice. Now that I think about it, this isn't a solution to your problem... This solves the issue where you're logging in remotely (e.g. via ssh) and your ~/.bashrc doesn't get loaded. You're not remotely logging in.Valiancy
But when you open a terminal normally, it will load your .bash_profile then it will load your .bashrc (maybe opposite order). What you are doing will make it; .bash_profile which will load your .bashrc then it will load your .bashrc. Probably this doesn't matter but if you wanted to do a work around, you could have a var $BASHRC_LOADED in your .bashrc and enclose everything in if(not($BASHRC_LOADED)). (sorry bad syntax)Diver
With the system I'm on now (Ubuntu 10.4) when I open a terminal it loads my .bashrc only. If I ssh into the same box, it loads my .bash_profile. I believe this is the way bash usually works. As a side note, for each user, Ubuntu sets up a .profile file (which gets loaded if .bash_profile doesn't exist) that sources your .bashrc if it exists, much like what I mentioned above.Valiancy

© 2022 - 2024 — McMap. All rights reserved.