修改 mysql/oracle/bash/vimrc/cmd 提示符格式与颜色

时间:2022-04-28
本文章向大家介绍修改 mysql/oracle/bash/vimrc/cmd 提示符格式与颜色,主要内容包括(1)修改mysql提示符:、2、连接上客户端后,通过 prompt 命令修改。、3、在 MySQL 的配置文件中配置。、4、通过环境变量配置。、(2)oracle提示符修改:、(3)修改 linux bash shell vimrc 提示符:、(4)修改 windows cmd 提示符:、(5)bash color 详解、Escape sequences、Refer:、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

(1)修改mysql提示符:

MySQL 客户端的默认提示符是 "mysql>",基本上没什么实际作用。其实可以修改这个提示符,让它显示一些有用的信息,例如当前所在的数据库等。修改方法有四种,其中前两种只对当前连接有效,后两种则对所有连接有效。

我的建议:vi /etc/my.cnf

[mysql]
prompt=\u@\h: \d \r:\m:\s>

效果:

mysql -uroot -proot
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.5.22-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

root@localhost : (none) 07:15:23>use mysql
Database changed
root@localhost : mysql 07:15:30>
root@localhost : mysql 07:16:52>

 1、连接客户端时通过参数指定。

mysql --prompt="(u@h) [d]> "

 这样提示符就会变成 (user@host) [database]>

还有更多可以可以参考官方文档 4.5.1.2. mysql Commands

 2、连接上客户端后,通过 prompt 命令修改。

prompt (u@h) [d]>  

 3、在 MySQL 的配置文件中配置。

[mysql]  

prompt=(\u@\h) [\d]>\_  

 4、通过环境变量配置。

export MYSQL_PS1="(u@h) [d]> "

  其中h表示主机而d表示数据库,更多设置如下(以下摘自MySQL手册) 

 v  服务器版本 

d  当前的数据库 

h  服务器主机 

p  当前的TCP/IP端口或套接字文件 

u  你的用户名 

U  你的全user_name@host_name账户名 

\  ‘’反斜线字符 

n  新行字符 

t  Tab字符 

  空格(反斜线后面的空格) 

_  空格 

R  当前的时间,24-小时军用时间(0-23) 

r  当前的时间,标准12-小时(1-12) 

m  当前时间的分钟 

y  当前的年,两位 

Y  当前的年,四位 

D  当前的日期 

s  当前时间的秒 

w  当前周的天,3字符格式(Mon,T ,...) 

P  am/pm 

o  当前的月,数字格式 

O  当前的月,3字符格式(Jan,Feb,...) 

c  随发出的每个语句递增的计数 

S  分号 

'  单引号 

"  双引号

以上内容在window及linux环境下测试通过,请放心使用。

REF:

http://renial.iteye.com/blog/773675

http://blog.163.com/yang_jianli/blog/static/1619900062011283527540/

(2)oracle提示符修改:

修改 C:oracleproduct11.2.0dbhomesqlplusadminglogin.sql

SET SQLPROMPT "_USER'@'_CONNECT_IDENTIFIER> " 
set serveroutput on
--显示当前时间
set time on 
--显示语句执行时间
set timing on  
--DEFINE _EDITOR=vim
set linesize 140

效果:

21:36:57 SCOTT@orcl> select * from test1 minus select * from test2;

        ID NAME
---------- ----------
        11 dongruan
       111    yg

REF: http://blog.csdn.net/tianlesoftware/article/details/6412769

(3)修改 linux bash shell vimrc 提示符:

请参考:http://www.linuxfocus.org/ChineseGB/May2004/article335.shtml

ip2=`ip a 2>/dev/null|grep 'eth0$'|grep -Po '(d{1,3}.d{1,3})(?=/)'` ip2=`ip a 2>/dev/null|grep 'eth0$'|grep -Po '((d{1,3}.){3}d{1,3})(?=/)'` alias june='eval "PS1="[e[36;1m]u[e[0m]@[e[33;1m]h[e[40;35;9m $ip2 e[0m]e[40;34;1mt e[0m[e[31;1m]w [e[0m]>n""'

vimrc 配置范例:

https://github.com/acumon/misc/blob/master/.vimrc

https://github.com/joedicastro/dotfiles/tree/master/vim

man page 颜色范例:

(1)配色 1:

export LESS_TERMCAP_mb=$'e[01;31m'
export LESS_TERMCAP_md=$'e[01;35m'
export LESS_TERMCAP_me=$'e[0m'
export LESS_TERMCAP_se=$'e[0m'
export LESS_TERMCAP_so=$'e[01;33m'
export LESS_TERMCAP_ue=$'e[0m'
export LESS_TERMCAP_us=$'e[04;36m'

