$PATH in Vim doesn't match Terminal

10

2

I'm using MacVim and when I don't launch it from the Terminal (mvim) its $PATH does not include what I have set in my .bash_profile. It only seems to have the default values, /usr/bin:/bin:/usr/sbin:/sbin.

I'm running OS X 10.5.8. Even if I could set it manually in my .vimrc that would be okay, though I would prefer it to pull from the same place as Terminal. I've tried following what one site suggested, adding let $PATH += /blah/foo:/bar/etc to no avail.

Edit/Solution: See my answer below. MacVim has an option to fix this.

donut

Posted 2009-08-28T13:56:51.687

Reputation: 926

Similar question here

– Brad Parks – 2018-03-30T00:07:36.287

Answers

12

MacVim appears to be getting the $PATH in a way that ignores a regular user's custom dotfiles.

I can't say why they prefer to do it this way, but it appears to be true. In the preferences (under the Advanced tab), you can choose to "Launch Vim processes in a login shell", but it only improves the $PATH slightly for me.

The best I can tell, MacVim uses /usr/libexec/path_helper - which in turn uses the items found (1) in the /etc/paths file and (2) anything additional found in the /etc/paths.d/ directory. Any changes made in a user's home directory (via .profile or the like) is ignored.

In a nutshell, you get the OSX defaults, plus any applications that add themselves to /etc/paths.d. (For example, MacTex does so on my system.) So if you want to adjust your MacVim path, one way would be to add a file with items to /etc/paths.d. (I wouldn't directly edit /etc/paths since then you run the risk of the changes being undone by system updates.)

Thanks for letting me know this. I'm off to file a wishlist bug at MacVim.

Telemachus

Posted 2009-08-28T13:56:51.687

Reputation: 5 695

I totally missed your first paragraph originally: Choosing to "Launch Vim processes in a login shell" did the trick for me. See my answer. – donut – 2010-02-03T09:55:36.537

I would have thought that as well, but I have added a few things '/etc/paths.d/' which show up in Terminal but not with MacVim. – donut – 2009-09-27T05:37:00.893

And actually, there's more in my '/etc/paths' file than what's in MacVim's path. – donut – 2009-09-27T05:47:26.210

@Donut: well, that is odd. On my machine, if I add something to /etc/paths.d, it shows up the next time I boot MacVim. What format are you putting in there? It should be one item per line, I believe, with a $PATH directory on each line (e.g., /usr/texbin). – Telemachus – 2009-09-27T11:17:47.133

7

BTW, the "Launch Vim processes in a login shell" preference is no longer available in MacVim (at least not v7.3). I see that launching MacVim from the Finder gets the wrong path, but launching it from a shell (as "mvim") gets the correct path.

L S

Posted 2009-08-28T13:56:51.687

Reputation: 166

3

It's not sourced from .bash_profile, but you can use this in your .vimrc:

let $PATH=$PATH . ':/blah/foo'

You have to have the : or it won't be a correct PATH variable.

Richard Turner

Posted 2009-08-28T13:56:51.687

Reputation: 397

I just tried it, and this doesn't seem to change the $PATH for MacVim. I also tested it with Vim in the Terminal (both /usr/bin/vim (from Apple) and /opt/local/bin/vim (from MacPorts), and it seems unnecessary. Vim in the terminal already picks up the proper $PATH as sourced by my dot files. – Telemachus – 2009-09-26T12:18:48.093

1I tried this as well to no avail. – donut – 2009-09-27T10:10:28.517

2

I can't believe I didn't see this before. Okay, I can believe it. Anyways, MacVim comes with an option that solves this. Under Preferences just check "Launch Vim processes in a login shell". That's it, it works. Snazzy. Stupid me.

Oops

I accidentally wrote this answer not realizing that Telemachus had already given me the correct answer. Marked his as the answer.

donut

Posted 2009-08-28T13:56:51.687

Reputation: 926

Glad it's sorted – Telemachus – 2010-02-14T15:30:44.650

1

I had no problem adding a file to the paths.d directory. I added a file called mysql, whose contents were:

/usr/local/mysql/bin[Return]

Then I placed this file under the paths.d directory, and changed its ownership to root:wheel. Now the mysql directory is automatically added to my $PATH variable.

I didn't investigate fully, but possibly (a) including the [RETURN] at the end of the line, and/or (b) setting the file ownership to root, might be necessary.

Jan Hettich

Posted 2009-08-28T13:56:51.687

Reputation: 11

1

I ran into this today. MacVim no longer has the "login shell" option, and editing system-wide files for a user-specific setting is sketchy.

Here's a solution using only vim options (see :help :! and :help shell).

In your ~/.vimrc add the lines

set shell=/bin/bash
set shellcmdflag=-l\ -c

This makes bash your default shell from (Mac)Vim, and adds the -l flag (login shell) to commands invoked with :!{cmd}. When executing :shell, bash is started as an "interactive shell that is not a login shell".

To make the $PATH work, you need to have it set in ~/.bash_profile for :!, and ~/.bashrc for :shell. (See INVOCATION in man bash)

UPDATE: This wasn't necessary on my other computer, so there is likely something else that is the root cause.

jladan

Posted 2009-08-28T13:56:51.687

Reputation: 111

1You can do a similar thing for zsh. Define $PATH in .zprofile, and include the -l flag in either set shell or set shellcmdflag. – jladan – 2018-05-16T23:23:36.290

1

This Mac Dev Center article may be of some use.

Lawrence Velázquez

Posted 2009-08-28T13:56:51.687

Reputation: 929

Can't get it to work. Could be doing something wrong. The article suggests adding the 'LSEnvironment' property in an app's 'information property list file' which I assumed to be ~/Library/Preferences/org.vim.MacVim.plist. I made it a dictionary with 'PATH' (also tried 'path' and '$PATH') as the a key and what I want for the path as a string value. Still seems to be pulling PATH from some mysterious source. Screenshot of the plist file

– donut – 2009-09-05T09:42:39.440

1

MacVim will source ~/.zprofile and /etc/profile, so you can put your PATH settings in there.

See https://github.com/b4winckler/macvim/wiki/Troubleshooting#for-zsh-users and this question on Stack Overflow.

(Tested with MacVim version 7.3 (65)).

a paid nerd

Posted 2009-08-28T13:56:51.687

Reputation: 2 803

0

I asked a very similar question On the Vim StackExchange Site and posted an answer there that works for me!

Brad Parks

Posted 2009-08-28T13:56:51.687

Reputation: 1 775

0

I know more about unix than about macs, so the following advice may or may not work. Try putting this your .vimrc file:

! source ~/.bash_profile

That might do the trick, but there's a slight caveat: the .bash_profile is only meant to be sourced once. It may contain some non-idempotent commands (things which should only execute once). Case in point:

$PATH += /blah/foo:/bar/etc

The more that gets called, the bigger and more redundant $PATH gets. However, if that's not such a big deal, the recommendation on unix would be to put the one-time command in ~/.bashrc and run source ~/.bashrc from your .vimrc file.

David Berger

Posted 2009-08-28T13:56:51.687

Reputation: 235

Neither sourcing .bash_profile or .bashrc worked. I tried manually running :source .bash_profile but it just comes up with a "Not an editor command" error for every command in the file and the same with .bashrc. – donut – 2009-08-29T01:58:20.147

sorry...what was I thinking...you might try ! source .bash_profile – David Berger – 2009-08-29T15:05:48.050

Tried both .bash_profile and .bashrc but still no go. Here's what I'm using in my .bash_profile to change PATH: 'PATH=/usr/local/bin:/Applications/Espresso.app/Contents/SharedSupport/Tools/:/Applications/MacVim/:$PATH' And in my .bashrc: 'export PATH=/Applications/MAMP/bin/php5/bin:$PATH'. And it's .bash_profile that actually affects my Terminal sessions. – donut – 2009-08-30T13:57:58.627