Console-colours

From wikipost
Revision as of 21:58, 13 May 2020 by Admin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

When working with Linux remotely over ssh it's sometimes nice to be able to work with a different colour scheme (or color scheme as some write). With ssh clients like Putty it's easy to change the foreground and background colours of the characters, but Linux supports different colours for different types of data; sometimes based on the file extension, sometimes based on functionality (e.g. directory names, command prompt, current working directory, bold text, links, etc..).

Below are listed the most commonly used colour sets and some instructions on how to change them.

change bash prompt colours

The bash prompt can show colors for username, hostname and current working directory.

The colours as well as the components that determine what the prompt should contain are all set in the PS1 environment variable.

The PS1 string is usually set in ~/.bashrc and can look something like this:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

Pay attention at the part \u@\h it is saying "user@host" and the number before it \[\033[01;32m\] indicates the color. This is what needs changing if you want a different colour.


The \u and \h are the username and hostname, but there are more:

\u    username
\h    system host name
\w    current (working) directory
\$    show a # if the effective UID is 0 (i.e. root user), otherwise display a $
\a    unknown
\@    time in 12-hour am/pm format


The full PS1 string looks quite complicated, but here is it broken up into the various pieces:

Set a colour and display the username, @-sign and hostname

\[\033[01;32m\]\u@\h       

Remove any colour codes (use the terminal default) and display the ':' sign

\[\033[00m\]:

Set a colour and display the current working directory

\[\033[01;34m\]\w

Once again disable any colour codes and display just the '$' sign

\[\033[00m\]\$

The actual colour is defined by the semicolon-separated number pair that is inside the inner square brackets (e.g. '01;32'). All the extra stuff is just escape characters and color-begin and color-end markers.

The colors numbers are:

Black       0;30     Dark Gray     1;30
Blue        0;34     Light Blue    1;34
Green       0;32     Light Green   1;32
Cyan        0;36     Light Cyan    1;36
Red         0;31     Light Red     1;31
Purple      0;35     Light Purple  1;35
Brown       0;33     Yellow        1;33
Light Gray  0;37     White         1;37



change manpage colors

When man pages are viewed, they are piped through a program called 'less', which offers page navigation and search capabilities of the man-page text.

The idea is to override the colour settings that less uses.

edit and add the following lines to ~/.bashrc to load up the new colourscheme.

export LESS_TERMCAP_mb=$(printf '\e[01;31m') # enter blinking mode - red
export LESS_TERMCAP_md=$(printf '\e[01;35m') # enter double-bright mode - bold, magenta
export LESS_TERMCAP_me=$(printf '\e[0m') # turn off all appearance modes (mb, md, so, us)
export LESS_TERMCAP_se=$(printf '\e[0m') # leave standout mode    
export LESS_TERMCAP_so=$(printf '\e[01;33m') # enter standout mode - yellow
export LESS_TERMCAP_ue=$(printf '\e[0m') # leave underline mode
export LESS_TERMCAP_us=$(printf '\e[04;36m') # enter underline mode - cyan

The color codes are as follows:

    30 – black
    31 – red
    32 – green
    33 – orange
    34 – blue
    35 – magenta
    36 – cyan
    37 – white

Some other escape codes which you could use include:

    0 – reset/normal
    1 – bold
    3 – italic/reversed
    4 – underlined
    5 – blink


change vi (vim) colours

The vi editor has a range of color schemes available that are defined in /usr/share/vim/vim74/colors/

Adding:

colo blue

..to ~/.vimrc will use the 'blue' colorscheme. Other options are:

darkblue
default
delek
desert
elflord
evening
industry
koehler
morning
murphy
pablo
peachpuff
ron
shine
slate
torte
zellner

You could of course also type: 'colo blue' on the vi status line to immediately change to it.

The status line is what appears after you press the <ESCAPE> key and type a : (the colon symbol)


Add the following line to your ~/.vimrc to change the bottom line text color (that shows the edit mode, INSERT or REPLACE):

hi ModeMsg term=bold ctermfg=2 gui=bold guifg=SeaGreen


When 'su' -ing into another account, the .vimrc of that su account will override the .vimrc from the account you were coming from.


More options:

hi LineNr ctermfg=grey guifg=grey
hi Statement ctermfg=black guifg=black
hi Identifier ctermfg=darkGreen guifg=darkGreen
hi Comment term=bold ctermfg=4 guifg=#406090
hi Constant term=underline ctermfg=Red guifg=#c00058
hi Special term=bold ctermfg=Blue guifg=SlateBlue
hi Identifier term=underline ctermfg=Black guifg=Black
hi Statement term=bold ctermfg=Brown gui=bold guifg=Brown
hi PreProc term=underline ctermfg=Magenta guifg=Magenta3
hi Type term=underline ctermfg=Green gui=bold guifg=SeaGreen
hi Ignore cterm=bold ctermfg=7 guifg=bg
hi Error term=reverse cterm=bold ctermfg=7 ctermbg=1 gui=bold guifg=White guibg=Red
hi Todo term=standout ctermfg=0 ctermbg=3 guifg=Blue guibg=Yellow

To see all the available (and currently configured color schemes) type the following:

hi

Lastly, vim has been configured on some systems to support Syntax Highlighting. Some find this fantastic, others find this annoying. To turn it off, type 'syntax off' on the status line or include it in your .vimrc file.


change directory and file type colours

In some cases you may want to change the file type colours that appear when viewing a directory listing in Linux.

Normally, directories are dark blue, .gz files are red, executables are green, etc..


The colours are managed by the LS_COLORS environment variable which can be easily modified with the utility 'dircolors'.


Step 1 – Copy the current LS_COLORS to a file in your home folder.

dircolors > ~/.dir_colors

Step 2 – Edit the files to make whatever changes you require.

vi ~/.dir_colors

Step 3 – load the new color scheme whenever a shell starts

echo ". ~/.dir_colors" >> ~/.bashrc


NOTE: when using 'su' the color scheme is inherited from the user you were logged in as. So if the colours while in 'su' are not to your liking, you need to change the dircolors scheme for the pre-su-user, not root.


You will notice that the LS_COLORS variable contains many definitions. The dircolors utility has a way of displaying the colour definitions a bit nicer:

dircolors --print-database


The first number is the style (1=bold), followed by a semicolon, and then the actual number of the color, possible styles are:

0   = default colour
1   = bold
4   = underlined
5   = flashing text
7   = reverse field
40  = black background
41  = red background
42  = green background
43  = orange background
44  = blue background
45  = purple background
46  = cyan background
47  = grey background
100 = dark grey background
101 = light red background
102 = light green background
103 = yellow background
104 = light blue background
105 = light purple background
106 = turquoise background


All possible colors:

31  = red
32  = green
33  = orange
34  = blue
35  = purple
36  = cyan
37  = grey
90  = dark grey
91  = light red
92  = light green
93  = yellow
94  = light blue
95  = light purple
96  = turquoise

These can even be combined, so that a parameter like:

di=1;4;31;42

in your LS_COLORS variable would make directories appear in bold underlined red text with a green background!

You can also change other kinds of files when using the ls command by defining each kind with:

di = directory
fi = file
ln = symbolic link
pi = fifo file
so = socket file
bd = block (buffered) special file
cd = character (unbuffered) special file
or = symbolic link pointing to a non-existent file (orphan)
mi = non-existent file pointed to by a symbolic link (visible when you type ls -l)
ex = file which is executable (ie. has 'x' set in permissions).
*.rpm = files with the ending .rpm

After you alter your .bashrc file, to put the changes in effect you will have to restart your shell.