Git bash提示换行符断开

Git bash提示换行符断开,git,bash,prompt,Git,Bash,Prompt,我正在OsX上自定义bash提示符,以包括git分支和一些分支状态标记。这会破坏换行 我知道这一点,但在函数中这样做确实会显示\[和\]很普通 如何在这些函数中转义这些序列? 免责声明:这是我第一次尝试bash脚本 function parse_git_dirty { # TODO make git status response a variable # [branch+] : working dir has staged changes if [[ $(git status 2&

我正在OsX上自定义bash提示符,以包括git分支和一些分支状态标记。这会破坏换行

我知道这一点,但在函数中这样做确实会显示\[和\]很普通

如何在这些函数中转义这些序列?

免责声明:这是我第一次尝试bash脚本

function parse_git_dirty {
  # TODO make git status response a variable
  # [branch+] : working dir has staged changes
  if [[ $(git status 2> /dev/null | grep "to be committed") ]]
  then S=$S"$(tput setaf 2)+$(tput sgr0)"
  fi
  # [branch+] : working dir has unstaged changes
  if [[ $(git status 2> /dev/null | grep "not staged for commit") ]]
  then S=$S"$(tput setaf 1)+$(tput sgr0)"
  fi
  # [branch+] : working dir has untracked files
  if [[ $(git status 2> /dev/null | grep "tracked files") ]]
  then S=$S"$(tput setaf 1)+$(tput sgr0)"
  fi
  # [branch<] : local branch is behind origin
  if [[ $(git status 2> /dev/null | grep "Your branch is behind") ]]
  then S=$S"$(tput setaf 5)<$(tput sgr0)"
  fi
  # [branch>] : local branch is ahead origin
  if [[ $(git status 2> /dev/null | grep "branch is ahead of") ]]
  then S=$S"$(tput setaf 5)>$(tput sgr0)"
  fi
  # [branch<>] : branches have diverged
  if [[ $(git status 2> /dev/null | grep "have diverged") ]]
  then S=$S"$(tput setaf 5)<>$(tput sgr0)"
  fi
  echo $S
}
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
function show_git_branch {
  if [[ $(parse_git_branch) ]]
  then echo "$(tput setaf 2)($(tput sgr0)$(parse_git_branch)$(parse_git_dirty)$(tput setaf 2))$(tput sgr0)"
  fi
}
export PS1="\u\[$(tput setaf 2)\]@\[$(tput sgr0)\]\h\[$(tput setaf 2)\]:\[$(tput sgr0)\]\W\[\$(show_git_branch)\] "
函数parse\u git\u dirty{
#TODO使git状态响应成为变量
#[分支机构+]:工作目录已进行阶段性更改
如果[$(git状态2>/dev/null | grep“待提交”)]]
然后S=$S“$(tput setaf 2)+$(tput sgr0)”
fi
#[分支机构+]:工作目录有未老化的更改
如果[[$(git状态2>/dev/null | grep“未进行提交”)]]
然后S=$S“$(tput setaf 1)+$(tput sgr0)”
fi
#[branch+]:工作目录有未跟踪的文件
如果[$(git状态2>/dev/null | grep“跟踪文件”)]]
然后S=$S“$(tput setaf 1)+$(tput sgr0)”
fi
#[branch/dev/null | grep“您的分支机构落后了”)]]
然后S=$S“$(tput setaf 5)$(tput sgr0)”
fi
#[分支]:分支已经分化
如果[[$(git状态2>/dev/null | grep“有分歧”)]]
然后S=$S“$(tput setaf 5)$(tput sgr0)”
fi
echo$S
}
函数parse_git_分支{
git分支--无颜色2>/dev/null | sed-e'/^[^*]/d'-e's/*\(.*)/\1/'
}
函数show\u git\u分支{
if[$(解析git分支)]]
然后回显“$(tput setaf 2)($(tput sgr0)$(parse_git_分支)$(parse_git_dirty)$(tput setaf 2))$(tput sgr0)”
fi
}
导出PS1=“\u\[$(tput SETAF2)\]@\[$(tput sgr0)\]\h\[$(tput SETAF2)\]:\[$(tput sgr0)\]\W\[\$(显示git分支)\]”

您需要在赋值中的值周围加上单引号:

export PS1='\u\[$(tput setaf 2)\]@\[$(tput sgr0)\]\h\[$(tput setaf 2)\]:\[$(tput sgr0)\]\W\[$(show_git_branch)\] '

由于在发出提示时会对内容进行评估,因此您不需要像在其他情况下那样使用双引号。

多亏了Dennis,更正后的代码是:

