热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用git当前分支和颜色的PS1行。-PS1linewithgitcurrentbranchandcolors

HereismycurrentPS1这是我现在的PS1。exportPS1[\u@\h\W$(__git_ps1(%s))]\$HowcanIdispla

Here is my current PS1

这是我现在的PS1。

export PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

How can I display the current branch in a different color?

如何以不同的颜色显示当前分支?

12 个解决方案

#1


30  

You can wrap the part that you want in colour with the following:

你可以把你想要的部分包装成彩色的:

\e[0;32m - sets colour (in this case, to green)

\e[0;32m -设置颜色(在本例中为绿色)

\e[m - sets colour back to the default

\e[m -设置颜色返回默认值。

For example, this sets the prompt to the last token of the current path, in green, followed by $ in the default colour:

例如,这会将提示符设置为当前路径的最后一个标记,在绿色中,然后是$ in默认颜色:

export PS1='\e[0;32m\w\e[m $'

Other colours are available too. Have a look at this article under colorization for a comprehensive list of alternatives.

还有其他颜色可供选择。看一看这篇文章,下面是一份综合的备选方案。

#2


94  

Here is, part by part (and no ruby):

以下是部分(不含ruby):

function color_my_prompt {
    local __user_and_host="\[\033[01;32m\]\u@\h"
    local __cur_location="\[\033[01;34m\]\w"
    local __git_branch_color="\[\033[31m\]"
    #local __git_branch="\`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`"
    local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E  s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`'
    local __prompt_tail="\[\033[35m\]$"
    local __last_color="\[\033[00m\]"
    export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
color_my_prompt

Looks like this (with my own terminal palette):

看起来像这样(用我自己的终端调色板):

colored prompt

Also, see this and this article

另外,请参见本文和本文。

#3


19  

Here is my PS1 line:

这是我的PS1线:

\n\[\e[1;37m\]|-- \[\e[1;32m\]\u\[\e[0;39m\]@\[\e[1;36m\]\h\[\e[0;39m\]:\[\e[1;33m\]\w\[\e[0;39m\]\[\e[1;35m\]$(__git_ps1 " (%s)")\[\e[0;39m\] \[\e[1;37m\]--|\[\e[0;39m\]\n$

alt text

#4


14  

function pc {
  [ -d .git ] && git name-rev --name-only @
}
PS1='\e];\s\a\n\e[33m\w \e[36m$(pc)\e[m\n$ '

ps1

Source

#5


10  

This is my PS1 solution

这是我的PS1解。

Looks great on a mac with the Novel theme. Sorry but my indentation got munged a bit. Hack it till you like it.

在mac上看起来很棒,主题很新颖。对不起,我的压痕被咬了。直到你喜欢为止。

function we_are_in_git_work_tree {
    git rev-parse --is-inside-work-tree &> /dev/null
}

function parse_git_branch {
    if we_are_in_git_work_tree
    then
    local BR=$(git rev-parse --symbolic-full-name --abbrev-ref HEAD 2> /dev/null)
    if [ "$BR" == HEAD ]
    then
        local NM=$(git name-rev --name-only HEAD 2> /dev/null)
        if [ "$NM" != undefined ]
        then echo -n "@$NM"
        else git rev-parse --short HEAD 2> /dev/null
        fi
    else
        echo -n $BR
    fi
    fi
}

function parse_git_status {
    if we_are_in_git_work_tree
    then 
    local ST=$(git status --short 2> /dev/null)
    if [ -n "$ST" ]
    then echo -n " + "
    else echo -n " - "
    fi
    fi
}

function pwd_depth_limit_2 {
    if [ "$PWD" = "$HOME" ]
    then echo -n "~"
    else pwd | sed -e "s|.*/\(.*/.*\)|\1|"
    fi
}

COLBROWN="\[\033[1;33m\]"
COLRED="\[\033[1;31m\]"
COLCLEAR="\[\033[0m\]"

# export all these for subshells
export -f parse_git_branch parse_git_status we_are_in_git_work_tree pwd_depth_limit_2
export PS1="$COLRED<$COLBROWN \$(pwd_depth_limit_2)$COLRED\$(parse_git_status)$COLBROWN\$(parse_git_branch) $COLRED>$COLCLEAR "
export TERM="xterm-color"

If you are checked out at a branch, you get the branch name.

如果在一个分支中签出,就会得到分支名称。

If you are in a just init'd git project, you just get '@'

如果你在一个初始化的git项目中,你就会得到@

If you are headless, you get a nice human name relative to some branch or tag, with an '@' preceding the name.

如果你是无头的,你会得到一个很好的人的名字相对于某个分支或标签,在名字前面有一个“@”。

If you are headless and not an ancestor of some branch or tag you just get the short SHA1.

如果你是无头的,而不是某个分支或标签的祖先,你只会得到短的SHA1。

In addition, a red '-' signifies a clean work dir and index, and a red '+' signifies the opposite.

此外,红色的“-”表示干净的工作目录和索引,而红色的“+”表示相反。

_c

_c

#6


6  

My uber-powerful multi-line Linux prompt!

Put it either in your .bashrc or better: save it in /etc/bash-prompt and source it from your .bashrc.
Using tput is supposed to be the right way to do colors.

将其放入.bashrc或更好的:将其保存在/etc/bash-prompt中,并从您的.bashrc中获取它。使用tput应该是做颜色的正确方法。

#!/bin/bash

set_prompt()
{
   local last_cmd=$?
   local txtreset='$(tput sgr0)'
   local txtbold='$(tput bold)'
   local txtblack='$(tput setaf 0)'
   local txtred='$(tput setaf 1)'
   local txtgreen='$(tput setaf 2)'
   local txtyellow='$(tput setaf 3)'
   local txtblue='$(tput setaf 4)'
   local txtpurple='$(tput setaf 5)'
   local txtcyan='$(tput setaf 6)'
   local txtwhite='$(tput setaf 7)'
   # unicode "✗"
   local fancyx='\342\234\227'
   # unicode "✓"
   local checkmark='\342\234\223'
   # Line 1: Full date + full time (24h)
   # Line 2: current path
   PS1="\[$txtbold\]\[$txtwhite\]\n\D{%A %d %B %Y %H:%M:%S}\n\[$txtgreen\]\w\n"
   # User color: red for root, yellow for others
   if [[ $EUID == 0 ]]; then
       PS1+="\[$txtred\]"
   else
       PS1+="\[$txtyellow\]"   
   fi
   # Line 3: user@host
   PS1+="\u\[$txtwhite\]@\h\n"
   # Line 4: a red "✗" or a green "✓" and the error number
   if [[ $last_cmd == 0 ]]; then
      PS1+="\[$txtgreen\]$checkmark \[$txtwhite\](0)"
   else
      PS1+="\[$txtred\]$fancyx \[$txtwhite\]($last_cmd)"
   fi
   # Line 4: green git branch
   PS1+="\[$txtgreen\]$(__git_ps1 ' (%s)')\[$txtwhite\]"
   # Line 4: good old prompt, $ for user, # for root
   PS1+=" \\$ "
}
PROMPT_COMMAND='set_prompt'

#7


2  

Just invoke tput with the appropriate parameters. See the tput(1) and terminfo(5) man pages.

只需用适当的参数调用tput。查看tput(1)和终止(5)手册页。

#8


2  

Take a look at liquidprompt:

看一下liquidprompt:

https://github.com/nojhan/liquidprompt

https://github.com/nojhan/liquidprompt

Maybe a bit too heavy for your requirements, but you can switch features off by setting

也许对你的要求来说有点重,但是你可以通过设置来切换特性。

LP_ENABLE_...=0

LP_ENABLE_…= 0

See the documentation on above page.

参见上面的文档。

#9


1  

Here is a windows/cygwin/bash solution.
Add the following to your ~/.bashrc file.
xxx is the location of your local Git repository.

这是一个windows/cygwin/bash解决方案。将以下内容添加到您的~/中。bashrc文件。(xxx是本地Git存储库的位置。

GetBranch()
{
    cat /cygdrive/c/xxx/.git/HEAD | sed 's+^ref: refs/heads/++'
}
export PS1="\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[36m\]\$(GetBranch) \[\e[33m\]\w \[\e[0m\] \n\$ "

#10


1  

Here's mine

这里是我的

export PS1="\n\[\033[1;30m\][$$:$PPID - \j:\!\[\033[1;30m\]]\[\033[0;36m\] \T \
\[\033[1;30m\][\[\033[1;34m\]\u@\H\[\033[1;30m\]:\[\033[0;37m\]${SSH_TTY:-o} \
\[\033[0;32m\]+${SHLVL}\[\033[1;30m\]] \[\033[1;37m\]\w\[\033[0;37m\]\[\033[1;34m\]\$(__git_ps1 \" (%s)\") \[\033[0;37m\] \n\$ "

#11


0  

for more complicated status of git you can use some larger script

对于更复杂的git状态,可以使用更大的脚本。

#12


0  

For my Mac with the Homebrew theme, this works really well. Fully debugged and very fast, and completely self-contained

对于我的Mac和Homebrew主题,这是非常好的。完全调试,非常快,并且完全独立。

# Color prompt for git
reset=$(tput sgr0)
boldgreen=$(tput setaf 2)$(tput bold)
cyan=$(tput sgr0)$(tput setaf 6)
boldred=$(tput setaf 1)$(tput bold)
boldwhite=$(tput setaf 7)$(tput bold)
boldyellow=$(tput setaf 3)$(tput bold)

PARENCLR=$'\001\e[0;36m\002'
BRANCHCLR=$'\001\e[1;33m\002'

alias branchname="git branch 2>/dev/null | sed -ne 's/^* \(.*\)/ ${PARENCLR}(${BRANCHCLR}\1${PARENCLR}\)/p'"

GIT_STATUS='$(branchname)'

PROMPT_CHAR="\$"
PS1="\[$boldgreen\]\u\[$cyan\]::\[$boldred\]\h \[$cyan\]{\[$boldwhite\].../\W\[$cyan\]}\[$reset\]$GIT_STATUS\[$reset\]$PROMPT_CHAR "

Here's what it looks like: Mac + Homebrew + Color Git Prompt

以下是它的样子:Mac + Homebrew + Color Git提示。

If you want to have the full path (or remove the .../), then just change the -W to -w (and remove the .../).

如果您希望有完整的路径(或删除…/),那么只需将-W改为-W(并删除…/)。


推荐阅读
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • Thisworkcameoutofthediscussioninhttps://github.com/typesafehub/config/issues/272 ... [详细]
  • Iwanttogettheaverageofthedifferencebetweenthecurrenttimeandadatetimevalueinthedat ... [详细]
  • HDFS2.x新特性
    一、集群间数据拷贝scp实现两个远程主机之间的文件复制scp-rhello.txtroothadoop103:useratguiguhello.txt推pushscp-rr ... [详细]
  • 本文介绍了在wepy中运用小顺序页面受权的计划,包含了用户点击作废后的从新受权计划。 ... [详细]
  • 如何查询zone下的表的信息
    本文介绍了如何通过TcaplusDB知识库查询zone下的表的信息。包括请求地址、GET请求参数说明、返回参数说明等内容。通过curl方法发起请求,并提供了请求示例。 ... [详细]
  • 本文介绍了如何使用vue-awesome-swiper组件,包括在main.js中引入和使用swiper和swiperSlide组件,以及设置options和ref属性。同时还介绍了如何在模板中使用swiper和swiperSlide组件,并展示了如何通过循环渲染swipes数组中的数据,并使用picUrl属性显示图片。最后还介绍了如何添加分页器。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • Android实战——jsoup实现网络爬虫,糗事百科项目的起步
    本文介绍了Android实战中使用jsoup实现网络爬虫的方法,以糗事百科项目为例。对于初学者来说,数据源的缺乏是做项目的最大烦恼之一。本文讲述了如何使用网络爬虫获取数据,并以糗事百科作为练手项目。同时,提到了使用jsoup需要结合前端基础知识,以及如果学过JS的话可以更轻松地使用该框架。 ... [详细]
  • SpringBoot整合SpringSecurity+JWT实现单点登录
    SpringBoot整合SpringSecurity+JWT实现单点登录,Go语言社区,Golang程序员人脉社 ... [详细]
  • Python已成为全球最受欢迎的编程语言之一,然而Python程序的安全运行存在一定的风险。本文介绍了Python程序安全运行需要满足的三个条件,即系统路径上的每个条目都处于安全的位置、"主脚本"所在的目录始终位于系统路径中、若python命令使用-c和-m选项,调用程序的目录也必须是安全的。同时,文章还提出了一些预防措施,如避免将下载文件夹作为当前工作目录、使用pip所在路径而不是直接使用python命令等。对于初学Python的读者来说,这些内容将有所帮助。 ... [详细]
  • gitlab重置password
    ruby没怎么学,自己搭建的gitlab的rootpassword又忘了。幸好看见此帖子,试验okhttp:roland.kierkels.netgitreset-your-git ... [详细]
  • 工作经验谈之-让百度地图API调用数据库内容 及详解
    这段时间,所在项目中要用到的一个模块,就是让数据库中的内容在百度地图上展现出来,如经纬度。主要实现以下几点功能:1.读取数据库中的经纬度值在百度上标注出来。2.点击标注弹出对应信息。3 ... [详细]
  • 先记住几个专用名词,如下:Workspace:工作区IndexStage:暂存区Repository:仓库区(或本地仓库)Remote:远程仓库一、新建代码库#在当前目录新建一个G ... [详细]
  • 用户视图(查看运行状态或其他参数)系统视图(配置设备的系统参数)system-viewEntersystemview,returnuservi ... [详细]
author-avatar
mobiledu2502923963
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有