I have a bash -tab-completion script. Is there an easy way to use it from zsh?

I have a bash -tab-completion script for Apache Hadoop. I usually use zsh as my everyday shell. It tends to be pretty bash -like when I need it, but it seems like tab completion systems are radically different between them. Is there an easy way to β€œconvert” existing bash-tab-completion definitions to work in zsh? I do not want to invest a lot of time in it, but if it would be easy, I would save moderate efforts.

+50
bash zsh tab-completion
Jul 14 '10 at 18:52
source share
4 answers

From this page (from 2010/01/05):

Zsh can handle bash completion functions. The latest version of zsh has a bashcompinit function that, at startup, allows zsh to read the specifications and shutdown functions of bash. This is described in the zshcompsys man page. To use it all you need to do, run bashcompinit anytime after compinit. It will define full and compgen functions corresponding to the built-in bash.

+20
Jul 15 '10 at 1:26
source share
autoload bashcompinit bashcompinit source /path/to/your/bash_completion_file 
+100
Dec 13 '11 at 15:49
source share
 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 works in the newly created zsh shell.

Thanks to git -completion.bash script for a hint: D




For more details on the three lines:

Bash has awesome built-in autocomplete support, but autocomplete bash scripts do not work directly with zsh, since the zsh environment does not have the necessary auxiliary helper functions of bash, such as compgen , complete . He does this to speed up the zsh session.

These days, zsh comes with matching completion scripts like compinit and bashcompinit , which have the necessary functions to support autocomplete bash scripts.

autoload <func_name> : Note that autoload is defined in zsh, not bash. autoload searches for the file named in the directory path returned by the fpath command and marks the function that loads it the first time it is called.

  • -U: Ignore any aliases when loading a function such as compinit or bashcompinit
  • + X: Just load the named function 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 in /usr/share/zsh/5.0.5/functions .

Also for most people there can only be autoload -U +X bashcompinit && bashcompinit , because some other script like git autocomplete or their own ~/.zshrc can execute autoload -U +X compinit && compinit , but it's safe to just run both of them.

+18
Jan 09 '15 at 4:44
source share

For zsh use:

  • compdef
  • compadd

My example:

 # bash completion for bxrun (/home/ecuomo/projects/bashx/bxrun) _bxrun_methods() { grep "^\s*\(function\s\+\)\?__.\+()\s*{.*$" "${1}" | 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 is https://github.com/reduardo7/bashx

+1
Jan 08 '16 at 15:59
source share



All Articles