8

I have used "alias ruby=ruby1.9.1", so I can execute my ruby with this:

ruby 123.rb

or

ruby1.9.1 123.rb

But in my vim, I use :!ruby and get /bin/bash: ruby: command not found.

I must use :!ruby1.9.1

How does alias work? Why vim doesn't know it?

Lai Yu-Hsuan
  • 27,509
  • 28
  • 97
  • 164
  • possible duplicate of [vim is not obeying command aliases](http://stackoverflow.com/questions/4642822/vim-is-not-obeying-command-aliases) – Josh Lee Mar 08 '11 at 17:06

3 Answers3

11

When Vim starts a process it makes a system call. It has only inherited the environment variables from your shell if you started it from the shell. But it won't know your bash aliases.

Bash aliases are only a convenience when you enter a command line in the Bash shell. They are expanded by Bash only.

If you want real aliases put symlinks in a private hidden folder, and add that folder to your PATH, or use the alternatives facility.

Benoit
  • 76,634
  • 23
  • 210
  • 236
  • "If you want real aliases put symlinks in a private hidden folder, and add that folder to your PATH." If I want global aliases, where should I put them? /usr/local/bin? – Lai Yu-Hsuan Mar 09 '11 at 03:33
  • @user590083: Either that, or use `/etc/bash.bashrc` to add some variable to your path. – Benoit Mar 09 '11 at 04:37
1

You can try

:set shellcmdflag+=i

to call bass as "interactive" although that does give an annoying message for every shell command executed.

Matteo Riva
  • 24,728
  • 12
  • 72
  • 104
0

Aliases (unlike environment variables) are not inherited by subshells. So if you want an alias always available, you need to set it in your .bashrc file, so every instance of the shell will get it on startup

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • 1
    This does not answer the question, as Vim is not a subshell. – Benoit Mar 08 '11 at 17:03
  • @Benoit: the vim `!` command invokes a subshell. Putting the `alias` in your .bashrc means that the subshell invoked by vim with `!` will get the alias and the command will work properly. – Chris Dodd Sep 04 '13 at 04:48
  • It only gets the aliases when bash is started in interactive mode. You should `:set shellcmdflag` to at least `-i` for that. And `'shell'` must be set to `/bin/bash` also probably. – Benoit Sep 04 '13 at 07:56
  • @Benoit: admittedly, it depends on what shell you are using -- for `csh` you need to put stuff in `.cshrc` and for `bash` you need to set `BASH_ENV=.bashrc` and put it in `.bashrc` (or you can use some other file name if you prefer) – Chris Dodd Sep 04 '13 at 20:07