Jak uzyskać zakończenie bash do pracy z aliasami?

Case in point:

Jestem na mac z bash v3. 2. 17, używam git zainstalowanego przez macports z wariantem bash_completion.

Kiedy wpisuję git checkout m<tab>. na przykład, wypełniam go do master.

/ Align = "left" / git checkout, gco. Kiedy wpiszę gco m<tab>, Nazwa gałęzi nie jest automatycznie uzupełniana.

Najlepiej, żeby autocompletion zadziałał magicznie dla wszystkich moich aliasów. Czy to możliwe? W przeciwnym razie chciałbym ręcznie dostosować go dla każdego aliasu. Więc, jak mam to zrobić?

Author: greg-449, 2008-12-05

10 answers

Jak wspomniano w komentarzach powyżej,

complete -o default -o nospace -F _git_checkout gco
Nie będzie już działać. Jednak w git-completion istnieje funkcja __git_complete.bash, który może być użyty do Ustawienia dopełnienia dla aliasów w ten sposób:
__git_complete gco _git_checkout
 152
Author: chris_sutter,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-21 18:25:48

Napotkałem również ten problem i wymyśliłem ten fragment kodu. Spowoduje to automatyczne uzupełnienie wszystkich aliasów. Uruchom go po zadeklarowaniu wszystkich (lub dowolnych) aliasów.