function parse_git_dirty {
  # TODO make git status response a variable
  # [branch+] : working dir has staged changes
  if [[ $(git status 2> /dev/null | grep "to be committed") ]]
  then S=$S"$(tput setaf 2)+$(tput sgr0)"
  fi
  # [branch+] : working dir has unstaged changes
  if [[ $(git status 2> /dev/null | grep "not staged for commit") ]]
  then S=$S"$(tput setaf 1)+$(tput sgr0)"
  fi
  # [branch+] : working dir has untracked files
  if [[ $(git status 2> /dev/null | grep "tracked files") ]]
  then S=$S"$(tput setaf 1)+$(tput sgr0)"
  fi
  # [branch<] : local branch is behind origin
  if [[ $(git status 2> /dev/null | grep "Your branch is behind") ]]
  then S=$S"$(tput setaf 5)<$(tput sgr0)"
  fi
  # [branch>] : local branch is ahead origin
  if [[ $(git status 2> /dev/null | grep "branch is ahead of") ]]
  then S=$S"$(tput setaf 5)>$(tput sgr0)"
  fi
  # [branch<>] : branches have diverged
  if [[ $(git status 2> /dev/null | grep "have diverged") ]]
  then S=$S"$(tput setaf 5)<>$(tput sgr0)"
  fi
  echo $S
}
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'
}
function show_git_branch {
  if [[ $(parse_git_branch) ]]
  then echo "$(tput setaf 2)($(tput sgr0)$(parse_git_branch)$(parse_git_dirty)$(tput setaf 2))$(tput sgr0)"
  fi
}
export PS1='\u\[$(tput setaf 2)\]@\[$(tput sgr0)\]\h\[$(tput setaf 2)\]:\[$(tput sgr0)\]\W\[$(show_git_branch)\] '
函数parse\u git\u dirty{
#TODO使git状态响应成为变量
#[分支机构+]:工作目录已进行阶段性更改
如果[$(git状态2>/dev/null | grep“待提交”)]]
然后S=$S“$(tput setaf 2)+$(tput sgr0)”
fi
#[分支机构+]:工作目录有未老化的更改
如果[[$(git状态2>/dev/null | grep“未进行提交”)]]
然后S=$S“$(tput setaf 1)+$(tput sgr0)”
fi
#[branch+]:工作目录有未跟踪的文件
如果[$(git状态2>/dev/null | grep“跟踪文件”)]]
然后S=$S“$(tput setaf 1)+$(tput sgr0)”
fi
#[branch/dev/null | grep“您的分支机构落后了”)]]
然后S=$S“$(tput setaf 5)$(tput sgr0)”
fi
#[分支]:分支已经分化
如果[[$(git状态2>/dev/null | grep“有分歧”)]]
然后S=$S“$(tput setaf 5)$(tput sgr0)”
fi
echo$S
}
函数parse_git_分支{
git分支--无颜色2>/dev/null | sed-e'/^[^*]/d'-e's/*\(.*)/\1/'
}
函数show\u git\u分支{
if[$(解析git分支)]]
然后回显“$(tput setaf 2)($(tput sgr0)$(parse_git_分支)$(parse_git_dirty)$(tput setaf 2))$(tput sgr0)”
fi
}
导出PS1='\u\[$(tput SETAF2)\]@\[$(tput sgr0)\]\h\[$(tput SETAF2)\]:\[$(tput sgr0)\]\W\[$(显示git分支)\]'

我很高兴听到您已经用您的版本解决了这个问题,但我认为可能值得指出的是,git已经发布了一个名为
\uuu git\u ps1
的有用且经过深思熟虑的bash函数,您可以将它包含在
ps1
中。例如,您可以这样使用它:

 export PS1='blah blah blah$(__git_ps1 " (%s)") '
如果您不在git存储库中,
$(%s)”
将变成空字符串。但是,如果是,则将使用格式字符串。这通常会显示你当前的分支,但是如果你处于合并的中间或者一个重新显示的基础,则

默认情况下,
\uuuu git\u ps1
不会显示树是否脏或是否存在未跟踪的文件,因为在某些存储库中,这可能会使bash提示符的显示速度慢得令人恼火。但是,如果您还想查看此信息,则如果将
GIT\u PS1\u showdrityState
GIT\u PS1\u shountracked files
设置为非空,则会显示这些信息


您可以在顶部找到更多信息。

不确定问题到底是什么,您的代码似乎工作正常,使用它我会得到以下提示,hutcho@hutcho-M17x:数学(硕士+)。顺便说一句,这是一个很酷的想法。我有一些(并非所有不寻常的)存储库,
git status
大约需要40秒才能完成,并且对于每个bash提示,您的代码将运行
git status
六次!我在下面添加了一个答案,其中提到了
\uuuu git\u ps1
,它可能会满足您的需要,并且可以针对输出中的不同细节级别进行配置。谢谢!我只需要删除show_git_分支函数的espaing,它就工作了。@BenoîtPointet:很抱歉我错过了。我已经更正了我的答案。有趣的是,我重新发明了轮子,在肮脏的州和本地与远程状态下,几乎达到了与uu git_ps1相同的标记。