bash 별칭에서 힘내 자동 완성?


84

go대한 간단한 bash 별칭으로 사용하고 git checkout branchname있습니다. 내가 놓친 것은 git checkout branchna...별칭이 아닌 전체 명령으로 작동하는 자동 완성 기능입니다 .

다른 명령에 대해 자동 완성 "드라이버"를 "상속"하도록 Bash에 지시하는 방법이 있습니까?




관련 : 별칭이 사용자 지정 git-dir을 설정하는 경우 this .
user202729

답변:


47

원래 명령에서 사용 된 완성 기능을 찾을 수 있으면를 사용하여 별칭에 할당 할 수 있습니다 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 

1
우분투 12.04에서 완성은 다음 위치에있는 것 같습니다 : /etc/bash_completion.d/git이 아니라 /etc/bash_complete.d/git
Maks

8
complete -F작동하지 않는 경우 __git_complete아래에 대한 답변을 참조하십시오. stackoverflow.com/a/24665529/3779
Steve Clay

100

사용 후 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

4
@ LưuVĩnhPhúc 저도 같은 문제를 겪고있었습니다. 내가했기 때문에 그것은이었다 __git_complete(와 사제를 통해 설치 내 bash는 완성 패키지의 기능 brew install bash-completion현재 나의 배쉬 프로파일에 공급되지 않았다). ~ / .bash_profile에이 줄을 추가해야했습니다 :[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
jangosteve

1
git checkout별칭 아래에 __git_complete를 추가하면 매력처럼 작동했습니다!
Rajaraman

2
__git_main, 아니 어야합니다 _git. _git존재 하는 이유 는 이전 버전과의 호환성 때문이며 기본적으로 __git_main래핑되어 있습니다.
FelipeC

19

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>

이것은 하나의 최신 답변에 기존 답변의 지혜를 결합, 모두 감사합니다.


ssh / terminal 전용 세션에 대해 추가하고 모든 것을 .bashrc에 넣습니다. .bash_profile에 git 완전한 내용과 .bashrc에 별칭이 있었지만 작동하지 못했습니다. 나는 echo "profile"/ echo "rc"를 추가하기 전까지 는 그것을 알아 냈습니다.
mtonc 19

18

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

1
수동로드가 필요한 이유는 현재 Bash가 해당 파일을 동적으로로드하기 때문입니다. 지금까지 이름에 따라로드합니다. 파일 .bashrc을 읽은 시간이 너무 늦어 Git 완료를 누르기 전까지는 파일이로드되지 않습니다 .
Franklin Yu

2

다른 우수한 답변에 추가하려면 일반적으로 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


1

의 경우 macOS다음을 실행하여 bash 완료를 설치하십시오.

 brew install bash-completion

그런 다음 다음을 추가하십시오.

[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh" 

.bashrc 또는 .bash_profile에


1

Ubuntu 20.04에서는 이것을 추가합니다. ~/.bashrc

source /usr/share/bash-completion/completions/git
alias g='git'
__git_complete g _git
alias go='git checkout'
__git_complete go _git_checkout

1

다른 사람이을 사용해야한다고 대답 했으므로 __git_complete그렇지 않으면 스크립트가 실패합니다.

alias g="git"
__git_complete g __git_main

alias g="gl"
__git_complete gl _git_log

그러나 _git주 명령 에는 사용하지 않아야합니다 __git_main.

불행히도 완성에 대한 많은 정보가 숨겨져 있지만 내 포크의 README에서 더 많은 것을 찾을 수 있습니다 : git-completion .


0

나는 당신이 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 ; ... '".
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.