# wrap_alias takes three arguments:
# $1: The name of the alias
# $2: The command used in the alias
# $3: The arguments in the alias all in one string
# Generate a wrapper completion function (completer) for an alias
# based on the command and the given arguments, if there is a
# completer for the command, and set the wrapper as the completer for
# the alias.
function wrap_alias() {
  [[ "$#" == 3 ]] || return 1

  local alias_name="$1"
  local aliased_command="$2"
  local alias_arguments="$3"
  local num_alias_arguments=$(echo "$alias_arguments" | wc -w)

  # The completion currently being used for the aliased command.
  local completion=$(complete -p $aliased_command 2> /dev/null)

  # Only a completer based on a function can be wrapped so look for -F
  # in the current completion. This check will also catch commands
  # with no completer for which $completion will be empty.
  echo $completion | grep -q -- -F || return 0

  local namespace=alias_completion::

  # Extract the name of the completion function from a string that
  # looks like: something -F function_name something
  # First strip the beginning of the string up to the function name by
  # removing "* -F " from the front.
  local completion_function=${completion##* -F }
  # Then strip " *" from the end, leaving only the function name.
  completion_function=${completion_function%% *}

  # Try to prevent an infinite loop by not wrapping a function
  # generated by this function. This can happen when the user runs
  # this twice for an alias like ls='ls --color=auto' or alias l='ls'
  # and alias ls='l foo'
  [[ "${completion_function#$namespace}" != $completion_function ]] && return 0

  local wrapper_name="${namespace}${alias_name}"

  eval "
function ${wrapper_name}() {
  let COMP_CWORD+=$num_alias_arguments
  args=( \"${alias_arguments}\" )
  COMP_WORDS=( $aliased_command \${args[@]} \${COMP_WORDS[@]:1} )
  $completion_function
  }
"

  # To create the new completion we use the old one with two
  # replacements:
  # 1) Replace the function with the wrapper.
  local new_completion=${completion/-F * /-F $wrapper_name }
  # 2) Replace the command being completed with the alias.
  new_completion="${new_completion% *} $alias_name"

  eval "$new_completion"
}

# For each defined alias, extract the necessary elements and use them
# to call wrap_alias.
eval "$(alias -p | sed -e 's/alias \([^=][^=]*\)='\''\([^ ][^ ]*\) *\(.*\)'\''/wrap_alias \1 \2 '\''\3'\'' /')"

unset wrap_alias
 51
Author: Hesky Fisher,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-25 03:16:28

W git-completion.bash znajduje się linia:

complete -o default -o nospace -F _git git

Patrząc na tę linię (I funkcję _git) możesz dodać tę linię do swojego .bash_profile:

complete -o default -o nospace -F _git_checkout gco
 18
Author: Chris Lloyd,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-09-19 15:27:55

Mam aliased g= 'git' , i w połączeniu z moimi aliasami git wpisuję takie rzeczy jak

$ g co <branchname>

Prostszą poprawką dla mojego konkretnego przypadku użycia było dodanie pojedynczej linii do git-completion.

Tuż pod tym wierszem:

__git_complete git _git

Dodałem tę linię do obsługi mojego pojedynczego aliasu 'g':

__git_complete g _git
 15
Author: wonderfulthunk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-05-22 18:07:22

Najlepiej, żeby autocompletion zadziałał magicznie dla wszystkich moich aliasów. Czy to możliwe?

Tak, jest to możliwe dzięki projektowi complete-alias (na Linuksie). Wsparcie dla komputerów Mac jest eksperymentalne, ale użytkownicy zgłosili sukces.

 9
Author: Cyker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-22 23:22:49

Ta strona forum pokazuje rozwiązanie.

Umieść te linie w swoim .bashrc lub .bash_profile:

# Author.: Ole J
# Date...: 23.03.2008
# License: Whatever

# Wraps a completion function
# make-completion-wrapper <actual completion function> <name of new func.>
#                         <command name> <list supplied arguments>
# eg.
#   alias agi='apt-get install'
#   make-completion-wrapper _apt_get _apt_get_install apt-get install
# defines a function called _apt_get_install (that's $2) that will complete
# the 'agi' alias. (complete -F _apt_get_install agi)
#
function make-completion-wrapper () {
    local function_name="$2"
    local arg_count=$(($#-3))
    local comp_function_name="$1"
    shift 2
    local function="
function $function_name {
    ((COMP_CWORD+=$arg_count))
    COMP_WORDS=( "$@" \${COMP_WORDS[@]:1} )
    "$comp_function_name"
    return 0
}"
    eval "$function"
}

# and now the commands that are specific to this SO question

alias gco='git checkout'

# we create a _git_checkout_mine function that will do the completion for "gco"
# using the completion function "_git"
make-completion-wrapper _git _git_checkout_mine git checkout

# we tell bash to actually use _git_checkout_mine to complete "gco"
complete -o bashdefault -o default -o nospace -F _git_checkout_mine gco

To rozwiązanie jest podobne do skryptu balshetzera , ale tylko ten naprawdę działa dla mnie. (skrypt balshetzera miał problemy z niektórymi z moich aliasów.)

 5
Author: hcs42,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:55:07

Możesz również spróbować użyć aliasów Gita. Na przykład w moim pliku ~/.gitconfig mam sekcję, która wygląda tak:

[alias]
        co = checkout

Więc możesz wpisać git co m<TAB>, a to powinno rozwinąć się do git co master, czyli git checkout.

 4
Author: mipadi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2008-12-05 15:39:26

Jeszcze jedną opcją jest użycie pliku ~/.bash_completion. Aby utworzyć gco alias dla git checkout wystarczy umieścić to tutaj:

_xfunc git __git_complete gco _git_checkout

Następnie w ~/.bashrc musisz wpisać tylko sam alias:

alias gco='git checkout'

Dwie linie. To wszystko.

Explanation:

~/bash_completion jest pobierany na końcu głównego skryptu bash_completion. W gentoo znalazłem skrypt Główny w /usr/share/bash-completion/bash_completion.

Bit _xfunc git zajmuje się pozyskiwaniem pliku git-completion, więc nie musisz niczego umieszczać else in ~/.bashrc.

Zaakceptowana odpowiedź wymaga skopiowania .git-completion.sh i źródła jej z pliku ~/.bashrc, który uważam za kiepski.


PS: wciąż próbuję dowiedzieć się, jak nie wprowadzać całego skryptu git-completion do mojego środowiska bash. Proszę skomentować lub edytować, jeśli znajdziesz sposób.

 3
Author: kub1x,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-18 08:17:56

Wystarczy znaleźć polecenie complete i zduplikować linię zawierającą nazwę aliasu.

Mam alias d-m="docker-machine". Słownie, d-m jest aliasem dla docker-machine.

Tak więc na Macu (via brew) pliki zakończenia są w cd `brew --prefix`/etc/bash_completion.d/.
W moim przypadku edytowałem plik o nazwie docker-machine.
Na samym dole było:

complete -F _docker_machine docker-machine

Więc dodałem kolejną linijkę, z moim aliasem:

complete -F _docker_machine docker-machine
complete -F _docker_machine d-m
 2
Author: luckydonald,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-07 11:07:37

Najpierw poszukaj oryginalnego polecenia zakończenia. Przykład:

$ complete | grep git

complete -o bashdefault -o default -o nospace -F __git_wrap__git_main git

Teraz dodaj je do skryptu startowego (np.~/."bashrc"): {]}

# copy the original statement, but replace the last command (git) with your alias (g)
complete -o bashdefault -o default -o nospace -F __git_wrap__git_main g

# load dynamically loaded completion functions (may not be required)
_completion_loader git

Linia _completion_loader może nie być wymagana. Ale w niektórych sytuacjach funkcja uzupełniania jest ładowana dynamicznie dopiero po wpisaniu polecenia i naciśnięciu TAB za pierwszym razem. Jeśli więc nie użyłeś oryginalnego polecenia i spróbujesz aliasu + TAB, może pojawić się błąd "bash: completion: function '_docker' not found".

 0
Author: wisbucky,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-26 22:50:04