(2)配色 2:

export LESS_TERMCAP_mb=$'E[01;31m'
export LESS_TERMCAP_md=$'E[01;31m'
export LESS_TERMCAP_me=$'E[0m'
export LESS_TERMCAP_se=$'E[0m'
export LESS_TERMCAP_so=$'E[01;44;33m'
export LESS_TERMCAP_ue=$'E[0m'
export LESS_TERMCAP_us=$'E[01;32m'

.bashrc 完整范例:

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

export LESS_TERMCAP_mb=$'E[01;31m'
export LESS_TERMCAP_md=$'E[01;31m'
export LESS_TERMCAP_me=$'E[0m'
export LESS_TERMCAP_se=$'E[0m'
export LESS_TERMCAP_so=$'E[01;44;33m'
export LESS_TERMCAP_ue=$'E[0m'
export LESS_TERMCAP_us=$'E[01;32m'
export HISTSIZE=5000
export HISTTIMEFORMAT='[%F %T] '
shopt -s histappend
export PROMPT_COMMAND=
export EDITOR=vim
export LANG=zh_CN.utf8
export PATH=$PATH:.:/opt/soft/python-2.7.11/bin

IP=`/sbin/ip a 2>/dev/null|grep -iE '(em1|eth0)$'|grep -Po '((d{1,3}.){3}d{1,3})(?=/)'`
#HOST_NAME=dop_online${IP##*.}
. .hostname
#alias june='eval "PS1="[e[36;1m]u[e[0m]@[e[33;1m]h[e[40;35;9m $ip e[0m]e[40;34;1mt e[0m[e[31;1m]w [e[0m]>n""'
#export PS1='[u@h W]$'
export PS1="[e[36;1m]u[e[0m]@[e[33;1m]$HOST_NAME[e[40;35;9m $IP e[0m]e[40;34;1mt e[0m[e[31;1m]w [e[0m]>n"

alias rp='realpath'
alias grep='grep --color'
alias ls='ls --color=tty'
alias vi='vim'
alias cmd='cd /opt/script/rpcMonitor && python exec_cmd.py'
alias lh='ls -ld `pwd`/.[^.]*'

#export KRB5CCNAME=/tmp/krb5cc_pub_$$
#trap kdestroy 0 1 2 3 5 15
kinit -k -t /etc/krb5.keytab

myScp(){
    [[ $1 == "" ]] || [[ $2 == "" ]] && echo "请输入源文件与目标文件路径,如:myScp ~/a.txt /work" && return
    for ip in `cat ~/.ip_list`
    do
        echo -e "nn==========>>>>>>>>>>>>$ip:"
        rsync -avz $1 work@$ip:$2
    done
}

#baseDir=/root/tmp
#cd $baseDir
#userDir=`echo $SSH_CLIENT|awk '{print $1}'`
changeDir(){
    echo "--------------------------------------------------------------------"
    echo -e "e[41;37;1m 欢迎登录,系统使用注意事项如下:  e[0m" 
    echo -e "e[41;37;1m 1、第一次登录请在 /home/work 下新建自己名字的目录(比如 mkdir -p ~/your_name ),私人文件请都放在自己的目录下。e[0m" 
    echo -e "e[41;37;1m 2、应用、服务请部署到 /opt 下对应的目录,比如 web soft script log 等等。e[0m" 
    echo -e "e[41;37;1m 3、由于一台机器会多人使用,请勿随意放置文件、安装程序,有需要请联系 your_name 统一规划。e[0m" 
    echo "--------------------------------------------------------------------"
}
[[ $- == *i* ]] && changeDir

(4)修改 windows cmd 提示符:

新建 prompt 环境变量,具体含义 help prompt

具体效果步骤请参考:

http://hi.baidu.com/leejun_2005/item/0e29f4bad4bca5a0ebba9373

http://hi.baidu.com/leejun_2005/item/4459eb7a2c314e3b70442353

(5)bash color 详解

Escape sequences

