Control codes
Control character
- https://en.wikipedia.org/wiki/Control_character
- https://en.wikipedia.org/wiki/C0_and_C1_control_codes
- https://simple.wikipedia.org/wiki/ASCII
Control character or non-printing character (NPC), resp. riadiaci znak, je znak, resp. kodovy bod (code point, number) z tabulky znakov (character set), ktory nepredstavuje pisany symbol (printing character), ale ma specialny vyznam pre spracovanie textu. Preto sa im zvykne hovorit aj non-printing znaky.
V ASCII kodovani (zakladne, 7 bitove, obsahujuce 128 znakov) su znaky s kodom mensim ako 32, resp. z intervalu 0x00—0x1F, control characters (riadiace znaky), oznacovane tiez ako aj C0 control code set. Riadiacim znakom je aj kod 127, resp. 0x7F, (DEL). Rozsirene ASCII kodovanie (8 bitove, obsahujuce 256 znakov) pridava kody od 128 do 159, resp. 0x80—0x9F, ako control characters. Tato druha sada riadiacich kodov sa oznacuje ako C1 set. Tychto 65 control characters (po 32 z C0 a C1 + znak DEL) bolo zahrnutych aj do Unicode znakovej sady, ktora pridava navyse este niektore vlastne riadiace znaky. Control characters sa tiez niekedy nazyvaju aj ako format effectors (formatovacie znaky).
Caret notation je sposob zapisu control characters, ktore su non-printable characters, v ASCII kodovani. Zapis zacina caret znakom ^
a velkym pismenom abecedy. Nulty symbol (NULL) sa zapisuju ako ^@
(znak @ je v ASCII tabulke pred znakom A), prvy control code (Start of Heading) ako ^A
, druhy control code (Start of Text) ako ^B
a podobne. Pre zapis mimo rozsahu 1-26 (A-Z) sa pouzivaju non-alphabetic ASCII characters. Takyto sposob zapisu sa niekedy tiez oznacuje ako Ctrl-Key.
Caret | Decimal | Octal | Hex | C escape | Abbreviation | Name |
---|---|---|---|---|---|---|
^@ |
00 | 000 | 0x00 | \0 |
NUL | Null character |
^G |
07 | 007 | 0x07 | \a |
BEL | Bell, Alert |
^H |
08 | 010 | 0x08 | \b |
BS | Backspace |
^I |
09 | 011 | 0x09 | \t |
HT | Horizontal TAB |
^J |
10 | 012 | 0x0A | \n |
LF | Line Feed (new line) |
^K |
11 | 013 | 0x0B | \v |
VT | Vertical TAB |
^L |
12 | 014 | 0x0C | \f |
FF | Form Feed (page break) |
^M |
13 | 015 | 0x0D | \r |
CR | Carriage Return |
^[ |
27 | 033 | 0x1B | \e * |
ESC | Escape |
^? |
127 | 177 | 0x7F | DEL | Delete | |
NOTE C escape sequence \e nie je sucastou ISO C ani ostatnych prog. jazykov, ale napr. gcc ho podporuje. Odporuca sa pouzivat napr. hex prezentaciu.
|
$ echo -ne '\n' | od -t c -t d1 -t o1 -t x1 # od(1) 0000000 \n 10 012 0a 0000001
ANSI escape code
Rozsirujuci kod spomedzi control characters je ASCII escape character (27, resp. 033, resp. 0x1B, ESC
, ^[
, \e
). Escape sequences (escape postupnosti) je kod vlozeny v text pre rozne formatovanie (farba, styl fontu) vystupu na na textovy terminal (emulator) alebo aj napr. na ovladanie kurzora. Umoznuje pozmenit standardne spravanie terminalov, napr. zmazanie obrazovky, farebny vypis, zmena pozicie kurzora a podobne. Do textu sa vkladaju (zakoduju) urcite postupnosti bytes (znakov), ktore terminal neinterpretuje ako kody znakov (text), ale ako prikazy. Historicky existovala mnozstvo terminalov s roznymi postupnostami (a k tomu este proprietarnymi), preto sa dnes pouziva zavedeny ANSI standard.
Escape sequences maju roznu dlzku a pozostavaju iba zo znakov z rozsahu 32—126, resp. 0x20—0x7E, t.j. zo vsetkych non-control characters, resp. vsetky printable characters. Ak by znak bol mimo daneho rozsahu, spravanie terminalu nie je definovane.
Name | Decimal | Hex | Usage example |
---|---|---|---|
nF Escape sequences | 32—47 | 0x20—0x2F | used for ANSI/ISO code-switching mechanisms |
Fp Escape sequences | 48—63 | 0x30—0x3F | up to sixteen private-use control functions |
Fe Escape sequences | 64—95 | 0x40—0x5F | most used, for example CSI (with SGR) or OSC sequences |
Fs Escape sequences | 96—126 | 0x60—0x7E | used for control functions individually registered with the ISO-IR registry |
Fe Escape sequences
Ak po ESC
nasleduje byte z rozsahu 0x40 az 0x5F, escape sequence je typu Fe. Typickym predstavitelom je postupnost ESC
[
(Control Sequence Introducer), pripadne ESC
]
(Operating System Command). Prostredie, ktore podporuje iba ASCII (7-bit), rozpoznava iba dvoj-znakovu postupnost. Standard vsak umoznuje, v 8-bit prostredi, aj jedno-znakovu escape sequence z rozsahu 0x80—0x9F, t.j. C1 control code set. Napr. existuje aj jedno-znakove CSI (155, resp. 0x9B), ale dvoj-znakova postupnost ESC
[
sa pouziva castejsie. V sucasnosti sa na modernych zariadeniach, ako napr. UTF-8 alebo CP-1252, jedno-znakove kody pouzivaju na ine ucely, a zvycajne sa pouziva iba dvoj-znakova postupnost.
NOTES
- Niektore kodovania mozu pouzivat viac ako jeden byte na znak, v dalsom budeme predpokladat pouzivanie len ASCII znakov, ktore su reprezentovane prave jednym byte (jeden byte, jeden znak).
- Niektore kody z C0 control set (teda okrem
ESC
), mozu mat rovnaky, podobny efekt ako niektore escape sequence. - Dalej, pokial nebude uvedene inak, budeme predpokladat default prostredie pre Fedora Linux: GNU bash, pouzivanie terminal emulatora GNOME Terminal,
TERM="xterm-256color"
,COLORTERM="truecolor"
,LANG="en_US.UTF-8"
.
Control codes in shell script
Prikaz echo
with option -e
(enable interpretation of backslash-escaped characters) nie je POSIX, a teda, stracame portabilitu. Vseobecne sa odporuca nahradzat prikaz echo -e
prikazom printf
(formatuje ako C printf
), ale kedze echo
prikaz je extremne rozsireny napriec celym Linux (Unix) svetom, je pouzivanie echo -e
"pripustne". Obidva prikazy rozpoznavaju standardne sequences, ako napr. \b
, \n
, \t
, \e
a podobne. Okrem toho umoznuju zadavat priamo control codes ako octal, hexadecimal alebo Unicode znaky s hex hodnotami. Ako uz bolo spomenute, POSIX odporuca uprednostnovat printf
namiesto echo
, obzvlast v pripade echo -e
.
NOTE V praxi sa vacsinou pouzivaju builtin prikazy echo
a printf
shell prostredia a nie ich standalone ekvivalent. Ako dosledok toho, builtin prikaz echo
vie spracovat aj Unicode znaky na rozdiel od standalone verzie (standalone man echo
vs. echo in man builtins
).
# in all cases absolute identically output $ echo -ne '\t'; echo -ne '\011'; echo -ne '\x09'; echo -ne '\u0009' $ printf '\t'; printf '\011'; printf '\x09'; printf '\u0009'
NOTE Interpretacia octal value nnn (zero to three octal digits) je pre prikaz echo -e
v tvare \0nnn
, ale pre printf
v tvare \nnn
(nezacina nulou). Takze pre prikaz echo -e
plati '\0011'
= '\011'
≠ '\11'
, ale pre prikaz printf
plati '\011'
= '\11'
≠ '\0011'
. Zapisy pre hex, resp. Unicode hodnoty su identicke v oboch pripadoch a preto su preferovane.
NOTE Prikaz printf
interpretuje number format 0N
, t.j. integer cislo zacinajuce nulou ako octal hodnotu, preto printf '%d' 11
vypise 11 ale printf '%d' 011
vypise ako 9. Nezamienat, nepliest si s formatom \nnn
pre escape codes, ktore zacina znakom \
(backslash).
Pre zobrazenie standardneho vystupu sa najbeznejsie pouzivaju prikazy cat(1) a less(1), ktory nahradza dnes uz zastarany more(1). Prikaz less
zobrazuje vacsinu control codes znakov by default (option -U
pridava navyse aj backspaces, tabs and carriage returns) a pre zobrazenie ANSI "color" escape sequences je potrebne pouzit option -R
. Naopak, prikaz cat
by default zobrazuje "farebne" a pre zobrazenie non-printing bytes, znakov je potrebne pouzit option -v
alebo rovno -A
. Citame man stranky.
$ echo -ne '\t' | cat -A ^I $ printf '\u0009' | less -U --quit-if-one-screen #--quit-if-one-screen
=-F
^I
Moderny Bash este ponuka moznost zapisu pomocou ANSI-C Quoting, kedy je zapis $string
specialne osetreny (nie je POSIX). Backslash \
escape sequences su dekodovane ako ANSI C standard. Kedze je to Bash rozsirenie, nie je podstatne, ci pouzijem prikaz printf
alebo echo
(without option -e
). Pouzivanie ANSI-C Quoting s printf
je skor ojedinele, kedze prikaz sam o sebe formatuje ako C printf
.
NOTE Bash ANSI-C Quoting dekoduje octal value v tvare \nnn
(one to three octal digits).
$ bla1=$'\011'; bla2=$'\11'; bla3=$'\t'
$ (echo -n "$bla1"; echo -n "$bla2"; echo -n "$bla3") | cat -A
^I^I^I
$ (printf "$bla1"; printf "$bla2"; printf "$bla3") | less -U --quit-if-one-screen
^I^I^I
CSI (Control Sequence Introducer) sequences
Jedna z najpouzivanejsich escape sequences je Control Sequence Introducer (CSI), t.j. sequences ESC
[
, resp. ^[
[
(napisane ako \e[
, \033[
alebo \x1b[
), za ktorou nasleduje nejaky lubovolny pocet bytes z rozsahu urcenom pre escape sequences, t.j. z rozsahu 32—126, resp. 0x20—0x7E. Znova plati, ak je byte (znak) mimo daneho rozsahu, spravanie terminalu nie je definovane. CSI je definovana ako sequences ESC
[
nasledovana lubovolnym (alebo ziadnym) poctom parameter bytes, nasledovana lubovolnym (alebo ziadnym) poctom intermediate bytes, a na koniec, nasledovana jednym final byte (vid tabulka).
CSI (Control Sequence Introducer) sequences ESC [ followed by
| ||
none or any numbers of parameter bytes |
none or any numbers of intermediate bytes |
single final byte |
0x30—0x3F | 0x20—0x2F | 0x40—0x7E |
48—63 | 32—47 | 64—126 |
0–9:;<=>? |
space !"#$%&'()*+,-./ |
@A–Z[\]^_`a–z{|}~
|
subset of private bytes* | ||
< = > ? |
p–z { | } ~
| |
reserved byte* for future standardization | ||
: |
||
typically used bytes | ||
0–9 ; |
are not usually used | A–Z a–o
|
NOTE A subset of bytes was declared "private" so that terminal manufacturers could insert their own sequences without conflicting with the standard. NOTE All escape sequences with "reserved" byte : must be ignored. It was left for future standardization.
|
Typicka CSI sequences je CSI
n1
;
n2
;
...
(intermediate bytes)
final byte
. Final byte je pismeno urcujuce prikaz (funkciu). Cisla(o) n su parametre tohto prikazu (funkcie) oddelene bodkociarkou (semicolon). Final byte moze byt modifikovany pouzitim intermediate bytes, ale zvycajne sa nepouzivaju. Cisla n nie su povinne parametre, a pokial su vynechane, ich implicitna hodnota zavisi od prikazu, ale obycajne je to 0
alebo 1
. Sekvenciu 1;2;3
mozno zapisat aj ako 1;;3
, kedy sa za prostredne cislo bude povazovat 0
pripadne 1
.
SGR (Select Graphic Rendition) parameters
Escape sequence s ktorou sa uzivatel asi najskor stretne je control sequence CSI
n
m
, nazyvana ako Select Graphic Rendition (SGR), a ktora nastavuje rozne hodnoty zobrazenia (terminal zobrazuje farebne). Niekolko atributov moze byt nastavenych spolu v tej istej sekvencii n1;n2;n3;...
oddelenych navzajom bodkociarkou (semicolon). Kazdy atribut zobrazenia zostava v platnosti, pokial ho nasledujuca SGR sekvencia nezmeni, resp. neresetuje. Vynechany, resp. ziaden parameter v SGR sequence CSI
m
sa interpretuje ako CSI
0
m
, t.j. reset vsetkych atributov na ich default hodnotu. SGR sa tiez zvykne oznacovat aj ako ANSI color escape sequences.
Examples
- https://en.wikipedia.org/wiki/ANSI_escape_code#CSIsection
- https://en.wikipedia.org/wiki/ANSI_escape_code#SGR
- https://man7.org/linux/man-pages/man4/console_codes.4.html
- ANSI Escape Sequences (Gist on GitHub)
Code | Abbr | Name | Effect |
---|---|---|---|
ESC [ n D
|
CUB | Cursor Back | Moves the cursor n (default 1 ) cells in the left.
|
ESC [ n K |
EL | Erase in Line | Erases part of the line. If n is 0 (or missing), clear from cursor to the end of the line. If n is 1 , clear from cursor to beginning of the line. If n is 2 , clear entire line. Cursor position does not change.
|
ESC [ n m |
SGR | Select Graphic Rendition | Sets display attributes (colors and style) of the characters following this code. |
# we will mainly use hexadecimal notation '\x1b' $ printf '1234567890\x1b[Dafter\n' 123456789after $ printf '1234567890\x1b[8Dafter\n' 12after890 $ printf '1234567890\x1b[8D\x1b[Kafter\n' 12after $ printf '1234567890\x1b[8D\x1b[1Kafter\n' after890 $ CUB3='\x1b[3D' $ printf '12345%b#\n12345%b\x1b[K\n12345%b\x1b[1K\n' "$CUB3" "$CUB3" "$CUB3" 12#45 12 45
# example of DEC private mode sequences (DECTECM) $ printf '\x1b[?25l' # cursor invisible $ printf '\x1b[?25h' # cursor visible (by default)
$ SGR='\x1b[%bm' # SGR with parameter(s) $ printf 'normal'"$SGR"'effect'"$SGR"'reset\n' "4" "0" normaleffectreset $ printf 'normal'"$SGR"'effect'"$SGR"'reset\n' "3;31" "0" normaleffectreset $ printf 'normal'"$SGR"'effect'"$SGR"'reset\n' "1;32;41" "0" normaleffectreset $ printf 'normal'"$SGR"'effect'"$SGR"'reset\n' "1;3;4;35" "0" normaleffectreset $ printf "normal${SGR}effect${SGR}reset\n" "1" "0"
Code | Color | SGR Color depth | Note |
---|---|---|---|
ESC [ 30+i m ESC [ 40+i m |
foreground background |
3-bit (8 colors) | The original specification only had 8 basic colors; i ∈ <0, 7> |
ESC [ 90+i m and 3-bitESC [ 100+i m and 3-bit |
foreground background |
4-bit (16 colors) | Later terminals added another 8 "bright" colors; i ∈ <0, 7> |
ESC [ 38;5;ni m ESC [ 48;5;ni m |
foreground background |
8-bit (256 colors) | ni is a number from wiki table; i ∈ <0, 255> |
ESC [ 38;2;ri;gi;bi m ESC [ 48;2;ri;gi;bi m |
foreground background |
24-bit (true colors) | About 16M of RGB colors; i ∈ <0, 255> |
NOTE Moderny terminal vacsinou pouziva nejaku preddefinovanu paletu zakladnych 8/16 farieb z built-in schemes. GNOME Terminal by default pouziva "GNOME" palette schema, kde napr. zakladna modra (34/44) je RGB(18,72,139), ale napr. "Linux console" palette schema pouziva pre zakladnu modru RGB(0,0,170). Okrem toho, uzivatel si v principe moze kazdu zo zakladnych farieb nastavit podla uvazenia.
# GNOME Terminal with "Linux console" palette built-in schema $ SGRC='\x1b[%bm'; SGRR='\x1b[0m' # SGR with parameter (color) and reset $ printf "$SGRC"'3-bit blue'"$SGRC"' 4-bit bright blue'"$SGRR"' reset\n' "34" "94" 3-bit blue 4-bit bright blue reset $ printf "$SGRC"'8-bit standard blue'"$SGRC"' 8-bit high-intensity blue'"$SGRR"' reset\n' "38;5;4" "38;5;12" 8-bit standard blue 8-bit high-intensity blue reset $ printf "$SGRC"'24-bit RGB(0,0,170)'"$SGRC"' 24-bit RGB(85,85,255)'"$SGRR"' reset\n' "38;2;0;0;170" "38;2;85;85;255" 24-bit RGB(0,0,170) 24-bit RGB(85,85,255) reset $ printf "$SGRC"'FG: 4-bit bright white, BG: 24-bit RGB(85,85,255)'"$SGRR"' reset\n' "97;48;2;85;85;255" FG: 4-bit bright white, BG: 24-bit RGB(85,85,255) reset $ printf "$SGRC"'FG: 4-bit bright white, BG: 24-bit RGB(85,85,255)'"$SGRR"' reset\n' "97;48;2;85;85;255" | cat -v ^[[97;48;2;85;85;255mFG: 4-bit bright white, BG: 24-bit RGB(85,85,255)^[[0m reset $ printf "$SGRC"'FG: 4-bit bright white, BG: 24-bit RGB(85,85,255)'"$SGRR"' reset\n' "97;48;2;85;85;255" | less -F ESC[97;48;2;85;85;255mFG: 4-bit bright white, BG: 24-bit RGB(85,85,255)ESC[0m reset
Examples commands
Niektore Linux prikazy vytvaraju control escape sequences output. Najbeznejsie sa stretneme s prikazmi ls(1) alebo grep(1), ktore umoznuje "colorize the output". Obidva prikazy pouzivaju option --color=auto
(emits color codes only when standard output is connected to a terminal) by default . Pre experimentovanie je vhodnejsie pouzit --color=always
option. Environment variable LS_COLORS
, resp. GREP_COLORS
umoznuje menit preddefinovane nastavenia.
Prikaz ls
rozlisuje rozne farby pre rozne typy suborov. Environment variable LS_COLORS
je nastavena (by default) prostrednictvom /etc/DIR_COLORS. Prikaz dircolors(1) nastavi (a vypise) hodnotu LS_COLORS
. Viac info dir_colors(5).
$ mkdir ls_test; cd ls_test; touch 111.txt 222.gz 333.xz 444.dat zzz; mkdir dir555 $ ls 111.txt 222.gz 333.xz 444.dat dir555 zzz $ ls --color=always | less -F 111.txt ESC[0mESC[01;31m222.gzESC[0m # SGR reset → SGR effect → 222.gz → SGR reset ESC[01;31m333.xzESC[0m # SGR effect → 333.xz → SGR reset 444.dat ESC[01;34mdir555ESC[0m # SGR effect → dir555 → SGR reset zzz
Prikaz grep
pouziva environment variable GREP_COLORS='ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36'
(hodnota by default). Viac info GREP_COLORS.
$ printf '111x222' | grep 'x'
111x222
$ printf '111x222' | grep --color=always 'x' | less -F
111ESC[01;31mESC[KxESC[mESC[K222 # 111 → SGR effect → EL default → x → SGR reset → EL default → 222
# EL (Erase in Line) default = erase from cursor to the end of the line. Cursor position does not change. Capability ne from GREP_COLORS
is false by default.
$ # export GREP_COLORS='mt=01;34' # change only mt capability (others without changes) $ # export GREP_COLORS='' # reset all to default $ printf '111x222' | GREP_COLORS='mt=32:ne' grep 'x' 111x222 $ printf '111x222' | GREP_COLORS='mt=32' grep --color=always 'x' | less -F # boolean capability ne (no EL) is false by default 111ESC[32mESC[KxESC[mESC[K222 # 111 → SGR effect → EL default → x → SGR reset → EL default → 222 $ printf '111x222' | GREP_COLORS='mt=32:ne' grep --color=always 'x' | less -F # capability ne is true 111ESC[32mxESC[m222 # 111 → SGR effect → x → SGR reset → 222
NOTE Najlepsia dokumentacia je priamo zdrojovy kod grep.c. Vysvetlene, okrem ineho, preco vyvojari zaviedli (Why have EL to Right after SGR?) capability ne from GREP_COLORS
a preco je by default false.
# replace SGR Escape sequences in color grep by using the backspace character, resp. by overstriking with backspaces (see bellow) G_COLOR='\x1b\[31m' # SGR color G_RESET='\x1b\[m' # SGR reset G_UNDER='_\x08' # underline (overstriking with backspace) printf '111x222' | GREP_COLORS='mt=31:ne' grep --color=always 'x' | sed -e "s/$G_COLOR/$G_UNDER/g" -e "s/$G_RESET//g" | less -F # -U
man page formatting
- https://en.wikipedia.org/wiki/Man_page
- https://en.wikipedia.org/wiki/Groff_(software)
- https://wiki.archlinux.org/title/Groff
man-db package na Fedore implementuje rozne nastroje, resp. prikazy na prezeranie man stranok. Prikaz man(1) formatuje a zobrazuje manualove stranky. Package man-pages obsahuju samotne man stranky s obsahom. By default man
pouziva terminal pager program less
pre zobrazenie obsahu.
Default format pre man stranky je troff(1), ktory umoznuje spolu s roznymi makro balikmi formatovat pre rozne vystupy ako napr. TTY (stdout), PDF, html a pod. OpenBSD od roka 2010 pouziva mandoc namiesto troff pre formatovanie man stranok. Moderny Linux pouziva groff(7) (the GNU implementation of roff(7)). Tu zacina "terminologicka dzungla" roff, troff, nroff, groff, grotty a dalsie desiatky terminov, resp. prikazov. Jedina vyhoda, v pripade ak sa clovek rozhodne v tom vyznat, vsetko je podrobne zdokumentovane v man strankach. Existuje tu akasi paralela: TeXLive vs. groff, TeX vs. troff/roff atd.
man(7) vysvetluje ako sa pouzivaju makra pre formatovanie man stranok, ktore vytvara prikaz groff(1).
$ man -w git /usr/share/man/man1/git.1.gz $ zcat /usr/share/man/man1/git.1.gz | groff -T utf8 -m man #-m man
=-man
$ zcat /usr/share/man/man1/git.1.gz | groff -T utf8 -m man | less -R # less pager $ zcat /usr/share/man/man1/git.1.gz | groff -T pdf -m man > man_git.pdf # dnf install groff-perl (provides gropdf, PDF postprocessor for groff)
-T utf8
set TTY output devices, text with UTF-8 encoding, postprocessor is grotty(1). -m man
include man macro packages for traditional man page format, see groff_man(7). Existuju rozne vystupne zariadenia, napr. default PS (grops), PDF (gropdf), dvi (grodvi), html (grohtml) a samozrejme TTY output devices (grotty) s textom v roznom kodovani a zaroven existuju rozne macro packages, napr. man (groff_man), mdoc (groff_mdoc), me (groff_me), ms (groff_ms) a podobne, see groff_tmac(5).
NOTE Niekedy sa este zvykne pouziva prikaz nroff(1) - use groff to format documents for TTY devices. GNU nroff emulates the traditional Unix nroff command using groff. nroff generates output via grotty. V praxi je to shell script /usr/bin/nroff, resp. groff wrapper, ktory nastavi TTY output device s aktualne pouzivanim kodovanim. Pre Fedora Linux nroff
= groff -mtty-char -Tutf8
.
groff(7), resp. groff_man(7) obsahuju dokumentaciu pre strukturu, syntax, formatovanie, ktore sa pouziva, okrem ineho, pre man stranky. Prikazy roff jazyka (historically, the roff language was called troff) su v podstate len dvoch druhov.
- requests sa pisu vzdy na vlastny riadok (roff je "line-oriented" jazyk, t.j. co riadok to prikaz) a zacinaju
.
alebo'
. - escape sequences are in-line functions and in-word formatting elements zacinajuce
\
(nezamienat s ANSI escape sequences).
Casto sa pouziva font escape sequence \f
za ktorou nasleduje indikator B (bold), I (italic, underline) or R (regular, roman). Jednoduchy priklad man stranky, resp. roff jazyka, subor exam.roff (priklad na formatovanie a nie strukturu man stranok).
.TH EXAMPLE 1 \" Title heading (request)
.SH DESCRIPTION \" Section heading (request)
.B bold text \" bold (request)
next \fBbold \fIitalic OR underline \" bold, italic (escape sequences)
\fRregular OR roman \" regular (escape sequences)
$ groff -T utf8 -man exam.roff DESCRIPTION bold text next bold italic OR underline regular OR roman
Man stranky v principe pouzivaju len tri sposoby formatovania textu, resp. font changes: regular, bold, italic/underline. Existuje moznost formatovat priamo aj farebny text, napr. \m[green]\fBgreen bold \fR\m[]reset
→ green bold reset
, ale tato moznost sa dorazne neodporuca hlavne kvoli maximalnej portabilite (man stranky sa zobrazuju na velmi sirokom spektre zariadeni, terminalov). groff
option -c
disable color output, alebo inymi slovami, ignoruje \m
escape sequence.
Prikaz groff
, resp. grotty(1) postprocessor (zaujima nas vystup na terminal), by default, formatuje (zobrazuje) change text attributes (bold, italic/underline pripadne colors) pomocou SGR Escape sequence na TTY zariadeniach.
$ printf '.TH EXAM\n\\fBbold\\fIitalic\\fRregular' | groff -T utf8 -man
bolditalicregular
$ printf '.TH EXAM\n\\fBbold\\fIitalic\\fRregular' | groff -T utf8 -man | less -F # less, by default, display control characters
ESC[1mboldESC[4mESC[22mitalicESC[24mregular # SGR effect(1=bold) → bold → SGR effect(4=underline) → SGR effect(22=normal) → italic → SGR effect(24=not underlined) → regular
$ printf '.TH EXAM\n\\fBbold\\fIitalic\\fRregular' | groff -T utf8 -man | less -F -R # less -R
display SGR (ANSI escape sequences are output in "raw" form)
bolditalicregular
Existuje aj starsi, ale este stale pouzivany sposob pre zobrazenie bold a underline, from less FAQ: Some text files, notably Unix-style "man pages", use overstriking (backspaces) to simulate underlined and bold text. Less interprets this overstriking and displays the text in real underlined or bold mode. Tato analogia zobrazovania pochadza este z klasickych pisacich strojoch (typewriters).
- bold use letter + backspace + letter sequences to indicate a letter should be displayed in bold face (
a
+backspace
+a
= a). - underline use underscore + backspace + letter sequences to indicate a letter should be displayed in underline face (
_
+backspace
+a
= a).
$ printf 'a\ba' | less -F # -U # less -U
display backspaces, tabs and carriage returns control characters
a # a^Ha
$ printf '_\ba' | less -F # -U
a # _^Ha
Niektore terminaly, napr. kterm, ale aj starsie verzie programu less
(resp. without -R
option) nevedia zobrazovat SGR Escape sequence. V pripade ak potrebujeme formatovat (zobrazit) man stranky starsim sposobom (using the backspace character), potrebujeme nastavit grotty(1) environment variable GROFF_NO_SGR
alebo pouzit option -c
(grotty -c
= groff -P-c
).
$ printf '.TH EXAM\n\\fBbold\\fIitalic\\fRregular' | GROFF_NO_SGR=1 groff -T utf8 -man | less -F -U $ printf '.TH EXAM\n\\fBbold\\fIitalic\\fRregular' | groff -T utf8 -man -P-c | less -F -U b^Hbo^Hol^Hld^Hd_^Hi_^Ht_^Ha_^Hl_^Hi_^Hcregular
$ zcat /usr/share/man/man1/git.1.gz | groff -T utf8 -man | less # using SGR Escape sequence (by default)
$ zcat /usr/share/man/man1/git.1.gz | groff -T utf8 -man -P-c | less -U # using the backspace character (SGR disabled) or set GROFF_NO_SGR
Prave tento starsi sposob zobrazovania (using the backspace character) sa da vyuzit na "farebne" zobrazovanie man stranok. "Farebne" znamena len jednoduchu zamenu bold a underline text attributes na nejaku nami definovanu farbu. Tuto zamenu umoznuje option -D
(or long --color
) programu less
, ktory je terminal pager program pre man stranky. Samotne man stranky su formatovane bez farby aj napriek tomu, ze tato moznost existuje (vid vyssie), ale z dovodu maximalnej portability sa vsak neodporuca.
$ zcat /usr/share/man/man1/git.1.gz | groff -T utf8 -man -P-c | less --color=d+r --color=u+b # color man page (bold+red and underline+blue)
$ printf '.TH EXAM\n\\fBbold\\fIitalic\\fRregular' | GROFF_NO_SGR=1 groff -T utf8 -man | less -F -Dd+y -Dur bolditalicregular
Pozorne citame less(1) man stranky. -Dur
change color for underlined text (u) on red (r), -Dd+y
change bold text (d) on yellow (y) color and (+) displays bold text as both yellow and in bold format. Podstatne je, ze tento "change color" pracuje iba pre formatovanie (zobrazovanie) vytvorene starsim sposobom (using the backspace character, resp. by overstriking with backspaces) a nie pre text formatovany cez SGR Escape sequence, co je by default sposob pre groff. Prave preto je potrebne pouzit grotty environment variable GROFF_NO_SGR
alebo option -c
. Kedze uz mame farebny vystup je vhodne pouzit aj option --use-color
, ktora "zafarbi" aj samotny program less
(stavovy riadok, vyhladavanie a podobne).
Summarum Color for man pages (MANROFFOPT
and MANPAGER
are man
environment variables)
$ MANROFFOPT='-c' MANPAGER='less -Dd+y -Dur --use-color' man git
or export variables (use of LESS_TERMCAP_*
variables is not necessary).
export MANROFFOPT='-c' # or GROFF_NO_SGR='1' export MANPAGER='less -Dd+y -Dur --use-color'
Bash color prompt
- https://www.gnu.org/software/bash/manual/html_node/Controlling-the-Prompt.html
- https://wiki.archlinux.org/title/Bash/Prompt_customization
- https://src.fedoraproject.org/rpms/shell-color-prompt/blob/rawhide/f/bash-color-prompt.sh
- https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh (search color)
NOTE for Bash prompt use C escape \e[
or octal \033[
CSI notation (hex \x1b[
not working).
PS1='[\u@\h \W]\$ ' # traditional prompt PS1='[\u@\h_\e[44m_BLUE_\e[0m_\W]\$ ' # add colored BLUE word !!! don't use !!! (works but with issues)
bash(1) recommended wrapping output in \[
and \]
(use \[\e[COLORm\]
instead of \e[COLORm
), this helps Bash ignore non-printable characters. The wrap will not work for surrounding non-printable characters (command substitution). Instead you can use the octal escapes \001
and \002
(use \001\e[COLORm\002
). More info [1] or [2].
PS1='[\u@\h_\[\e[44m\]_BLUE_\[\e[0m\]_\W]\$ ' # OK PS1='[\u@\h_\001\e[44m\002_BLUE_\001\e[0m\002_\W]\$ ' # OK (more universal)