Macos 如何在终端中显示当前分支和文件夹路径?
我一直在看一些TeamTreehouse的视频,当使用Git时,它们有一个非常好看的终端 例如,他们有(类似的东西): 我的终端如何向我显示我所在分支的一些有用信息,并使用颜色来区分我想要的数据位?有什么我还没有找到的插件吗Macos 如何在终端中显示当前分支和文件夹路径?,macos,git,terminal,git-branch,Macos,Git,Terminal,Git Branch,我一直在看一些TeamTreehouse的视频,当使用Git时,它们有一个非常好看的终端 例如,他们有(类似的东西): 我的终端如何向我显示我所在分支的一些有用信息,并使用颜色来区分我想要的数据位?有什么我还没有找到的插件吗 我使用的是Mac OSX 10.8,它与插件无关。这是关于壳中的即时技巧 要了解bash中的酷设置,请查看这家伙的dotfiles项目: 要获得一个奇特的提示,请在~/.bash\u配置文件或~/.bashrc中包含.bash\u提示 要获得与您的问题完全相同的提示,请更
我使用的是Mac OSX 10.8,它与插件无关。这是关于壳中的即时技巧 要了解bash中的酷设置,请查看这家伙的
dotfiles
项目:
要获得一个奇特的提示,请在~/.bash\u配置文件
或~/.bashrc
中包含.bash\u提示
要获得与您的问题完全相同的提示,请更改末尾的export PS1
行。bash\u prompt
如下所示:
export PS1="\[${BOLD}${MAGENTA}\]\u\[$WHITE\]@\[$ORANGE\]\h\[$WHITE\]: [\[$GREEN\]\w\[$WHITE\]\$([[ -n \$(git branch 2> /dev/null) ]] && echo \" - \")\[$PURPLE\]\$(parse_git_branch)\[$WHITE\]] \$ \[$RESET\]"
大约一个月前,我使用了这个存储库中的所有.bash*
文件,这对我来说非常有用
对于Git,在.gitconfig
中有额外的优点
由于您是mac用户,因此在
.osx
中有更多的优点。安装在您系统上的git软件包包括bash文件,可帮助您创建信息提示。要创建颜色,需要在提示符中插入端子转义序列。最后一个要素是使用内置变量prompt\u命令在执行每个命令后更新提示符
编辑~/.bashrc以包含以下内容,您应该在问题中得到提示,对一些颜色差异进行模化
#
# Git provides a bash file to create an informative prompt. This is its standard
# location on Linux. On Mac, you should be able to find it under your Git
# installation. If you are unable to find the file, I have a copy of it on my GitHub.
#
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/40-git-prompt.sh
#
source /usr/share/git/completion/git-prompt.sh
#
# Next, we need to define some terminal escape sequences for colors. For a fuller
# list of colors, and an example how to use them, see my bash color file on my GitHub
# and my coniguration for colored man pages.
#
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/10-colors.sh
# https://github.com/chadversary/home/blob/42cf697ba69d4d474ca74297cdf94186430f1384/.config/kiwi-profile/40-less.sh
#
color_start='\e['
color_end='m'
color_reset='\e[0m'
color_bg_blue='44'
#
# To get a fancy git prompt, it's not sufficient to set PS1. Instead, we set PROMPT_COMMAND,
# a built in Bash variable that gets evaluated before each render of the prompt.
#
export PROMPT_COMMAND="PS1=\"\${color_start}\${color_bg_blue}\${color_end}\u@\h [\w\$(__git_ps1 \" - %s\")]\${color_reset}\n\$ \""
#
# If you find that the working directory that appears in the prompt is ofter too long,
# then trim it.
#
export PROMPT_DIRTRIM=3
简单方法
在您喜爱的编辑器中打开~/.bash\u profile
,并在底部添加以下内容
Git分支处于提示状态
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "
要扩展现有的优秀答案,获得优秀终端的一个非常简单的方法是使用开源Dotfiles项目
在OSX和Linux上安装非常简单。在终端中运行以下命令
git clone https://github.com/mathiasbynens/dotfiles.git && cd dotfiles && source bootstrap.sh
这将是:
cd
放入文件夹中- 最后一个命令的退出状态(如果不是0)
- 根发生显著变化
-stylersync
user@host:用于复制粘贴的路径名
- Git分支、索引、修改、未跟踪和上游信息
- 漂亮的颜色
~/.bashrc
:
#
#设置提示#
#
#选择显示的git信息,有关更多信息,请参阅/usr/share/git/completion/git-prompt.sh
导出GIT_PS1_SHOWDIRTYSTATE=1#'*'=未分级,'+'=分级
导出GIT_PS1_showstate=1#'$'=stashed
导出GIT_PS1_SHOWUNTRACKEDFILES=1#'%'=untracked
导出GIT_PS1_SHOWUPSTREAM=“verbose”#“u=”=无差异,“u+1”=提前1次提交
导出GIT_PS1_StateSeptor=''分支和索引状态之间没有空格
导出GIT_PS1_descripe_STYLE=“descripe”#分离头样式:
#包含相对于较新注释标记(v1.6.3.2~35)的内容
#相对于较新的标记或分支的分支(master~4)
#相对于较旧的注释标签(v1.6.3.1-13-gdd42c2f)进行描述
#默认的精确eatching标记
#检查我们是否支持颜色
__颜色_启用(){
本地-i颜色=$(tput颜色2>/dev/null)
[[$?-eq 0]]&&[$colors-gt 2]]
}
取消设置着色提示和启用着色提示=1
__set_bash_prompt()
{
local exit=“$?”#保存最后一个命令的退出状态
#PS1由$PreGitPS1++$PostGitPS1组成
本地PreGitPS1=“${debian_chroot:+($debian_chroot)}”
本地PostGitPS1=“”
如果[$\u着色\u提示]];则
导出GIT\u PS1\u ShowColorHits=1
#将颜色代码包装在\[和\]之间,以便
#bash计算换行的正确字符数:
本地红色=“\[\e[0;31m\]”;本地繁殖=“\[\e[1;31m\]”
本地Gre='\[\e[0;32m\]';本地BGre='\[\e[1;32m\]'
本地Yel='\[\e[0;33m\]';本地BYel='\[\e[1;33m\]'
本地Blu='\[\e[0;34m\]';本地BBlu='\[\e[1;34m\]'
本地Mag='\[\e[0;35m\]';本地BMag='\[\e[1;35m\]'
本地Cya='\[\e[0;36m\]';本地BCya='\[\e[1;36m\]'
本地Whi='\[\e[0;37m\]';本地BWhi='\[\e[1;37m\]'
本地无=“\[\e[0m\]”返回默认颜色
#没有用户名和明亮的颜色,如果根
如果[${EUID}==0]];则
PreGitPS1+=“$h\h”
其他的
PreGitPS1+=“$Red\u@\h$None:”
fi
PreGitPS1+=“$Blu\w$None”
否则就没有颜色了
#设置提示,如:ravi@boxy:~/prj/sample\u应用程序
未设置GIT\u PS1\u显示颜色提示
PreGitPS1=“${debian_chroot:+($debian_chroot)}\u@\h:\w”
fi
#现在在git的状态之后构建部件
#突出显示非标准出口代码
如果[[$exit!=0]];则
PostGitPS1=“$Red[$exit]”
fi
#如果是根,则更改提示的颜色
如果[${EUID}==0]];则
PostGitPS1+=“$reded”\$“$None”
其他的
PostGitPS1+=“$Mag”\$”“$None”
fi
#从$PreGitPS1++$PostGitPS1设置PS1
__git_ps1“$PreGitPS1”“$PostGitPS1”“(%s)”
#回显“$PS1=”$PS1”#调试
#defaut Linux Mint 17.2用户提示:
#PS1='${debian\u chroot:+($debian\u chroot)}\[\033[01;32m\]\u\h\[\033[01;340m\]\w\[\033[0亿\]$($debian\u chroot)\[\033[01;34m\]\w\[\033[0亿\]$(%s)]\$'
}
#这会告诉bash在每个命令之后重新解释PS1,我们需要这样做
#需要,因为_git_ps1将返回不同的文本和颜色
PROMPT\u命令=\uuuu set\u bash\u PROMPT
只需安装oh my zsh
插件,如中所述
它在macOS和Linux上工作得最好
基本安装
Oh My Zsh是通过在终端中运行以下命令之一安装的。您可以使用
git clone https://github.com/mathiasbynens/dotfiles.git && cd dotfiles && source bootstrap.sh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)"
# \[\e[0m\] resets the color to default color
reset_color='\[\e[0m\]'
# \[\033[33m\] sets the color to yellow
path_color='\[\033[33m\]'
# \e[0;32m\ sets the color to green
git_clean_color='\[\e[0;32m\]'
# \e[0;31m\ sets the color to red
git_dirty_color='\[\e[0;31m\]'
# determines if the git branch you are on is clean or dirty
git_prompt ()
{
# Is this a git directory?
if ! git rev-parse --git-dir > /dev/null 2>&1; then
return 0
fi
# Grab working branch name
git_branch=$(git branch 2>/dev/null| sed -n '/^\*/s/^\* //p')
# Clean or dirty branch
if git diff --quiet 2>/dev/null >&2; then
git_color="${git_clean_color}"
else
git_color="${git_dirty_color}"
fi
echo " [$git_color$git_branch${reset_color}]"
}
export PS1="${path_color}\w\[\e[0m\]$(git_prompt)\n"
1) Prompt the path you're in, in color: path_color.
2) Tell you which branch are you.
3) Color the name of the branch based on the status of the branch with git_clean_color
for a clean work directory and git_dirty_color for a dirty one.
4) The brackets should stay in the default color you established in your computer.
5) Puts the prompt in the next line for readability.
override_git_prompt_colors() {
GIT_PROMPT_THEME_NAME="Custom"
# Clean or dirty branch
if git diff --quiet 2>/dev/null >&2; then
GIT_PROMPT_BRANCH="${Green}"
else
GIT_PROMPT_BRANCH="${Red}"
fi
}
reload_git_prompt_colors "Custom"
parse_git_branch() {
git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF='%f'
COLOR_USR='%F{243}'
COLOR_DIR='%F{197}'
COLOR_GIT='%F{39}'
NEWLINE=$'\n'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n@%M ${COLOR_DIR}%d ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}${NEWLINE}%% '
$ git branch --show-current
master
# This function returns '' in all below cases:
# - git not installed or command not found
# - not in a git repo
# - in a git repo but not on a branch (HEAD detached)
get_git_current_branch() {
git branch --show-current 2> /dev/null
}
$ git version
git version 2.23.0
touch .zprofile
nano .zprofile
source ~/.bash_profile
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\u@\h \[\033[32m\]\w - \$(parse_git_branch)\[\033[00m\] $ "
git_stuff() {
git_branch=$(git branch --show-current 2> /dev/null)
if [[ $git_branch == "" ]];then
echo -e ""
elif [[ $git_branch == *"Nocommit"* ]];then
echo -e "No commits"
else
echo -e "$git_branch"
fi
}
prompt() {
PS1="\e[2m$(date +%H:%M:%S.%3N) \e[4m$(git_stuff)\033[0m\n\w$ "
}
PROMPT_COMMAND=prompt
System Preference -> Users & Groups -> Right click user user
-> Advanced Option -> Login shell -> /bin/bash
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'
}
export PS1="\W\[\033[33m\]\$(parse_git_branch)\[\033[00m\] $ "