与Commands executed from vim are not recognizing bash command aliases相同的问题,但那里的解决方案不起作用。
我在〜/ .vimrc中设置了这些变量:
set shellcmdflag=-ic
set shell=/bin/bash\ -i
我的〜/ .bash_aliases中有别名:
rgr() { if [ ! -z "$2" ]; then grep -rI --exclude=\*.svn\* "$1" * --include=$2 ; else grep -rI --exclude=*svn* "$1" * ; fi ; }
从命令行执行时有效,但是当我尝试从vim(:!rgr test
)调用它时,我收到一条错误消息并且vim退出:
bash: rgr: command not found
[4]+ Stopped vi ~/somefile
如果我禁用交互模式,我只是收到“未找到命令”消息,而vim不会退出。
如何让vim识别我的别名?我在OS X和Ubuntu上都重现了这种行为。
答案 0 :(得分:33)
如果您想要非交互式shell(默认情况下)但扩展bash别名,请将别名定义放在文件中,例如: .bash_aliases
和在此文件中明确启用别名扩展:
shopt -s expand_aliases
alias la='ls -la'
然后将其添加到.vimrc
中,以便每次从vim中运行shell命令时实际读取别名文件:
let $BASH_ENV = "~/.bash_aliases"
答案 1 :(得分:22)
尝试将此行添加到~/.vimrc
:
set shell=/bin/bash\ -i
然后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
至于为什么它没有开始工作,当bash
被简单地运行时(即既不是交互式也不是登录;默认为vim和大多数其他用途),它会读取{{1中指定的任何文件}}:
$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.
,我们将shell设置为交互式,因此它显示为-i
:
~/.bashrc
启动登录shell时会读取 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.
个文件:
*profile
答案 2 :(得分:9)
我通过bash
:
set shell=/bin/bash\ --rcfile\ ~/.bash_profile
同样,--init-file
有效。
请注意,\ -i
不是必需的,但可以将其添加到命令中:
set shell=/bin/bash\ --rcfile\ ~/.bash_profile\ -i
实施例:
~/.bash_profile
包含
source ~/.bash_aliases
~/.bash_aliases
包含
alias rdc="open -a \"Remote Desktop Connection\""
~/.vimrc
包含
set shell=/bin/bash\ --rcfile\ ~/.bash_profile
map ,r :!rdc &<cr>
答案 3 :(得分:0)
我在~/.vimrc
:
set shell=bash\ -i
所有自定义功能:
mkcd () {
mkdir -p "$*"
cd "$*"
}
和别名:
alias lsvn='svn list -vR'
从Vim使用时工作。
但我不使用~/.bash_aliases
,我已将它们直接添加到~/.bashrc
。
您是否从~/.bash_aliases
采购~/.bashrc
?