4

When I start a terminal with the :terminal command, I get this output:

++ parse_git_branch
bash: parse_git_branch: command not found
~ $

parse_git_branch is a bash function defined in my .bash_profile that I use for a custom prompt:

export PS1="\\[\\033[33;1m\\]\\w\\[\\033[m\\]\$(parse_git_branch || echo unknown)\\[\\033[00m\\] $ "

Why does neovim not read the .bash_profile? How can I fix this?

1 Answer 1

6

I'll take a crack at this though I'm purely a Vim user.

If you look at man bash you'll find the rules bash uses to load the various startup files.

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order,

So you need to start a login shell...

A login shell is one whose first character of argument zero is a -, or one started with the --login option.

So adding --login to the bash command that neovim uses should do what you want. (Assuming it's the same as vim that can be specified with something like let &shell='/usr/bin/bash --login' in neovim init file.)

Alternatively, --rcfile path-to-bash-profile can probably be used instead of --login. The difference between the two is subtle and for most in-vim use they are effectively equivalent. I mention both in the unlikely event someone is doing something sensitive to whether the terminal is a login shell or not in which case they can try the alternative.

5
  • "I'll not go into whether a login shell is appropriate for this usage or whether you should be putting functions in .bash_profile" -- I would appreciate your advice on those things regardless
    – Jonah
    Apr 23, 2018 at 0:37
  • 1
    Don't worry too much about it, I wasn't suggesting you were doing something seriously wrong. But, for example, a function defined in .bash_profile won't be visible in an interactive subshell (e.g. run bash from bash command line). .bashrc is executed for that scenario. Common pattern is to source .bashrc from .bash_profile...but that's not a perfect solution either. Come to think of it I might just remove that from my answer...likely to be a distraction more than anything.
    – B Layer
    Apr 23, 2018 at 1:00
  • FYI, I'm glad you added the --rcfile note, because the --login version broke an unrelated plugin, but the rcfile version did not.
    – Jonah
    Apr 23, 2018 at 5:00
  • Ah, perhaps my original warning about login shells and bash_profile was prudent. What's the plugin and how did it break, if I may?
    – B Layer
    Apr 23, 2018 at 9:35
  • fugitive broke. Gstatus command was showing an empty window
    – Jonah
    Apr 23, 2018 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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