可以在 zsh 中使用 Bash 选项卡完成脚本吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3249432/
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
Can a Bash tab-completion script be used in zsh?
提问by Coderer
I have a Bash tab-completion script for Apache's Hadoop. Normally, I use zsh as my day-to-day shell. It tends to be pretty bash-like when I need it to be, but it looks like the tab-completion systems are radically different between them. Is there a simple way to "convert" the existing bash-tab-completion definitions to work in zsh? I don't want to invest a ton of time in this, but if it's easy I'd save a moderate amount of effort.
我有一个用于 Apache Hadoop 的 Bash 选项卡完成脚本。通常,我使用 zsh 作为我的日常 shell。当我需要它时,它往往非常像 bash,但看起来它们之间的制表符完成系统完全不同。有没有一种简单的方法可以“转换”现有的 bash-tab-completion 定义以在 zsh 中工作?我不想在这方面投入大量时间,但如果这很容易,我会节省一些精力。
采纳答案by Paused until further notice.
From this page(dated 2010/01/05):
从此页面(日期为 2010/01/05):
Zsh can handle bash completions functions. The latest development version of zsh has a function bashcompinit, that when run will allow zsh to read bash completion specifications and functions. This is documented in the zshcompsys man page. To use it all you need to do is run bashcompinit at any time after compinit. It will define complete and compgen functions corresponding to the bash builtins.
Zsh 可以处理 bash 完成功能。zsh 的最新开发版本有一个函数 bashcompinit,运行时将允许 zsh 读取 bash 完成规范和函数。这记录在 zshcompsys 手册页中。要使用它,您需要做的就是在 compinit 之后随时运行 bashcompinit。它将定义与 bash 内置函数相对应的 complete 和 compgen 函数。
回答by freestyler
autoload bashcompinit
bashcompinit
source /path/to/your/bash_completion_file
回答by Jatin Kumar
autoload -U +X compinit && compinit
autoload -U +X bashcompinit && bashcompinit
source /path/to/your/bash_completion_script
I am running zsh zsh 5.0.2 (x86_64-apple-darwin13.0)
without any ~/.zshrcand the above sequence worked in a freshly spawned zsh shell.
我在zsh 5.0.2 (x86_64-apple-darwin13.0)
没有任何 ~/.zshrc 的情况下运行 zsh并且上述序列在新生成的 zsh shell 中工作。
Thanks to git-completion.bash script for the hint :D
感谢 git-completion.bash 脚本的提示:D
Read-on for more details on above 3 lines:
继续阅读以上 3 行的更多详细信息:
Bash has awesome in built auto completion support but the bash autocomplete scripts don't work directly zsh as zsh environment doesn't have the essential bash autocomplete helper functions like compgen
, complete
. It does so in an effort to keep zsh session fast.
Bash 在内置自动完成支持方面非常出色,但 bash 自动完成脚本不能直接在 zsh 中工作,因为 zsh 环境没有基本的 bash 自动完成辅助功能,如compgen
, complete
。这样做是为了让 zsh 会话保持快速。
These days zsh is shipped with appropriate completion scripts like compinit
and bashcompinit
which have the required functions to support bash autocomplete scripts.
如今,zsh 附带了适当的完成脚本,例如compinit
并且bashcompinit
具有支持 bash 自动完成脚本所需的功能。
autoload <func_name>
: Note that autoload is defined in zsh and not bash. autoload
looks for a file named in the directory paths returned by fpath
command and marks a function to load the same when it is first invoked.
autoload <func_name>
: 请注意,自动加载是在 zsh 而不是 bash 中定义的。autoload
查找在fpath
command返回的目录路径中命名的文件,并标记一个函数以在第一次调用时加载相同的函数。
- -U:Ignore any aliases when loading a function like compinit or bashcompinit
- +X:Just load the named function fow now and don't execute it
- -U:在加载像 compinit 或 bashcompinit 这样的函数时忽略任何别名
- +X:现在只加载命名函数 fow 并且不执行它
For example on my system echo $fpath
returns /usr/share/zsh/site-functions
and /usr/share/zsh/5.0.5/functions
and both compinit
and bashcompinit
are available at /usr/share/zsh/5.0.5/functions
.
例如在我的系统echo $fpath
恢复/usr/share/zsh/site-functions
和/usr/share/zsh/5.0.5/functions
无一不compinit
和bashcompinit
可在/usr/share/zsh/5.0.5/functions
。
Also for most people may be only autoload -U +X bashcompinit && bashcompinit
is required because some other script like git autocomplete or their own ~/.zshrc
may be doing autoload -U +X compinit && compinit
, but it's safe to just run both of them.
此外,对于大多数人来说,可能只是autoload -U +X bashcompinit && bashcompinit
因为其他一些脚本(如 git autocomplete 或他们自己的脚本)~/.zshrc
可能正在执行autoload -U +X compinit && compinit
,因此可能只需要,但同时运行它们是安全的。
回答by Eduardo Cuomo
For zsh
use:
对于zsh
使用:
compdef
compadd
compdef
compadd
My example:
我的例子:
# bash completion for bxrun (/home/ecuomo/projects/bashx/bxrun)
_bxrun_methods() {
grep "^\s*\(function\s\+\)\?__.\+()\s*{.*$" "" | while read line ; do
echo "$line" | sed "s/()\s*{.*//g" | sed "s/\s*\(function\s\+\)\?__//g"
done
}
_bxrun_lst() {
if [ -d "/home/ecuomo/projects/bashx/src/actions" ]; then
for f in /home/ecuomo/projects/bashx/src/actions/* ; do
if [ -f "${f}" ]; then
basename "${f}" | sed 's/\..*$//g'
fi
done
fi
_bxrun_methods "/home/ecuomo/projects/bashx/bxrun"
_bxrun_methods "/home/ecuomo/projects/bashx/src/bashx.sh"
}
_bxrun() {
local cur
COMPREPLY=()
cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $( compgen -W '$( _bxrun_lst )' -- $cur ) )
}
_bxrun_zsh() {
compadd `_bxrun_lst`
}
if type complete >/dev/null 2>/dev/null; then
# bash
complete -F _bxrun bxrun
else if type compdef >/dev/null 2>/dev/null; then
# zsh
compdef _bxrun_zsh bxrun
fi; fi
Source: My code https://github.com/reduardo7/bashx
回答by Walter Wilfinger
I am running Antigenas a Oh-My-Zsh plugin manager. I had a few bash completion scripts written by coworkers that I wanted to load into Zsh with a simple source /path/to/completion
.
我将Antigen作为 Oh-My-Zsh 插件管理器运行。我有一些同事编写的 bash 完成脚本,我想用一个简单的 .bash 文件加载到 Zsh 中source /path/to/completion
。
I had some trouble, because it seems like either Antigen or OMZ (hard to tell) concern themselves with only loading completion scripts from their plugins. I finally got around this by autoloading bashcompinit
andcompinit
after antigen apply
. Simply autoloading bashcompinit
wasn't enough.
我遇到了一些麻烦,因为似乎 Antigen 或 OMZ(很难说)只关心从他们的插件加载完成脚本。我终于通过自动加载bashcompinit
和compinit
之后解决了这个问题antigen apply
。仅仅自动加载bashcompinit
是不够的。
source ~/.antigen/antigen.zsh
antigen use oh-my-zsh
antigen apply
autoload -U +X compinit && compinit
autoload -U +X bashcompinit && bashcompinit
source /path/to/bash_completion
Antigen creates its .zcompdump
file at $ANTIGEN_COMPDUMP
which for me was ~/.antigen/.zcompdump
Antigen 创建它的.zcompdump
文件$ANTIGEN_COMPDUMP
,对我来说是~/.antigen/.zcompdump
The re-invoke of compinit and bashcompinit create a second .zcompdump at $HOME/.zcompdump
重新调用 compinit 和 bashcompinit 在以下位置创建第二个 .zcompdump $HOME/.zcompdump
That seemsto all work out, because I am able to use the completions set up by /path/to/bash_completion
. I've deleted both .zcompdump files a few times to make sure they're regenerated and seems to work.
这似乎都解决了,因为我能够使用/path/to/bash_completion
. 我已经多次删除了 .zcompdump 文件以确保它们重新生成并且似乎可以工作。
I've had to rm the .zcompdump files a few times after a machine reboot because of errors thrown when trying to tab complete, but I'm unsure if that's due to this set up or something else. rm ~/.zcompdump && rm $ANTIGEN_COMPDUMP
and a new shell fixes that for me.
由于尝试完成选项卡时抛出错误,我不得不在机器重新启动后多次 rm .zcompdump 文件,但我不确定这是由于此设置还是其他原因。rm ~/.zcompdump && rm $ANTIGEN_COMPDUMP
一个新的外壳为我解决了这个问题。
Versions used at time of writing:
撰写本文时使用的版本:
Antigen = v2.2.3 = d3d4ee0
Oh-my-zsh = c3b072e
Zsh = 5.3