bash Vim 在交互模式下无法识别别名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8841116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Vim not recognizing aliases when in interactive mode?
提问by ashgromnies
Same question as this, however the solution there didn't work.
与此相同的问题,但是那里的解决方案不起作用。
I set these variables in my ~/.vimrc:
我在 ~/.vimrc 中设置了这些变量:
set shellcmdflag=-ic
set shell=/bin/bash\ -i
and I have an alias in my ~/.bash_aliases:
我的 ~/.bash_aliases 中有一个别名:
rgr() { if [ ! -z "" ]; then grep -rI --exclude=\*.svn\* "" * --include= ; else grep -rI --exclude=*svn* "" * ; fi ; }
which works when executed from the command line, but when I try to call it from Vim with :!rgr test, I get an error message and Vim exits:
它在从命令行执行时有效,但是当我尝试从 Vim 调用它时:!rgr test,我收到一条错误消息并且 Vim 退出:
bash: rgr: command not found
[4]+ Stopped vi ~/somefile
If I disable the interactive mode, I just get the "command not found" message and Vim doesn't exit.
如果我禁用交互模式,我只会收到“找不到命令”消息,而 Vim 不会退出。
How can I get Vim to recognize my aliases? I've reproduced this behavior on both OS X and Ubuntu.
如何让 Vim 识别我的别名?我已经在 OS X 和 Ubuntu 上重现了这种行为。
采纳答案by kikuchiyo
I got this to work, via the man page for bash:
我让这个工作,通过手册页bash:
set shell=/bin/bash\ --rcfile\ ~/.bash_profile
Similarly, --init-fileworks.
同样,--init-file作品。
Note that \ -iis not necessary, though it can be added to the command:
请注意,这\ -i不是必需的,但可以将其添加到命令中:
set shell=/bin/bash\ --rcfile\ ~/.bash_profile\ -i
Example:
示例:
~/.bash_profilecontains
~/.bash_profile包含
source ~/.bash_aliases
~/.bash_aliasescontains
~/.bash_aliases包含
alias rdc="open -a \"Remote Desktop Connection\""
~/.vimrccontains
~/.vimrc包含
set shell=/bin/bash\ --rcfile\ ~/.bash_profile
map ,r :!rdc &<cr>
回答by Jakob
If you want non-interactive shell (as default) but expansion of bash aliases, put your alias definitions in a file, e.g. .bash_aliasesand explicitly enablealias expansion in this file:
如果您想要非交互式 shell(默认情况下)但 bash 别名的扩展,请将您的别名定义放在一个文件中,例如.bash_aliases并在此文件中显式启用别名扩展:
shopt -s expand_aliases
alias la='ls -la'
Then add this to your .vimrcso the aliases file is actually read each time you run a shell command from within vim:
然后将其添加到您.vimrc的别名文件中,以便每次从 vim 中运行 shell 命令时实际读取别名文件:
let $BASH_ENV = "~/.bash_aliases"
回答by Kevin
Try adding this line to your ~/.vimrc:
尝试将此行添加到您的~/.vimrc:
set shell=/bin/bash\ -i
Then vimwill use an interactive shell (-i) which reads from ~/.bashrc, by default. See :h shellfor more information on shell.
然后vim将使用默认情况下-i从 读取的交互式外壳 ( ) ~/.bashrc。有关:h shell更多信息,请参见shell。
I see this is essentially the same as the previous answers that you say don't work. Please try the sample session below on your machine to see if you have similar results ( and post any errors / divergences from the output you see in the sample ).
我认为这与您说不起作用的先前答案基本相同。请在您的机器上尝试下面的示例会话,看看您是否有类似的结果(并发布与您在示例中看到的输出的任何错误/分歧)。
$ 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
As for why it wasn't working to begin with, when bashis simply run (i.e. neither interactive nor login; default for vim and most other purposes), it reads whatever file is specified in $BASH_ENV:
至于为什么一开始就不起作用,当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.
By adding the -i, we make the shell interactive and it therefore it reads ~/.bashrc:
通过添加-i,我们使外壳具有交互性,因此它显示为~/.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.
The *profilefiles are read when starting a login shell:
该*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.
回答by romainl
I have this in my ~/.vimrc:
我有这个~/.vimrc:
set shell=bash\ -i
All the custom functions:
所有自定义函数:
mkcd () {
mkdir -p "$*"
cd "$*"
}
and aliases:
和别名:
alias lsvn='svn list -vR'
work when used from Vim.
从 Vim 使用时工作。
But I don't use ~/.bash_aliases, I've added them directly to my ~/.bashrc.
但我不使用~/.bash_aliases,我已将它们直接添加到我的~/.bashrc.
Do you source ~/.bash_aliasesfrom ~/.bashrc?
你是从哪里来~/.bash_aliases的~/.bashrc?

