[Command line] configuration color (windows cmd, powershell, linux console)

Source: Internet
Author: User
Modify the cmd color

Color = xy x (foreground color) and y (background color) can respectively take the following values: 0 = black 8 = gray
1 = blue 9 = light blue
2 = Green A = light green
3 = light green B = light green
4 = red C = light red
5 = purple D = lavender
6 = yellow E = pale yellow
7 = white F = bright white

For example, if color = 28 is entered, the color will be restored to the default setting.

Modify the powershell color

First, we will give a general introduction to powershell. Powershell has many cmdlets and alias many commands. For example, both 'dir' and 'LS' point to get-childitem, so that whether you are used to win or linux Command lines, can be used. Powershell script syntax has its own characteristics and supports some. net syntax. You can write some functions to implement some functions by yourself. Here we will introduce some methods to modify the color. First, the personalized configuration needs to take effect every time it is started, so a user configuration file is required. You can find the configuration file loaded by default powershell at startup in the following path.

Description: Router ----------- ---- current user and current host $ Home \ [My] Documents \ WindowsPowerShell \ Profile. currently, ps1 users and all hosts $ Home \ [My] Documents \ Profile. all ps1 users, current host $ PsHome \ Microsoft. powerShell_profile.ps1 all users, all hosts $ PsHome \ Profile. the ps1 preset route entry contains the following changes:-$ PsHome changes: used to store Windows PowerShell security program. -$ Home changes: used to store the current user's primary object.

Assume that the path of my configuration file is "C: \ Users \ gogdi.pdf \ Documents \ WindowsPowerShell \ profile. ps1"
The content is as follows:

set-alias ll         Get-ChildItemColorfunction prompt{    $my_path = $(get-location).toString()    $my_pos = ($my_path).LastIndexOf("\") + 1    if( $my_pos -eq ($my_path).Length ) { $my_path_tail = $my_path }    else { $my_path_tail = ($my_path).SubString( $my_pos, ($my_path).Length - $my_pos ) }    Write-Host ("[") -nonewline -foregroundcolor 'Green'    Write-Host ("Gogdizzy") -nonewline -foregroundcolor 'Red'    Write-Host ("@") -nonewline -foregroundcolor 'Yellow'    Write-Host ("WIN7 ") -nonewline -foregroundcolor 'Magenta'    Write-Host ($my_path_tail) -nonewline -foregroundcolor 'Blue'    Write-Host ("]#") -nonewline -foregroundcolor 'Green'    return " "}function Get-ChildItemColor {<#.Synopsis  Returns childitems with colors by type..Description  This function wraps Get-ChildItem and tries to output the results  color-coded by type:  Directories - Cyan  Compressed - Red  Executables - Green  Text Files - Gray  Image Files - Magenta  Others - Gray.ReturnValue  All objects returned by Get-ChildItem are passed down the pipeline  unmodified..Notes  NAME:      Get-ChildItemColor  AUTHOR:    Tojo2000 <tojo2000@tojo2000.com>#>  $regex_opts = ([System.Text.RegularExpressions.RegexOptions]::IgnoreCase `      -bor [System.Text.RegularExpressions.RegexOptions]::Compiled)   $fore = $Host.UI.RawUI.ForegroundColor  $compressed = New-Object System.Text.RegularExpressions.Regex(      '\.(zip|tar|gz|rar|7z|tgz|bz2), $regex_opts)  $executable = New-Object System.Text.RegularExpressions.Regex(      '\.(exe|bat|cmd|py|pl|ps1|psm1|vbs|rb|reg|sh), $regex_opts)  $text_files = New-Object System.Text.RegularExpressions.Regex(      '\.(txt|cfg|conf|ini|csv|log), $regex_opts)  $image_files = New-Object System.Text.RegularExpressions.Regex(      '\.(bmp|jpg|png|gif|jpeg), $regex_opts)   Invoke-Expression ("Get-ChildItem $args") |    %{      if ($_.GetType().Name -eq 'DirectoryInfo') { $Host.UI.RawUI.ForegroundColor = 'Cyan' }      elseif ($compressed.IsMatch($_.Name)) { $Host.UI.RawUI.ForegroundColor = 'Red' }      elseif ($executable.IsMatch($_.Name)) { $Host.UI.RawUI.ForegroundColor = 'Green' }      elseif ($text_files.IsMatch($_.Name)) { $Host.UI.RawUI.ForegroundColor = 'Gray' }      elseif ($image_files.IsMatch($_.Name)) { $Host.UI.RawUI.ForegroundColor = 'Magenta' }      else { $Host.UI.RawUI.ForegroundColor = 'Gray' }      echo $_      $Host.UI.RawUI.ForegroundColor = $fore    }}function Show-Color( [System.ConsoleColor] $color ){    $fore = $Host.UI.RawUI.ForegroundColor    $Host.UI.RawUI.ForegroundColor = $color    echo ($color).toString()    $Host.UI.RawUI.ForegroundColor = $fore}function Show-AllColor{    Show-Color('Black')    Show-Color('DarkBlue')    Show-Color('DarkGreen')    Show-Color('DarkCyan')    Show-Color('DarkRed')    Show-Color('DarkMagenta')    Show-Color('DarkYellow')    Show-Color('Gray')    Show-Color('DarkGray')    Show-Color('Blue')    Show-Color('Green')    Show-Color('Cyan')    Show-Color('Red')    Show-Color('Magenta')    Show-Color('Yellow')    Show-Color('White')}

We can see that the ll command is associated with Get-ChildItemColor through alias, and the file suffix is used to determine the color to use.
Get-ChildItemColor was copied from the Internet and was slightly modified.
In addition, I provide two Commands: Show-Color and Show-AllColor. If you have any requirements, you can modify the Color in the code.
The custom prompt is implemented by redefining the function prompt, that is, the user's function replaces the default function provided by the system, which uses the-foregroundcolor option in Write-Host, color command prompt is provided.

Modify the color of the linux console

The Linux LS command originally had the -- color option. I used securecrt to connect to Linux, So I directly set the color scheme in securecrt. The following describes how to configure the color command line prompt. Linux is implemented by setting the PS1 variable. The content of my PS1 variable is as follows:

export PS1 = '`_a=$?; if [ $_a -ne 0 ]; then _a="   "$_a; echo -ne "\[\e[s\e[1A\e[$((COLUMNS-2))G\e[31m\e[1;41m${_a:(-3)}\e[u\e[0m\]"; fi`\[\e[01;32m[\e[31m\u\e[33m@\e[35m\h \e[34m\W\e[32m\]]\$ '

I copied this from my blog post, mainly adding the ability to Display error codes in the color function. The following explains the meaning:

_a=$?; if [ $_a -ne 0 ]; then _a="   "$_a;

$? Is the return value of the previous command. If it is not 0, it must be the error code, save it in the $ _ a variable, and add three spaces before it;

echo -ne "\[\e[s\e[1A\e[$((COLUMNS-2))G\e[31m\e[1;41m${_a:(-3)}\e[u\e[0m\]";

'\ E [' is a control code. The characters that follow this control code have special meanings.
'\ E [s' to save the current cursor position
'\ E [1A' Move up a row
'\ E [$ (columns-2) G' $ columns variable stores the current row width, which is equivalent to moving to the first two of the last positions, which is actually the last third position.
'\ E [31m' sets the foreground color to red.
'\ E [1; 41m' is set to red, and the foreground is highlighted.
'$ {A :(-3)}' takes the last three characters of the variable (which knows why three spaces are added before? Because if the length of a is less than three, you need to fill it with spaces. Otherwise, an error will occur)
'\ E [U' restore the cursor position
'\ E [0m' cancel color settings
Note that all control code commands must be included with '\ [' and.

\[\e[01;32m[\e[31m\u\e[33m@\e[35m\h \e[34m\W\e[32m\]]\$ 

This section sets the prompt and mainly configures the color. Note that there are square brackets related to the control code and the square brackets of the prompt content.
'\ E [M [': highlighted, green left square brackets
['\ E [31m \ U' displays the red User Name (\ U is not the control code control, but the built-in name in PS1), inheriting the highlighted attribute.
'\ E [33m @' indicates a yellow @, which inherits the highlighted attribute.
'\ E [35m \ H' displays the pink Host Name (\ H is the built-in name of PS1 and displays the host name), inheriting the highlighted property.
'\ E [34m \ W' shows the blue path name (\ W shows the relative path, \ W shows the absolute path), and inherits the highlight.
'\ E [32m \]' ends the control operator, and the color is set to green, inheriting the highlighted color.
'] \ $' Show right square brackets], and then display the prompt (\ $, when it is the root user #, other users show $)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.