Can a Bash tab-completion script be used in zsh?
From this page (dated 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.
autoload bashcompinit
bashcompinit
source /path/to/your/bash_completion_file
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 ~/.zshrc and the above sequence worked in a freshly spawned zsh shell.
Thanks to git-completion.bash script for the hint :D
Read-on for more details on above 3 lines:
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.
These days zsh is shipped with appropriate completion scripts like compinit
and bashcompinit
which have the required functions to support bash autocomplete scripts.
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.
- -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
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
.
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.