附加Git的分支名到命令提示符 [英] Append git's branch name to command prompt

查看:160
本文介绍了附加Git的分支名到命令提示符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Git的-completion.bash功能之一,但我不能自定义我想拥有的外观。这里是我的.bash_profile文件的相关部分:

I wanted to use one of the Git-completion.bash features but I can't customize the look I'd like to have. Here is relevant part of my .bash_profile:

source ~/.git-completion.bash

function prompt
{
local WHITE="\[\033[1;37m\]"
local GREEN="\[\033[0;32m\]"
local CYAN="\[\033[0;36m\]"
local GRAY="\[\033[0;37m\]"
local BLUE="\[\033[0;34m\]"
export PS1="
${GREEN}\u${CYAN}@${BLUE}\h ${CYAN}\w $(__git_ps1 '(%s)') ${GRAY}
$ "
}
prompt

和它没有显示分支名称

不过,如果我替换上面与下面的一家出口PS1,它按预期工作:

However, if I replace export PS1 above with the one below, it works as expected:

export PS1='\w$(__git_ps1 "(%s)") > '

我想这是一些撇号/引号的问题。

I guess it's some apostrophe / quotation marks issue.

我应该如何纠正第一版本,以获得它的工作?

How should I correct the 1st version to get it to work?

推荐答案

要获得引用正确的窍门是有eveything双引号的,除了 $(__ git_ps1(%S)) ,这是单引号。

The trick to get the quoting right is to have eveything double-quoted, except for $(__git_ps1 "(%s)"), which is single-quoted.

source ~/.git-completion.bash
function prompt
{
local WHITE="\[\033[1;37m\]"
local GREEN="\[\033[0;32m\]"
local CYAN="\[\033[0;36m\]"
local GRAY="\[\033[0;37m\]"
local BLUE="\[\033[0;34m\]"
export PS1="
${GREEN}\u${CYAN}@${BLUE}\h ${CYAN}\w"' $(__git_ps1 "(%s)") '"${GRAY}"
}
prompt

这是另一种解决办法是更换 $( \\ $(在code中的问题

An alternative solution is to replace $( with \$( in the code in the question.

背景资料:两个换人发生:首先在出口PS1 =...时间,后来当显示提示。要执行 __ git_ps1 每次显示提示时间,所以你必须确保第一次换人保持 $(...)完好无损。所以,你要么写'$(...)\\ $(...)。这是我提出的解决方案背后的两个基本思路。

Background information: Two substitutions take place: first at export PS1="..." time, and later when the prompt is displayed. You want to execute __git_ps1 each time the prompt is displayed, so you have to make sure that the first substitution keeps $(...) intact. So you write either '$(...)' or "\$(...)". These are the two basic ideas behind the solutions I've proposed.

这篇关于附加Git的分支名到命令提示符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