You’ve probably seen things like e[32m or x1b[1;31m. These are ANSI escape codes used for defining a color. All ANSI escape sequences start with, well, ESC. There’re several ways of encoding an ESC:

Shell

e

ASCII Hex

x1B

ASCII Oct

33

So x1b[31;4me[31;4m and 33[31;4m are different ways to write the same sequence. Let’s look at the structure of this sequence.

x1b[ is a Control Sequence Introducer that consists of hexadecimal ASCII ESC character code and a [.

31;4 is a list of instructions separated by ;. Usually this list is formatted as follows:

[<PREFIX>];[<COLOR>];[<TEXT DECORATION>]

For example 31;4 means “no prefix, color - red, underline”. <PREFIX> is used for 256 color mode. More on color modes later.

Finally m indicates the end of control sequence so terminal would know not to interpret text after mas a color code.

The following command should print “hello” in red underscore text:

> echo "x1b[31;4mHellox1b[0m"

x1b[0m means “reset all attributes”.

declare -A colors
#curl www.bunlongheng.com/code/colors.png

# Reset
colors[Color_Off]='33[0m'       # Text Reset

# Regular Colors
colors[Black]='33[0;30m'        # Black
colors[Red]='33[0;31m'          # Red
colors[Green]='33[0;32m'        # Green
colors[Yellow]='33[0;33m'       # Yellow
colors[Blue]='33[0;34m'         # Blue
colors[Purple]='33[0;35m'       # Purple
colors[Cyan]='33[0;36m'         # Cyan
colors[White]='33[0;37m'        # White

# Bold
colors[BBlack]='33[1;30m'       # Black
colors[BRed]='33[1;31m'         # Red
colors[BGreen]='33[1;32m'       # Green
colors[BYellow]='33[1;33m'      # Yellow
colors[BBlue]='33[1;34m'        # Blue
colors[BPurple]='33[1;35m'      # Purple
colors[BCyan]='33[1;36m'        # Cyan
colors[BWhite]='33[1;37m'       # White

# Underline
colors[UBlack]='33[4;30m'       # Black
colors[URed]='33[4;31m'         # Red
colors[UGreen]='33[4;32m'       # Green
colors[UYellow]='33[4;33m'      # Yellow
colors[UBlue]='33[4;34m'        # Blue
colors[UPurple]='33[4;35m'      # Purple
colors[UCyan]='33[4;36m'        # Cyan
colors[UWhite]='33[4;37m'       # White

# Background
colors[On_Black]='33[40m'       # Black
colors[On_Red]='33[41m'         # Red
colors[On_Green]='33[42m'       # Green
colors[On_Yellow]='33[43m'      # Yellow
colors[On_Blue]='33[44m'        # Blue
colors[On_Purple]='33[45m'      # Purple
colors[On_Cyan]='33[46m'        # Cyan
colors[On_White]='33[47m'       # White

# High Intensity
colors[IBlack]='33[0;90m'       # Black
colors[IRed]='33[0;91m'         # Red
colors[IGreen]='33[0;92m'       # Green
colors[IYellow]='33[0;93m'      # Yellow
colors[IBlue]='33[0;94m'        # Blue
colors[IPurple]='33[0;95m'      # Purple
colors[ICyan]='33[0;96m'        # Cyan
colors[IWhite]='33[0;97m'       # White

# Bold High Intensity
colors[BIBlack]='33[1;90m'      # Black
colors[BIRed]='33[1;91m'        # Red
colors[BIGreen]='33[1;92m'      # Green
colors[BIYellow]='33[1;93m'     # Yellow
colors[BIBlue]='33[1;94m'       # Blue
colors[BIPurple]='33[1;95m'     # Purple
colors[BICyan]='33[1;96m'       # Cyan
colors[BIWhite]='33[1;97m'      # White

# High Intensity backgrounds
colors[On_IBlack]='33[0;100m'   # Black
colors[On_IRed]='33[0;101m'     # Red
colors[On_IGreen]='33[0;102m'   # Green
colors[On_IYellow]='33[0;103m'  # Yellow
colors[On_IBlue]='33[0;104m'    # Blue
colors[On_IPurple]='33[0;105m'  # Purple
colors[On_ICyan]='33[0;106m'    # Cyan
colors[On_IWhite]='33[0;107m'   # White


color=${colors[$input_color]}
white=${colors[White]}
# echo $white



for i in "${!colors[@]}"
do
  echo -e "$i = ${colors[$i]}I love you$white"
done

——END——

Refer:

[1] 小应用之:shell 日志工具 logdotsh 

https://github.com/dangoakachan/logdotsh

[2] xterm-256color 终端的 256 色模式

http://blogread.cn/it/article/3830?f=wb

[3] Tutorial: How to Color Man Pages & How It Works

http://www.tuxarena.com/2012/04/tutorial-colored-man-pages-how-it-works/

[4] 彩色的命令行 —— 使用 ANSI 色彩代码

http://www.linuxfocus.org/ChineseGB/May2004/article335.shtml

[5] How to change the output color of echo in Linux

https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux

[6] Bash tips: Colors and formatting (ANSI/VT100 Control sequences)

http://misc.flogisoft.com/bash/tip_colors_and_formatting

[7] Colors In Terminal

http://jafrog.com/2013/11/23/colors-in-terminal.html