从 vim 执行的命令无法识别 bash 命令别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4642822/
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
Commands executed from vim are not recognizing bash command aliases
提问by Nick Vanderbilt
I use bash on mac and one of the aliases is like this
我在 mac 上使用 bash,其中一个别名是这样的
alias gitlog='git --no-pager log -n 20 --pretty=format:%h%x09%an%x09%ad%x09%s --date=short --no-merges'
However when I do
但是当我做
:! gitlog
I get
我得到
/bin/bash: gitlog: command not found
I know I can add aliases like this in my .gitconfig
我知道我可以在我的 .gitconfig 中添加这样的别名
[alias]
co = checkout
st = status
ci = commit
br = branch
df = diff
However I don't want to add all my bash aliases to .gitconfig. That is not DRY.
但是我不想将我所有的 bash 别名添加到 .gitconfig。那不是 DRY。
Is there a better solution?
有更好的解决方案吗?
回答by Josh Lee
Bash doesn't load your .bashrc unless it's interactive. Use
Bash 不会加载您的 .bashrc,除非它是交互式的。用
:set shellcmdflag=-ic
to make Vim's :!shell behave like your command prompt.
让 Vim 的:!shell 表现得像你的命令提示符。
回答by Zahnon
I know this question was already previously "answered", but I have a problem with the answer. The shell doesn't needto be set to interactive in Vim. See this thread for an alternative answer without having to exit an interactive shell.
我知道这个问题之前已经“回答过”,但我的回答有问题。在 Vim 中不需要将 shell设置为交互式。请参阅此线程以获取替代答案,而无需退出交互式 shell。
If you want non-interactive shell (as default) but expansion of bash aliases, put your alias definitions in a file, e.g. .bash_aliases and explicitly enable alias expansion in this file:
shopt -s expand_aliases alias la='ls -la'Then 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"
如果您想要非交互式 shell(默认情况下)但 bash 别名的扩展,请将您的别名定义放在一个文件中,例如 .bash_aliases 并在此文件中显式启用别名扩展:
shopt -s expand_aliases alias la='ls -la'然后将其添加到您的 .vimrc 中,以便每次从 vim 中运行 shell 命令时实际读取别名文件:
let $BASH_ENV = "~/.bash_aliases"
This solution was suggested by "Jakob". See the link below for the original. I tested this on Mac OS X 10.9 and it worked flawlessly!
这个解决方案是由“Jakob”提出的。原文见下方链接。我在 Mac OS X 10.9 上对此进行了测试,它运行完美!
回答by user836003
Note that depending on how your bash dotfiles are configured you may want to use the -l rather than the -i option. This will launch the shell as login shell.
请注意,根据 bash 点文件的配置方式,您可能希望使用 -l 而不是 -i 选项。这将启动 shell 作为登录 shell。
回答by Damian Borowski
I know it may be an old question, however none of the above answers worked for me as desired. So for the ones who came here from googling and for (oh-my-)zsh users:
我知道这可能是一个老问题,但是上述答案都没有如我所愿。所以对于那些通过谷歌搜索来到这里的人和 (oh-my-)zsh 用户:
My solution to this was as simply as copying .zshrc to .zshenv - as per http://zsh.sourceforge.net/Intro/intro_3.html:
我对此的解决方案就像将 .zshrc 复制到 .zshenv 一样简单 - 根据http://zsh.sourceforge.net/Intro/intro_3.html:
`.zshenv' is sourced on all invocations of the shell, unless the -f option is set. It should contain commands to set the command search path, plus other important environment variables. `.zshenv' should not contain commands that produce output or assume the shell is attached to a tty.
`.zshenv' 来源于所有 shell 调用,除非设置了 -f 选项。它应该包含设置命令搜索路径的命令,以及其他重要的环境变量。`.zshenv' 不应该包含产生输出或假设 shell 附加到 tty 的命令。
So $ cp ~/.zshrc ~/.zshenvwill do the thing.
所以$ cp ~/.zshrc ~/.zshenv会做这件事。
回答by pfnuesel
I don't feel too comfortable with setting the -i option, as it has quite some impact and I am using the shell often from vim. What I'd do instead is something like :!bash -c ". ~/.alias; gitlog"
我对设置 -i 选项感觉不太舒服,因为它有相当大的影响,而且我经常从 vim 使用 shell。我会做的是类似的事情:!bash -c ". ~/.alias; gitlog"

