效果預(yù)覽如下:
image
將下列代碼粘貼到linux用戶目錄下的 ~/.bashrc
尾部即可
#
# 追加到.bashrc文件底部即可
# Append to end of .bashrc file
#
# 前景 背景 顏色
# ---------------------------
# 30 40 黑色
# 31 41 紅色
# 32 42 綠色
# 33 43 黃色
# 34 44 藍色
# 35 45 紫紅色
# 36 46 青藍色
# 37 47 白色
#
# 代碼 意義
# -----------------
# 0 OFF
# 1 高亮顯示
# 4 underline
# 5 閃爍
# 7 反白顯示
# 8 不可見
_wjk_color_none='\[\033[00m\]'
_wjk_color_light_yellow='\[\033[1;33m\]'
_wjk_color_dark_yellow='\[\033[0;33m\]'
_wjk_color_light_blue='\[\033[1;34m\]'
_wjk_color_light_green='\[\033[1;32m\]'
_wjk_color_dark_green='\[\033[0;32m\]'
_wjk_color_light_purple='\[\033[1;35m\]'
_wjk_color_light_red='\[\033[1;31m\]'
_wjk_inner_color_none='\033[00m'
_wjk_inner_color_light_yellow='\033[1;33m'
_wjk_inner_color_dark_yellow='\033[0;33m'
_wjk_inner_color_light_blue='\033[1;34m'
_wjk_inner_color_light_green='\033[1;32m'
_wjk_inner_color_dark_green='\033[0;32m'
_wjk_inner_color_light_purple='\033[1;35m'
_wjk_inner_color_dark_purple='\033[0;35m'
_wjk_inner_color_light_red='\033[1;31m'
_wjk_inner_color_dark_red='\033[0;31m'
# ps1
_wjk_ps1() {
local user_indicator='$'
if [ $UID -eq "0" ] ; then
user_indicator='#'
fi
local ps1_user_segment="${_wjk_color_light_yellow}${debian_chroot:+($debian_chroot)}\u${_wjk_color_none}"
local ps1_host_segment="${_wjk_color_light_blue}\$(_wjk_custom_text)${_wjk_color_none}"
local ps1_path_segment="${_wjk_color_light_green}\w${_wjk_color_none}"
local ps1_git_segment="\$(_wjk_git_info)${_wjk_color_none}"
echo "${ps1_user_segment}@${ps1_host_segment} ${ps1_path_segment} ${ps1_git_segment}${_wjk_color_light_red}${user_indicator}${_wjk_color_none} "
}
# 獲取當前時間
_wjk_current_datetime() {
echo $(date "+%Y-%m-%d %r")
}
# 主機或者自定義文字
_wjk_custom_text() {
local hostnamestr=`hostname`
if [[ -e '/etc/.wjk.bash.customtext' ]] ; then
hostnamestr=`/etc/.wjk.bash.customtext`
fi
echo $hostnamestr
}
# git的倉庫分支信息
function _wjk_git_info {
if [[ "$(type -t git)" = "" ]] ; then
return
fi
if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
local ref bare
if [[ "$(type -t git)" != "" ]] ; then
ref=$(git symbolic-ref --short -q HEAD 2> /dev/null)
bare=$(git config --bool core.bare)
fi
local repo_path=$(git rev-parse --git-dir 2>/dev/null)
local current_mode=""
local branch_color=$_wjk_inner_color_light_purple
if [[ -e "${repo_path}/BISECT_LOG" ]]; then
current_mode=" <B>"
branch_color=$_wjk_inner_color_dark_purple
elif [[ -e "${repo_path}/MERGE_HEAD" ]]; then
current_mode=" >M<"
branch_color=$_wjk_inner_color_dark_purple
elif [[ -e "${repo_path}/rebase" || -e "${repo_path}/rebase-apply" || -e "${repo_path}/rebase-merge" || -e "${repo_path}/../.dotest" ]]; then
current_mode=" >R>"
branch_color=$_wjk_inner_color_dark_purple
else
if [[ "$(git status --porcelain -uno 2>/dev/null | head -n 1)" != "" ]] ; then
current_mode=" ?"
fi
fi
local iscommitnode=""
if [[ "$ref" = "" ]] ; then
ref=$(git log --oneline -1 2>/dev/null | cut -d " " -f 1)
if [ ${ref} ] ; then
iscommitnode="yes"
# ref="${_wjk_inner_color_dark_yellow} (($ref))"
ref=" (($ref))"
fi
else
if $bare; then
branch_color=$_wjk_inner_color_dark_green
fi
# ref="${branch_color} (${ref}${_wjk_inner_color_dark_yellow}${current_mode}${branch_color})${_wjk_inner_color_none}"
ref="(${ref}${current_mode})"
fi
# echo -e $ref
echo $ref
fi
}
export PS1=$(_wjk_ps1)
將代碼粘貼之后, 重新登錄ssh即可生效。
如果不想重新登錄也可以直接運行命令使其生效:
source ~/.bashrc
主機名默認是系統(tǒng)主機名, 可以自己定義自己想要的字符串疚俱。其shell文件位于/etc/.wjk.bash.customtext
.
可以創(chuàng)建該文件, 然后寫入以下內(nèi)容:
#!/bin/bash
echo "Jeencol的虛擬機環(huán)境"
給文件添加可執(zhí)行權(quán)限即可:
sudo chmod ogu+x /etc/.wjk.bash.customtext