我在peepcode的一个截屏视频中使用了.bashrc。
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
# set a fancy prompt (non-color, overwrite the one in /etc/profile)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
# Commented out, don't overwrite xterm -T "title" -n "icontitle" by default.
# If this is an xterm set the title to user@host:dir
#case "$TERM" in
#xterm*|rxvt*)
# PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
# ;;
#*)
# ;;
#esac
# enable bash completion in interactive shells
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
# . /etc/bash_completion
#fi
# sudo hint
if [ ! -e "$HOME/.sudo_as_admin_successful" ]; then
case " $(groups) " in *\ admin\ *)
if [ -x /usr/bin/sudo ]; then
cat <<-EOF
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
EOF
fi
esac
fi
# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found -o -x /usr/share/command-not-found ]; then
function command_not_found_handle {
# check because c-n-f could've been removed in the meantime
if [ -x /usr/lib/command-not-found ]; then
/usr/bin/python /usr/lib/command-not-found -- $1
return $?
elif [ -x /usr/share/command-not-found ]; then
/usr/bin/python /usr/share/command-not-found -- $1
return $?
else
return 127
fi
}
fi
. ~/bin/bash_colors.sh
#load bash aliases
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
# Unbreak broken, non-colored terminal
export TERM='xterm-color'
alias ls='ls -G'
alias ll='ls -lG'
export LSCOLORS="ExGxBxDxCxEgEdxbxgxcxd"
export GREP_OPTIONS="--color"
# Erase duplicates in history
export HISTCONTROL=erasedups
# Store 10k history entries
export HISTSIZE=10000
# Append to the history file when exiting instead of overwriting it
shopt -s histappend
function minutes_since_last_commit {
now=`date +%s`
last_commit=`git log --pretty=format:'%at' -1`
seconds_since_last_commit=$((now-last_commit))
minutes_since_last_commit=$((seconds_since_last_commit/60))
echo $minutes_since_last_commit
}
grb_git_prompt() {
local g="$(__gitdir)"
if [ -n "$g" ]; then
local MINUTES_SINCE_LAST_COMMIT=`minutes_since_last_commit`
if [ "$MINUTES_SINCE_LAST_COMMIT" -gt 30 ]; then
local COLOR=${RED}
elif [ "$MINUTES_SINCE_LAST_COMMIT" -gt 10 ]; then
local COLOR=${YELLOW}
else
local COLOR=${GREEN}
fi
local SINCE_LAST_COMMIT="${COLOR}$(minutes_since_last_commit)m${NORMAL}"
# The __git_ps1 function inserts the current git branch where %s is
local GIT_PROMPT=`__git_ps1 "(%s|${SINCE_LAST_COMMIT})"`
echo ${GIT_PROMPT}
fi
}
export PS1='$(grb_git_prompt)$ '
gd() { git diff $* | view -; }
gdc() { gd --cached $*; }
source ~/bin/git-completion.bash
我可以确认错误是在grb_git_promit中。会发生什么是shell中输入的第二行是覆盖第一行。任何人都可以帮我这个吗?
编辑:
grb_git_prompt() {
local g="$(__gitdir)"
if [ -n "$g" ]; then
local MINUTES_SINCE_LAST_COMMIT=`minutes_since_last_commit`
if [ "$MINUTES_SINCE_LAST_COMMIT" -gt 30 ]; then
local COLOR="\[\033[0;31m\]"
elif [ "$MINUTES_SINCE_LAST_COMMIT" -gt 10 ]; then
local COLOR="\[\033[0;37m\]"
else
local COLOR="\[\033[0;32m\]"
fi
local SINCE_LAST_COMMIT="${COLOR}$(minutes_since_last_commit)m$"
# The __git_ps1 function inserts the current git branch where %s is
echo "$(__git_ps1 '(%s|')${SINCE_LAST_COMMIT}"
fi
}
答案 0 :(得分:27)
如果提示中有颜色和其他非打印转义序列,则必须使用转义单括号将它们包围起来。这是一个简单的例子:
PS1='\[\033[38;5;1m\]some red text\[\033[0m\]\$ '
这将导致提示以红色显示“一些红色文字”,并以默认颜色显示美元符号(或“#”)。
这是另一种做同样事情的方法:
red='\033[38;5;1m'
none='\033[0m'
PS1='\[$red\]some red text\[$none\]\$ '
顺便说一下,为了更便携地完成这项工作并且变量的构建不那么复杂:
red=$(tput setaf 1)
none=$(tput sgr0)
有关设置的详情,请参阅man 5 terminfo
。