2017-10-11 5 views
4

私の.bash_profileには、gitのための多くの機能ショートカットがあります。たとえば、:カスタムbash関数のGitオートコンプリート

しかし、これを端末に入力するときにgitブランチを自動完成したいとします。これはどうすればいいですか?

答えて

3
(I既に .git-completion.bashを使用しています)

手動クラフトbashの補完は、このように単純である:上記のコマンドを供給した後

# our handler that returns choices by populating Bash array COMPREPLY 
# (filtered by the currently entered word ($2) via compgen builtin) 
_gitpull_complete() { 
    branches=$(git branch -l | cut -c3-) 
    COMPREPLY=($(compgen -W "$branches" -- "$2")) 
} 

# we now register our handler to provide completion hints for the "gitpull" command 
complete -F _gitpull_complete gitpull 

$ gitpull <TAB> 
asd  master qwe  zxc 
$ gitpull m<TAB> 
$ gitpull master 

bashの完了に究極の基準です(もちろん)、bashマニュアルのProgrammable Completionのセクションですが、Debian Administrationのページ(part 1ともっと重要なのはpart 2)で紹介しています。

+1

しかし、それはそれを自動完成しません。 'gitpull mas 'と入力すると、それは 'master'に完了しません。 – Nxt3

+1

あなたは正しいです、申し訳ありません、現在の(接頭辞)単語に従って 'compgen'を使って選択肢をフィルタリングする必要があります。 – randomir

+2

これは素晴らしいです。どうもありがとうございます! – Nxt3

関連する問題