에 go대한 간단한 bash 별칭으로 사용하고 git checkout branchname있습니다. 내가 놓친 것은 git checkout branchna...별칭이 아닌 전체 명령으로 작동하는 자동 완성 기능입니다 .
다른 명령에 대해 자동 완성 "드라이버"를 "상속"하도록 Bash에 지시하는 방법이 있습니까?
에 go대한 간단한 bash 별칭으로 사용하고 git checkout branchname있습니다. 내가 놓친 것은 git checkout branchna...별칭이 아닌 전체 명령으로 작동하는 자동 완성 기능입니다 .
다른 명령에 대해 자동 완성 "드라이버"를 "상속"하도록 Bash에 지시하는 방법이 있습니까?
git-dir을 설정하는 경우 this .
답변:
원래 명령에서 사용 된 완성 기능을 찾을 수 있으면를 사용하여 별칭에 할당 할 수 있습니다 complete -F.
예를 들어, 내 우분투 상자에서에서 사용하는 완료 함수 git checkout는 _git_checkout(에서 찾을 수 있음 /etc/bash_complete.d/git)입니다.
실행하기 전에 complete -F:
[me@home]$ git checkout <TAB><TAB>
HEAD master origin/HEAD origin/master
[me@home]$ alias go="git checkout"
[me@home]$$ go <TAB><TAB>
.git/ precommit_config.py README.md SvnSentinel/
.gitignore precommit.py startcommit.py tests/
후:
[me@home]$$ complete -F _git_checkout go
[me@home]$$ go <TAB><TAB>
HEAD master origin/HEAD origin/master
사용 후 complete -F:
complete -F _git_checkout go
뒤에 탭하면 다음과 같은 go결과가 발생할 수 있습니다.
bash: [: 1: unary operator expected
complete, 사용__git_complete이 목적을위한 git bash 완료의 내장 함수입니다.
별칭을 선언 한 후 올바른 자동 완성 기능을 여기에 바인딩합니다.
alias g="git"
__git_complete g _git
alias go="git checkout"
__git_complete go _git_checkout
alias gp="git push"
__git_complete gp _git_push
__git_complete(와 사제를 통해 설치 내 bash는 완성 패키지의 기능 brew install bash-completion현재 나의 배쉬 프로파일에 공급되지 않았다). ~ / .bash_profile에이 줄을 추가해야했습니다 :[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
git checkout별칭 아래에 __git_complete를 추가하면 매력처럼 작동했습니다!
__git_main, 아니 어야합니다 _git. _git존재 하는 이유 는 이전 버전과의 호환성 때문이며 기본적으로 __git_main래핑되어 있습니다.
Ubuntu 18.04 (Bionic)에서는 다음이 작동합니다. 원하는 bash는 구성 파일의 예에 (당신의 별명을 가진)이 조각 같은 추가 .bashrc, .bash_aliases .bash_profile.
# define aliases
alias gc='git checkout'
alias gp='git pull'
# setup autocompletion
if [ -f "/usr/share/bash-completion/completions/git" ]; then
source /usr/share/bash-completion/completions/git
__git_complete gc _git_checkout
__git_complete gp _git_pull
else
echo "Error loading git completions"
fi
일반적으로 __git_complete지시문 의 형식은 다음과 같습니다.
__git_complete <YOUR ALIAS> _git_<GIT COMMAND NAME>
이것은 하나의 최신 답변에 기존 답변의 지혜를 결합, 모두 감사합니다.
echo "profile"/ echo "rc"를 추가하기 전까지 는 그것을 알아 냈습니다.
Ubuntu 16.04.3 LTS에서 소스에 필요한 파일은 /usr/share/bash-completion/completions/git. 그래서 .bash_custom(또는 .bashrc, 무엇이든) :
[ -f /usr/share/bash-completion/completions/git ] && . /usr/share/bash-completion/completions/git
__git_complete g __git_main
.bashrc을 읽은 시간이 너무 늦어 Git 완료를 누르기 전까지는 파일이로드되지 않습니다 .
다른 우수한 답변에 추가하려면 일반적으로 Git 별칭이 많으며 모든 별칭에 대해 수동으로 완료를 전달하는 것이 지루할 수 있습니다. 이 작업을 자동으로 수행하는 간단한 방법은 다음과 같습니다.
if [ -f "/usr/share/bash-completion/completions/git" ]; then
# Enable Git completions for aliases
. /usr/share/bash-completion/completions/git
for a in $(alias | sed -n 's/^alias \(g[^=]*\)=.git .*/\1/p'); do
c=$(alias $a | sed 's/^[^=]*=.git \([a-z0-9\-]\+\).*/\1/' | tr '-' '_')
if set | grep -q "^_git_$c *()"; then
eval "__git_complete $a _git_$c"
fi
done
fi
Linux Mint에서는 이것이 작동하지 않았습니다. 나는 받고 있었다 bash: [: 1: unary operator expected
.
사용자가 제공 한 문제 해결 섹션에서 다음 응답이 매우 잘 작동한다는 것을 알았습니다. /superuser/436314/how-can-i-get-bash-to-perform-tab-completion-for-my-aliases
다른 사람이을 사용해야한다고 대답 했으므로 __git_complete그렇지 않으면 스크립트가 실패합니다.
alias g="git"
__git_complete g __git_main
alias g="gl"
__git_complete gl _git_log
그러나 _git주 명령 에는 사용하지 않아야합니다 __git_main.
불행히도 완성에 대한 많은 정보가 숨겨져 있지만 내 포크의 README에서 더 많은 것을 찾을 수 있습니다 : git-completion .
나는 당신이 bash 별칭에 대해 구체적으로 묻는 것을 알고 있지만 복잡한 git 별칭에 대해 bash에서 자동 완성 을 찾는 사람들은 여기를 참조 하십시오 .
특히:
# If you use complex aliases of form '!f() { ... }; f', you can use the null
# command ':' as the first command in the function body to declare the desired
# completion style. For example '!f() { : git commit ; ... }; f' will
# tell the completion to use commit completion. This also works with aliases
# of form "!sh -c '...'". For example, "!sh -c ': git commit ; ... '".