How do I get bash completion to work with aliases?

后端 未结 12 1012
温柔的废话
温柔的废话 2020-11-28 00:35

Case in point:

I\'m a on mac with bash v3.2.17, I\'m using git installed via macports with the bash_completion variant.

When I type git checkout m<

相关标签:
12条回答
  • 2020-11-28 01:19

    In git-completion.bash there is a line:

    complete -o default -o nospace -F _git git
    

    Looking at that line (and the _git function) you can add this line to your .bash_profile:

    complete -o default -o nospace -F _git_checkout gco
    
    0 讨论(0)
  • 2020-11-28 01:21

    Ideally I'd like autocompletion to just magically work for all my aliases. Is it possible?

    Yes, it is possible with the complete-alias project (on Linux). Support for Mac is experimental but users have reported success.

    0 讨论(0)
  • 2020-11-28 01:21

    You could also try using Git aliases. For example, in my ~/.gitconfig file, I have a section that looks like this:

    [alias]
            co = checkout
    

    So you could type git co m<TAB>, and that should expand to git co master, which is the git checkout command.

    0 讨论(0)
  • 2020-11-28 01:23

    You just have to find the complete command and duplicate the line having the alias name instead.

    I have alias d-m="docker-machine". In words, d-m shall be the alias for docker-machine.

    So on Mac (via brew), the completion files are in cd `brew --prefix`/etc/bash_completion.d/.
    For my case I edited the file called docker-machine.
    All the way at the bottom there was:

    complete -F _docker_machine docker-machine
    

    So I just added another line, with my alias:

    complete -F _docker_machine docker-machine
    complete -F _docker_machine d-m
    
    0 讨论(0)
  • 2020-11-28 01:24

    I have aliased g='git', and combined with my git aliases I type things like

    $ g co <branchname>
    

    The simpler fix for my specific use case was to add a single line to git-completion.

    Right below this line:

    __git_complete git _git
    

    I added this line to handle my single 'g' alias:

    __git_complete g _git
    
    0 讨论(0)
  • 2020-11-28 01:27

    First, look up the original completion command. Example:

    $ complete | grep git
    
    complete -o bashdefault -o default -o nospace -F __git_wrap__git_main git
    

    Now add these to your startup script (e.g. ~/.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
    

    The _completion_loader line may not be required. But for some situations, the completion function is only loaded dynamically after you type the command and press TAB the first time. So if you haven't used the original command, and try the alias + TAB, you may get an error like "bash: completion: function '_docker' not found".

    0 讨论(0)
提交回复
热议问题