6

When I'm using vim in a terminal, it uses the PATH I have set, and I can run programs and scripts located in my ~/bin folder using !

For example

:! some_bash_script.sh

When I try this in MacVim, it doesnt find my personal shell scripts. How can I fix this?

I've set my shell in my ~/.vimrc like so:

set shell=/bin/zsh

but that doesn't seem to help. I did notice that if I launch MacVim from the command line, it works as expected, i assume because the shell's environment is the environment in which MacVim ends up running in.

e.g.

$ open -a MacVim.app

Is there a better way to do this? I like launching MacVim using Spotlight, but it doesnt work if I do it that way, only via Terminal.

NOTE: This is similar to this question, but that was for vim, not gvim or MacVim

  • 2
    superuser.com/q/31353/358509 has MacVim solutions for the PATH. – chicks Mar 29 '18 at 18:11
  • 1
    Is this about zsh or bash? – muru Mar 30 '18 at 1:31
  • I agree with muru's question, and will also say that either shell will read different dotfiles (and thus potentially create different $PATHs) depending on whether it's launched as a login shell, etc. :echo $PATH in MacVim will tell you exactly what $PATH it's seeing, but I'm sure that won't give you additional info. You'll probably need to set your path in a shell dotfile that's read regardless of how the shell is launched. – brhfl Mar 30 '18 at 2:02
  • The answer i posted here allows you to use whatever shell you use (as it uses whatever you have set in Terminal). – Brad Parks Mar 31 '18 at 22:41
2

I answered a similar question here. The problem is that zsh (and other shells) only runs ~/.zshrc for interactive shells and ~/.zprofile for "login" shells.

To force running commands as a login shell, add the -l flag to either the shell option, or the shellcmdflag option. That is (in your ~/.gvimrc),

set shell=/bin/zsh\ -l
" or
set shellcmdflag=-l\ -c

You need to escape the spaces because of how the set command works. Putting it in your ~/.gvimrc will prevent $PATH from growing when running it from the terminal.

  • hey! I tried this, but couldnt get it to work. I put it in my ~/.vimrc, ~/.gvimrc and the system files used by MacVim, but no difference in executing programs. I tried it with both lines, to no effect (ie - it couldnt find anything I had in my ~/bin folder, which is in my path) – Brad Parks May 17 '18 at 11:14
  • Worth noting that $ZDOTDIR/.zshenv is sourced for non-interactive shells, but I wouldn't really recommend customizing $PATH in there. – brhfl May 17 '18 at 13:06
  • Where do you define your path? :! runs commands non-interactively, so you should define $PATH in .zprofile, not .zshrc. Otherwise, I'm out of ideas. I tested and implemented this fix just before writing the answer. – jladan May 18 '18 at 14:32
  • @brhfl why not? – Bluu Nov 21 at 23:37
2

So far, my best solution is to launch MacVim using an alternate application launcher, which you can create using AppleScript like so:

  1. Create an apple script like by Running "Script Editor", clicking "New Document", and supplying the following:

    tell application "Terminal"
         do script "open -a MacVim.app; exit"
         delay 5
        quit
    end tell
    
  2. Export that as an application, by

    • choosing "File | Export"
    • choose "File Format" as "Application"
    • Click "Save", and call it whatever you like. I called mine MacVimLauncher.app, and I saved it under Applications
  3. Run that instead of MacVim.app.

Note: If you have MacVim running, you have to shut it down before trying this out. I've had a similar problem with Eclipse in the past, and this should solve that issue as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.