Search code examples
linuxbashterminaldebianxterm

Color listing in xterm terminal


I installed a minimal debian and i'm trying to set everything by myself, so i'm trying to tune my default terminal Xterm by giving it some colors, after some research i found that this can be done either by editing the .Xresources file so i can give the first 16 colors in Hexa code (#numbers) or by editing the .bashrc file. After some research seems like the second solution is more dynamic 'cause i can specify the colors of files by extensions, the problem is that all the articles on the net use a color code very weird :

.gz  01;31
.bz2 01;31
.deb 07;31
.rpm 01;31
.jar 01;31

I want to know if there's a way to use RGB code color or hexa color so i can have more flexibility with my directory and files listings.


Solution

  • AFAIK, there is no RGB support, terminal emulators are limited to display a limited set of colors (a palette with 64 combinations of foreground and background, if I'm not mistaken). If ease of use is what concerns you, you can always declare several variables for the predefined colors, something like:

    readonly BLACK_FONT_BLACK_BACKGROUND='30;40'     
    readonly BLACK_FONT_RED_BACKGROUND='30;41'       
    readonly BLACK_FONT_GREEN_BACKGROUND='30;42'     
    readonly BLACK_FONT_YELLOW_BACKGROUND='30;43'
    ...
    readonly WHITE_FONT_CYAN_BACKGROUND='37;46'
    readonly WHITE_FONT_WHITE_BACKGROUND='37;47'
    

    And use them later like:

    .gz  $BLACK_FONT_YELLOW_BACKGROUND
    

    The following is a table with the color codes for foreground and background:

      Color   Foreground   Background
      -------------------------------
      black       30           40
      red         31           41
      green       32           42
      yellow      33           43
      blue        34           44
      magenta     35           45
      cyan        36           46
      white       37           47
      -------------------------------
    

    If generating all the color names variables manually is too tedious, you can use the following python script:

     colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']; 
     index = 0
     for x in ["{0}_FONT_{1}_BACKGROUND='{2};{3}'".format(colors[i].upper(), colors[j].upper(), i + 30, j + 40) for i in xrange(0, 8) for j in xrange(0, 8)]: 
         print x.ljust(38) + "#" + str(index) 
         index += 1