3

When I try to call an aliased shell command from vim using :! it says

bash: l: command not found

shell returned 127

l is
alias l=ls
(from my .bashrc)

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
Serid
  • 354
  • 6
  • 13
  • @DoktorOSwaldo, yes, this question discusses same problem but it's shorter and has more useful answer with explanation. – Serid Aug 17 '18 at 12:35
  • then add your answer to the original question, if you feel this question is still important, because it is easier to find, mark it as duplicate. I do not think it is the case, googling `vim use .bashrc` brings you the answer in 3 seconds and uses only words from your title. So it clearly seems that you want do help here, and I recognize that, but it is the wrong way to do it. If your answer is better than existing one, put it under the same question. If your question is better, create it and mark it as duplicate. – Doktor OSwaldo Aug 17 '18 at 12:39
  • @DoktorOSwaldo, ok I got it. Marked as duplicate. – Serid Aug 17 '18 at 12:41
  • Perfect, now feel free to post your answer there – Doktor OSwaldo Aug 17 '18 at 12:44

1 Answers1

3

Use the vim command :set shellcmdflag=-ic which tells it to use the -i argument when starting a shell when executing a :! command. The -i argument means interactive (the shell you are commonly using in terminal), and the way it solves the problem is to use the -i argument, which tells bash to read the .bashrc with your aliases (and probably .bash_alises if you have one). But that may not be what you want because this solution leads to weird behaviour, like vim going into the foreground (use 'fg' to recall it).

A better solution is add this line to your .bashrc (or .bash_aiases):

shopt -s expand_aliases

Then all aliases, even in non-interactive shells, will be expanded correctly.

Also, add this to your .vimrc so the aliases file is actually read each time you run a shell command from within vim:

let $BASH_ENV = "~/.bash_aliases"

Original answer: https://stackoverflow.com/a/19819036/6152931

Bash manual (explains the difference between interactive and non-interactive shells).

Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
Serid
  • 354
  • 6
  • 13