programing tip

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

itbloger 2020. 12. 29. 06:45
반응형

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


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

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


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

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

(이 답변 jangosteve에 대한 답변으로 프로그래머의 댓글에 속하지만 댓글을 작성하려면 50 명의 담당자가 필요하므로 자체 답변으로 스팸으로 처리합니다.)


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>

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


Linux Mint에서는이 기능이 작동하지 않았습니다. 나는 받고 있었다 bash: [: 1: unary operator expected .

I found the following response worked quite well - with the troubleshooting section the user provided to be quite helpful. https://superuser.com/questions/436314/how-can-i-get-bash-to-perform-tab-completion-for-my-aliases


To add to other excellent answers: normally you have a lot of Git aliases and it may be tedious to manually forward completions for all of them. Here's a small trick to do this automatically:

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

ReferenceURL : https://stackoverflow.com/questions/9869227/git-autocomplete-in-bash-aliases

반응형