0

I wanted to write some scripts and have MacVim call some bash commands. I have some aliases and wanted vim to be able to call them, so I knew I needed a login shell, just didn't know how to make vim use one.

I read this: https://stackoverflow.com/questions/4642822/vim-is-not-obeying-command-aliases (which didn't work for my MacVim); and this: https://stackoverflow.com/questions/4642822/vim-is-not-obeying-command-aliases which seemed like a good idea.

So I opened my .vimrc and put this in: set shell=/bin/bash\ -l.

Now something very strange is happening: I have an alias in my .bash_profile, like so:

alias mytest='echo "TEST!!"'

My .bashrc sources .bash_profile, so I know it shouldn't be a matter of where the alias is set. If I go inside vim and run: :! alias mytest

I see this:

alias mytest='echo "TEST!!"'

Press ENTER or type command to continue

If I run :! mytest, though, this is what I get:

/bin/bash: mytest: command not found

shell returned 127

Press ENTER or type command to continue

So I'm all out of ideas about how this can be happening. How come when I run "alias" the alias is there, but when I run the alias itself, bash doesn't recognize it?

I appreciate any insight anyone can give me on this matter.

Thank you.

UPDATE:

I now tried changing my .vimrc line to:

set shell=/bin/bash\ -li

In order to make the shell interactive as well as being a login shell, and it worked.

So I change my question a bit: Why does this make sense? As I understand it, a login shell runs my .bash_profile, and thus loads my aliases. Why is it not sufficient?

2 Answers 2

2

Aliases are intended as user shorthand, so they only really make sense in interactive shells. Therefore, as the bash man page says: "Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt (see the description of shopt under SHELL BUILTIN COMMANDS below)." I suppose you could add shopt -s expand_aliases to your .bash_profile, but that might cause unexpected behavior in other sorts of non-interactive shells...

1
  • Damn, had not gotten to that part of the bash man page. Thanks a lot for your answer!
    – malvim
    Apr 7, 2011 at 15:46
0

This should work: :! bash -ic 'mytest; exit'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .