与从vim执行的命令相同的问题是没有识别bash命令别名,但是那里的解决方案不起作用.
我在〜/ .vimrc中设置了这些变量:
set shellcmdflag=-ic
set shell=/bin/bash\ -i
Run Code Online (Sandbox Code Playgroud)
我的〜/ .bash_aliases中有别名:
rgr() { if [ ! -z "$2" ]; then grep -rI --exclude=\*.svn\* "$1" * --include=$2 ; else grep -rI --exclude=*svn* "$1" * ; fi ; }
Run Code Online (Sandbox Code Playgroud)
从命令行执行时,它工作,但当我尝试从vim(:!rgr test)调用它时,我收到一条错误消息,vim退出:
bash: rgr: command not found
[4]+ Stopped vi ~/somefile
Run Code Online (Sandbox Code Playgroud)
如果我禁用交互模式,我只是得到"命令未找到"消息,并且vim不会退出.
如何让vim识别我的别名?我在OS X和Ubuntu上都重现了这种行为.
Jak*_*kob 33
如果您想要非交互式shell(默认)但扩展bash别名,请将别名定义放在文件中,例如.bash_aliases并在此文件中显式启用别名扩展:
shopt -s expand_aliases
alias la='ls -la'
Run Code Online (Sandbox Code Playgroud)
然后将其添加到您的.vimrc所以每次从vim中运行shell命令时实际读取别名文件:
let $BASH_ENV = "~/.bash_aliases"
Run Code Online (Sandbox Code Playgroud)
Kev*_*vin 23
尝试将此行添加到您的~/.vimrc:
set shell=/bin/bash\ -i
Run Code Online (Sandbox Code Playgroud)
然后vim将使用默认情况下-i从中读取的交互式shell()~/.bashrc.有关:h shell详细信息,请参阅shell.
我看到这与你说的不起作用的先前答案基本相同.请在您的机器上尝试下面的示例会话,看看您是否有类似的结果(并从您在示例中看到的输出中发布任何错误/偏差).
$ cat .bash_aliases
alias test_alias="echo test alias"
test_func () {
echo test func
}
$ vim
[vim]:set shell=/bin/bash
[vim]:!type test_alias
/bin/bash: line 0: type: test_alias: not found
shell returned 1
Press ENTER or type command to continue
[vim]:!type test_func
/bin/bash: line 0: type: test_func: not found
shell returned 1
Press ENTER or type command to continue
[vim]:set shell=/bin/bash\ -i
[vim]:!type test_alias
test_alias is aliased to `echo test alias'
Press ENTER or type command to continue
[vim]:!type test_func
test_func is a function
test_func ()
{
echo test func
}
Press ENTER or type command to continue
Run Code Online (Sandbox Code Playgroud)
至于为什么它不能开始,bash只是简单地运行(即既不是交互式也不是登录;默认为vim和大多数其他目的),它会读取指定的任何文件$BASH_ENV:
When bash is started non-interactively, to run a shell script, for
example, it looks for the variable BASH_ENV in the environment, expands
its value if it appears there, and uses the expanded value as the name
of a file to read and execute. Bash behaves as if the following com?
mand were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the file
name.
Run Code Online (Sandbox Code Playgroud)
通过添加-i,我们使shell具有交互性,因此它读取~/.bashrc:
When an interactive shell that is not a login shell is started, bash
reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if
these files exist. This may be inhibited by using the --norc option.
The --rcfile file option will force bash to read and execute commands
from file instead of /etc/bash.bashrc and ~/.bashrc.
Run Code Online (Sandbox Code Playgroud)
该*profile文件启动一个登录shell时读:
When bash is invoked as an interactive login shell, or as a non-inter?
active shell with the --login option, it first reads and executes com?
mands 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, and reads and executes commands from the first one that
exists and is readable. The --noprofile option may be used when the
shell is started to inhibit this behavior.
Run Code Online (Sandbox Code Playgroud)
kik*_*iyo 10
我通过手册页得到了这个工作bash:
set shell=/bin/bash\ --rcfile\ ~/.bash_profile
Run Code Online (Sandbox Code Playgroud)
同样,--init-file作品.
请注意,这\ -i 不是必需的,但可以添加到命令中:
set shell=/bin/bash\ --rcfile\ ~/.bash_profile\ -i
Run Code Online (Sandbox Code Playgroud)
示例:
~/.bash_profile 包含
source ~/.bash_aliases
Run Code Online (Sandbox Code Playgroud)
~/.bash_aliases 包含
alias rdc="open -a \"Remote Desktop Connection\""
Run Code Online (Sandbox Code Playgroud)
~/.vimrc 包含
set shell=/bin/bash\ --rcfile\ ~/.bash_profile
map ,r :!rdc &<cr>
Run Code Online (Sandbox Code Playgroud)