programing tip

Bash 탭 완성 스크립트를 zsh에서 사용할 수 있습니까?

itbloger 2020. 10. 18. 08:39
반응형

Bash 탭 완성 스크립트를 zsh에서 사용할 수 있습니까?


Apache의 Hadoop에 대한 Bash 탭 완성 스크립트가 있습니다. 일반적으로 zsh를 일상적인 셸로 사용합니다. 내가 필요로 할 때 꽤 bash 같은 경향이 있지만 탭 완성 시스템이 근본적으로 다른 것처럼 보입니다. 기존 bash-tab-completion 정의를 zsh에서 작동하도록 "변환"하는 간단한 방법이 있습니까? 나는 이것에 많은 시간을 투자하고 싶지 않지만 그것이 쉽다면 적당한 양의 노력을 절약 할 것입니다.


에서 이 페이지 (2010/01/05 일자) :

Zsh는 bash 완료 기능을 처리 할 수 ​​있습니다. zsh의 최신 개발 버전에는 bashcompinit 함수가 있으며 실행시 zsh가 bash 완료 사양 및 함수를 읽을 수 있습니다. 이것은 zshcompsys 매뉴얼 페이지에 설명되어 있습니다. 이를 사용하려면 compinit 후 언제든지 bashcompinit을 실행하면됩니다. bash 내장에 해당하는 complete 및 compgen 함수를 정의합니다.


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

zsh 5.0.2 (x86_64-apple-darwin13.0) ~ / .zshrc없이 zsh 실행 하고 있으며 위의 시퀀스는 새로 생성 된 zsh 셸에서 작동했습니다.

힌트에 대한 git-completion.bash 스크립트 덕분에 : D


위의 3 줄에 대한 자세한 내용은 다음을 참조하세요.

Bash는 내장 된 자동 완성 지원 기능이 훌륭하지만 bash 자동 완성 스크립트는 zsh 환경에 compgen, 같은 필수 bash 자동 완성 도우미 기능이 없기 때문에 zsh에서 직접 작동하지 않습니다 complete. zsh 세션을 빠르게 유지하기 위해 그렇게합니다.

요즘 zsh는 bash 자동 완성 스크립트를 지원하는 데 필요한 기능이있는 compinit같은 적절한 완료 스크립트와 함께 제공됩니다 bashcompinit.

autoload <func_name>: 자동로드는 bash가 아닌 zsh에 정의되어 있습니다. 명령에 autoload의해 반환 된 디렉토리 경로에서 이름이 지정된 파일을 찾고 fpath처음 호출 될 때 동일하게로드 할 함수를 표시합니다.

  • -U : compinit 또는 bashcompinit와 같은 함수를로드 할 때 별칭을 무시합니다 .
  • + X : 이름 지정된 함수 fow를 지금로드하고 실행하지 마십시오.

내 시스템에 예를 들어 echo $fpath수익률 /usr/share/zsh/site-functions/usr/share/zsh/5.0.5/functions모두 compinitbashcompinit에서 확인할 수 있습니다 /usr/share/zsh/5.0.5/functions.

또한 대부분의 사람들은 autoload -U +X bashcompinit && bashcompinitgit autocomplete와 같은 다른 스크립트 또는 자신의 스크립트 ~/.zshrc가 수행 할 수 있기 때문에 필요할 autoload -U +X compinit && compinit있지만 둘 다 실행하는 것이 안전합니다.


에 대한 zsh사용 :

  • compdef
  • compadd

내 예 :

# 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

출처 : 내 코드 https://github.com/reduardo7/bashx


저는 Antigen 을 Oh-My-Zsh 플러그인 관리자로 실행하고 있습니다. 동료가 작성한 몇 가지 bash 완료 스크립트가있어서 간단한 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 and compinit after antigen apply. Simply autoloading bashcompinit wasn't enough.

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

The re-invoke of compinit and bashcompinit create a second .zcompdump at $HOME/.zcompdump

That seems to 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.

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.

Versions used at time of writing:

Antigen = v2.2.3 = d3d4ee0
Oh-my-zsh = c3b072e
Zsh = 5.3

참고URL : https://stackoverflow.com/questions/3249432/can-a-bash-tab-completion-script-be-used-in-zsh

반응형