如何将’git’别名为’g’以便保留bash-completion规则?

如果我这样做:

alias g='git' 

我放弃了所有完成规则(即在打字后点击TAB时,分支和遥控器不再自动完成,例如g push o )。

/etc/bash_completion.d/git复制和修改, /etc/bash_completion.d/git添加到~/.bashrc

 complete -o bashdefault -o default -o nospace -F _git g 2>/dev/null \ || complete -o default -o nospace -F _git g 

最新的bash-completion上游移动并重命名了一些东西。 下雪了:

 source /usr/share/bash-completion/completions/git __git_complete g __git_main 

当您遇到以下情况时,在最新版本的Ubuntu(例如14.04,也是Fedora 22+)中使用它:

 completion: function `_git' not found 

在完成期间。

~/.bashrc

 alias g='git' source /usr/share/bash-completion/completions/git complete -o default -o nospace -F _git g 

通过http://29a.ch/2013/8/9/fixing-bash-autocomplete-on-ubuntu-13-04

更新的方式 (完成对我不起作用):

  1. cd – 切换到您的主目录
  2. wget https://raw.github.com/git/git/master/contrib/completion/git-completion.bash
  3. source ~/git-completion.bash添加到.bashrc文件中(如果你没有这个文件在你的主文件夹中创建一个,bash会自动查找)
  4. alias g='git'添加到.bashrc文件中。
  5. 使用source ~/.bashrc开始新会话或获取更改

首先,查找原始完成命令。 例:

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

现在将这些添加到您的启动脚本(例如〜/ .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 

可能不需要_completion_loader行。 但在某些情况下,只有在键入命令并第一次按TAB键后才会动态加载完成function。 因此,如果您没有使用原始命令,并尝试别名+ TAB ,您可能会收到类似“bash:completion:function not found”的错误。

为了完整起见,我想使用~/.bash-completion文件添加一个答案,该文件来自bash-completion脚本的末尾:

 _xfunc git __git_complete g __git_main _xfunc git __git_complete gl _git_log _xfunc git __git_complete gd _git_diff _xfunc git __git_complete gb _git_branch 

然后在我的~/.bashrc我只有别名。 我试图:

  • 避免使用bash-completion填充我的~/.bashrc (保留它所属的东西)✓
  • 避免将整个git-completion应用到我的shell中

不幸的是, _xfunc无论如何_xfunc获得git-completion。 一旦我弄清楚如何正确地做到这一点,我就会更新这个答案(我也在这里问过午餐盘)。

请看这里: https : //gist.github.com/scue/576310b7c6b7714aad05

 wget https://gist.github.com/scue/576310b7c6b7714aad05/raw/459d46761c231f5c373c1cf496920b01bb6669d2/.bash_aliases.git -O ~/.bash_aliases.git echo "test -e ~/.bash_aliases.git && source ~/.bash_aliases.git" >> ~/.bashrc 

享受!(^ o ^)/

您可以像往常一样定义别名:

 alias g='git' 

然后安装complete-alias以使bash完成alias-aware。