The advantage of Linux is that you can customize anything you want. This is the advantage of projective systems over procedural systems. It is a kind of constructor, consisting of separate blocks, each of which can be customized at will.
It is especially interesting if you don't just copy ready-made solutions, but try to understand what and how it works. And even when solving a frivolous task, you learn something new.
For example, you want to customize the linux terminal colors in the output of the ls
command.
From the ls
documentation, you can understand that setting the colors for the output is done using the dircolors utility, which either sets the color parameters from its own database or takes them from the ~/.dircolors
file. It was also mentioned that the initialization of color usage is done via ~/.bashrc
, a file with shell settings, and the parameters themselves are defined via the LC_COLORS
variable.
The following script was found in .bashrc
:
Its essence is to check for the presence of the dircolors utility on the system, assign a value to the variable and, if successful, use the --color=auto
parameter each time ls
is called.
If you run the dircolors utility, it outputs the value of the LC_COLOR
variable.
It is also possible to output the value of the LC_COLORS
variable in structured form, by specifying the key:
$ dircolors -p
Since the output is long, I won't give it here.
If you redirect the output dircolors -p
> ~/.dircolors
, a structured file will be created from which the color settings will be taken. By editing this file and set the color settings for the ls
output.
In the created .dircolors
file there is a hint:
Setting the sequence through semicolon and get the color we need in the linux console, according to the listed. The linux terminal colors are from 30 to 37 for font color, and from 40 to 47 for background. For example:
DIR 01;34 implies that directory names will be bold and blue.
But this way you get only eight colors, and modern terminal emulators are capable of displaying at least 256.
There are other ways to set the color. For example, the value EXEC 38;5;208;1 - gives orange thick font in executable files.
In order not to go through the linux colors manually, there is a ready-made table of colors
Substituting in the color value 38;5;x - where x is the color from the table, we get the desired result. You get something like this:
How does it work? The point is that setting the color of information output in the terminal emulator is done with the help of ANSI escape codes. This is a certain sequence of characters that is not printed on the screen, but affects the output. It is called so because it starts with [ - code "Esc". Since there are many terminals and terminal emulators, these control codes are standardized. Color control through the sequence from 30 to 37 is the ECMA-48 standard. Information about it can be found in the console_codes
section.
Having understood how to set linux colors via esc codes, it will not be difficult to redesign and color the prompt. The PS1 variable, which is defined in the ~/.bashrc
file, is responsible for its output
In Linux Mint, the script for initializing it looks like this:
That is, it is first determined whether the script was run by root or a normal user. Since in the vast majority of cases it is the second option, the line following the first 'else' should be edited. The format of the line is deciphered in the bash documentation, in the "Promting" section. It implies that the esc-sequence should start with "[" and end with "]". Thus:
u
- outputs the user name;h
- outputs the host name;w
- outputs the working directory name;What else can be output in the prompt:
d
- the date, in the format "Wed Dec 28";A
- time, in 24-hour format;Let's introduce something more interesting:
PS1='${debian_chroot:+($debian_chroot)}[[01;32m]Matrix has you,u![[00m] [[01;34m]Blue pill[[00m] or [[31m]red?[[00m]--->$ '
And the prompt line will look like: