MacOS X设置命令行自动填充及Git提示符

MacOS X yosemite自带了bash 3.2。但是默认没有bash completion。可以用homebrew来启用命令行自动填充的功能。这样输入命令就可以箭步如飞了。

具体做法是:

  • 用homebrew安装bash-completion
  • 启用homebrew的bash-completion脚本

命令如下:

brew install bash-completion
cat <<'EOF' > ~/.bash_profile
# load generic bash completion
if [ -f $(brew --prefix)/etc/bash_completion ]; then
    . $(brew --prefix)/etc/bash_completion
fi
EOF

 

接下来是设置bash使其能够显示Git工作区的简要状态。典型的git状态提示界面如下:


MacOS X设置命令行自动填充及Git提示符_第1张图片
 

此工具在Homebrew的库里已经存在,只要用brew install bash-git-prompt即可。装完这个软件后还要在~/.bash_profile中加载该工具。默认的配色方案和Git自带的不一致,因此,可以自定义个新的配色方案。总之执行以下命令即可:

 

cat <<'EOF' >> ~/.bash_profile
if [ -f "$(brew --prefix bash-git-prompt)/share/gitprompt.sh" ]; then
  GIT_PROMPT_ONLY_IN_REPO=1
  GIT_PROMPT_THEME=Solarized_Git
  GIT_PROMPT_FETCH_REMOTE_STATUS=0
  source "$(brew --prefix bash-git-prompt)/share/gitprompt.sh"

fi
EOF

cat <<'EOF' > /usr/local/Cellar/bash-git-prompt/2.3.5/share/themes/Solarized_Git.bgptheme
# This theme for gitprompt.sh is optimized for the "Solarized Dark" and
# "Solarized Light" color schemes tweaked for Ubuntu terminal fonts.
# It use the use scheme which is consisent with Git.

override_git_prompt_colors() {
  GIT_PROMPT_THEME_NAME="Solarized Git"
  GIT_PROMPT_CONFLICTS="${Red}✖ "
  GIT_PROMPT_CHANGED="${Red}✚ "
  GIT_PROMPT_UNTRACKED="${Red}…"
  GIT_PROMPT_STAGED="${Green}●"
  GIT_PROMPT_STASHED="${BoldMagenta}⚑ "
  GIT_PROMPT_CLEAN="${Green}✔"
  GIT_PROMPT_END_USER=" \n${BoldBlue}${Time12a}${ResetColor} $ "
  GIT_PROMPT_END_ROOT=" \n${BoldBlue}${Time12a}${ResetColor} # "
}

reload_git_prompt_colors "Solarized Git"
EOF
 

你可能感兴趣的:(git,bash)