Linux 如何为 Git 命令设置自动完成功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11173447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:01:32  来源:igfitidea点击:

How can I set up autocompletion for Git commands?

linuxgitbashdebian

提问by redochka

I have Git (version 1.7.2.5) bash compeletion working on my Debian squeeze (6.0). Git was installed with aptitude and I am using standard debian's bash, which supports command line autocompletion.

我有 Git(版本 1.7.2.5)bash compeletion 在我的 Debian 挤压(6.0)上工作。Git 与 aptitude 一起安装,我使用的是标准 debian 的bash,它支持命令行自动完成。

Now, I just installed Git (1.5.6.5) on an other machine (Lenny/Debian 5.0) and the there is no autocompletion.

现在,我刚刚在另一台机器(Lenny/Debian 5.0)上安装了 Git(1.5.6.5)并且没有自动完成功能。

  1. Why is Git autocomplete not working on the second machine? How do I diagnose this?

  2. What is making completion work on my machine? I have looked for the file git-completion.bashbut it doesn't seem to be on my machine. How does Git complete ever work?

  3. How can I bring git complete to the other machine?

  1. 为什么 Git 自动完成功能在第二台机器上不起作用?我该如何诊断?

  2. 什么使我的机器上的完成工作?我已查找该文件,git-completion.bash但它似乎不在我的机器上。Git如何完成工作?

  3. 如何将 git complete 带到另一台机器?

采纳答案by Piotr Praszmo

You need to source /etc/bash_completion.d/gitto enable git auto-completion.

您需要source /etc/bash_completion.d/git启用 git 自动完成功能。

In my .bashrcit's done with:

在我.bashrc它是通过以下方式完成的:

for file in /etc/bash_completion.d/* ; do
    source "$file"
done

回答by Rudger

Put the following lines in your ~/.bashrc

将以下几行放入您的 ~/.bashrc

if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

The script/program /etc/bash_completionalready includes the scripts in /etc/bash_completion.dand also defines some functions needed by the included scripts.

脚本/程序/etc/bash_completion已经包含了脚本,/etc/bash_completion.d并且还定义了包含的脚本所需的一些功能。

回答by automatix

The shortest way to activate the bash auto-completion for Git on Debian is to add

在 Debian 上为 Git 激活 bash 自动完成功能的最短方法是添加

source /etc/bash_completion.d/git

to the ~/.bashrc(and restart the terminal).

~/.bashrc(并重新启动终端)。

See also here: "Pro Git" -> 2.7 Git Basics - Tips and Tricks -> Auto-Completion.

另请参见此处:“Pro Git” -> 2.7 Git 基础知识 - 提示和技巧 ->自动完成

回答by FuzzyBird

Use Notepad++ to edit your ~/.bashrc file. Put the line at the bottom of the script with a # at the beginning of the line. Save the file. For example: # source C:\cygwin64/etc/bash_completion.d/git

使用 Notepad++ 编辑您的 ~/.bashrc 文件。将该行放在脚本的底部,并在该行的开头使用 #。保存文件。例如:# source C:\cygwin64/etc/bash_completion.d/git

Don't forget to put the entire file path after 'source' and in front of '/etc/' For example, my cygwin64 folder which contains the 'etc' folder is in my c drive so my file path is c:\cygwin64/etc therefore the line I included in my bashrc file is:

不要忘记将整个文件路径放在“source”之后和“/etc/”之前。例如,包含“etc”文件夹的 cygwin64 文件夹在我的 c 驱动器中,因此我的文件路径是 c:\cygwin64 /etc 因此,我在 bashrc 文件中包含的行是:

# source c:\cygwin64/etc/bash_completion.d/git

Save bashrc file. Open Cygwin Terminal ... Boom! It's go time. I then entered the following command and it worked. git clone git:\/\/github.com/magnumripper/JohnTheRipper -b bleeding-jumbo JtR-Bleeding

保存 bashrc 文件。打开 Cygwin 终端 ... 砰!该出发了。然后我输入了以下命令并且它起作用了。 git clone git:\/\/github.com/magnumripper/JohnTheRipper -b bleeding-jumbo JtR-Bleeding

回答by Qi Luo

You need to install this package if missing. And then logout and login.

如果丢失,您需要安装此软件包。然后注销并登录。

apt-get install bash-completion

回答by hlosukwakha

At times git auto-complete disappears because you accidentally deleted your ~/.bashrc file. Check if the bashrc file is there in your home directory. If not, you can always copy it from:

有时 git auto-complete 会消失,因为您不小心删除了 ~/.bashrc 文件。检查 bashrc 文件是否在您的主目录中。如果没有,您可以随时从以下位置复制它:

/etc/skel/.bashrc

回答by lava-lava

For Manjaroand other Arch-based distros. I know it's about debian, but most things are the same but sometimes not. Whatever OS you use you'll end up here.

适用于Manjaro和其他基于 Arch 的发行版。我知道这是关于 debian 的,但大多数事情都是一样的,但有时不一样。无论您使用什么操作系统,您最终都会在这里。

In your ~/.bashrcadd:

在您~/.bashrc添加:

source /usr/share/git/completion/git-completion.bash

And then in terminal

然后在终端

$ source ~/.bashrc

回答by nkalra0123

For Ubuntu/Debian

对于 Ubuntu/Debian

Install Git and bash-completion by the following command:

通过以下命令安装 Git 和 bash-completion:

sudo apt-get install git bash-completion

I don't think you need to do anything else.

我不认为你需要做任何其他事情。