Walkthrough on setting up colorized mysql prompt.
Step 1. Understand how to login normally with a set prompt:
eric@dev ~ $ mysql --host=yourhost.com -u username --prompt="foobar> " -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 711
Server version: 5.6.19 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
foobar>
Step 2. Understand how you can pipe an interpreted expression through echo to 'alias':
Which does exactly the same as step 1 above:
eric@dev ~ $ alias penguins=$(echo -e 'mysql --host=yourhost.com -u dev --prompt="foobar> " -p')
eric@dev ~ $ penguins
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 713
Server version: 5.6.19 MySQL Community Server (GPL)
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
foobar> exit
Bye
eric@dev ~ $
Step 3. Understand how echo -e evaluates the colorized expression:
This colors the "foobar>" prompt red:
alias penguins=$(echo -e 'mysql --host=yourhost.com -u dev --prompt="\x1B[31mfoobar>\x1B[0m " -p')
penguins
Like this:
![enter image description here](../../I/static/images/689052763bc62f6b7cc3ea2b2ac9c585b103619e3258e899f76dce0f8cbaecae.jpg)
Step 4. If you are confused as to what is going on here:
Look at the expression: \x1B[31mfoobar>\x1B[0m
It has three parts:
code what it means:
\x1B[31m Start colorizing, 31m is red.
foobar> prompt text
\x1B[0m Stop colorizing.
Step 4. Advanced, Lets make the prompt real nice:
eric@dev ~ $ alias penguins=$(echo -e 'mysql --host=yourhost.com -u dev --prompt="\x1B[31m\\u\x1B[34m@\x1B[32m\\v\x1B[0m:\x1B[36m\\d>\x1B[0m " -p')
eric@dev ~ $ penguins
![enter image description here](../../I/static/images/82b38302f4409d5c268e2b2cf08dc7472fa7c5040d86f43fc38f9a5df6257d82.jpg)
If you are confused as to what this massive code does:
\x1B[31m\\u\x1B[34m@\x1B[32m\\v\x1B[0m:\x1B[36m\\d>\x1B[0m
Explanation:
Code Note
\x1B[31m Start colorizing red
\\u escape the backslash for passage through echo, and print username
\x1B[34m Start colorizing dark blue
@ literal at sign
\x1B[32m Start colorizing green
\\v escape the backslash for passage through echo, print server version
\x1B[0m Stop colorizing
: literal colon
\x1B[36m Start colorizing cyan
\\d> Backslash for passage through echo, print default db and >
\x1B[0m Stop colorizing.
So wow. Much codes.
1http://stackoverflow.com/questions/1940324/mysql-color-scheme – micke – 2012-03-05T19:03:18.300
1I saw it but it does not work – EscoMaji – 2012-03-05T19:06:51.587
Yeah I think it's possible the same way a in your regular shell prompt. Your best bet is probably some wrapper or a more advanced MySQL-client.
– micke – 2012-03-05T19:12:38.0371Does your setup support ANSI escape codes? If it does then you can use them in the following way: --prompt=" ^[[1;33mDTHIS PART IS IN YELLOW COLOUR^[[0m \u@\h:\d> " – Hennes – 2013-03-24T22:53:04.167