If you’re a command line dinosaur like me, maybe you’d still like to occasionally step out of the dark ages and have your output in a nice colour.

Maybe in the query below, I’d like some highlighting when the hiredate is (say) more recent than 1982.

image

Well, if you are really keen, then you can add the following functions to your database, and then it is easy! (well, sortta easy 😀)


SQL> create or replace
  2  function Black(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[30m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL> create or replace
  2  function Red(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[31m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL> create or replace
  2  function Green(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[32m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL> create or replace
  2  function Yellow(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[33m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL> create or replace
  2  function Blue(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[34m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL> create or replace
  2  function Magenta(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[35m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL> create or replace
  2  function Cyan(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[36m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL> create or replace
  2  function White(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[37m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL>
SQL>
SQL> create or replace
  2  function Bright_Red(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[31;1m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL> create or replace
  2  function Bright_Green(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[32;1m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL> create or replace
  2  function Bright_Yellow(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[33;1m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL> create or replace
  2  function Bright_Blue(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[34;1m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL> create or replace
  2  function Bright_Magenta(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[35;1m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL> create or replace
  2  function Bright_Cyan(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[36;1m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL> create or replace
  2  function Bright_White(p_str varchar2) return varchar2 is
  3  begin return  chr(27)||'[37;1m'||p_str||chr(27)||'[0m';
  4  end;
  5  /

Function created.

SQL>
SQL>

Grab the source from my github repo here

Now look at this beautiful modern output 😀

image

If you’re thinking “Man, this dude needs to look at APEX”, then I’d say you’re correct 😀

Note: If it is just syntax highlighting that you are after, then check out the options in SQLcl here

2 responses to “Quick color coding hack for your terminal”

  1. Yes, I loved the command line for sqlplus.

    Tom Kyte loved it too:

    “I copied and pasted all of the time in DOS windows. I liked it even BETTER cause I could select a SQUARE region.

    Alt-space/e/p (alt-space then edit then paste, once you do alt-space/e/p a time or time, it almost becomes a keystroke you can do it so fast).

    then there is an option hidden in some user friendy menu to make it so you can “copy on select” — it’ll let you select text and when you hit “enter” the selected text is placed in the copy buffer”

    1. yup,. “quick edit” mode.

      Windows 11 changed the square region to “normal” select, but holding down Alt brings back my favourite behaviour

Got some thoughts? Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Trending

Blog at WordPress.com.