Introduction to the linux setterm Command

Why doesn’t everyone understand this?~Introduction to the linux setterm Command1. What is setterm

https://man.he.net/man1/setterm

setterm – Set terminal attributes

setterm writes a character string to standard output that invokes the specified terminal function. It consults terminfo to find the string to use whenever possible. However, some options (marked as “only for virtual consoles”) do not correspond to functions in terminfo(5). In such cases, if the terminal type is “con” or “linux”, it outputs the string that invokes the specified function on the PC Minix virtual console driver. Options that are not implemented by the terminal will be ignored.

2. Parameters

[root@master1 ~]# setterm -hsetterm: Invalid argument.
Usage: setterm [options]
Options: -term <terminal_name>    Specify terminal type name (set terminal type)
-reset    Reset terminal settings (restore default terminal configuration)
-initialize    Initialize terminal (initialize terminal parameters)
-cursor <on|off>    Control cursor display (turn on/off cursor display)
-repeat <on|off>    Keyboard key repeat function (turn on/off key repeat)
-appcursorkeys <on|off>    Application cursor key mode (turn on/off application cursor key mode)
-linewrap <on|off>    Automatic line wrap function (turn on/off automatic line wrap)
-default    Restore default terminal settings (reset to default terminal configuration)
-foreground <default|black|blue|cyan|green|magenta|red|white|yellow>    Set foreground color (default/black/blue/cyan/green/magenta/red/white/yellow)
-background <default|black|blue|cyan|green|magenta|red|white|yellow>    Set background color (default/black/blue/cyan/green/magenta/red/white/yellow)
-ulcolor <black|blue|bright|cyan|green|grey|magenta|red|white|yellow>    Set underline color (black/blue/bright/cyan/green/grey/magenta/red/white/yellow)
-ulcolor <black|blue|bright|cyan|green|grey|magenta|red|white|yellow>    (duplicate, same as above, set underline color)
-hbcolor <black|blue|bright|cyan|green|grey|magenta|red|white|yellow>    Set highlight background color (black/blue/bright/cyan/green/grey/magenta/red/white/yellow)
-hbcolor <black|blue|bright|cyan|green|grey|magenta|red|white|yellow>      (duplicate, same as above, set highlight background color)
-inversescreen <on|off>    Screen inverse display (turn on/off screen inverse)
-bold <on|off>    Bold text display (turn on/off text bold)
-half-bright <on|off>    Half-brightness display (turn on/off half-brightness mode)
-blink <on|off>    Text blinking effect (turn on/off text blinking)
-reverse <on|off>    Inverse display (turn on/off inverse mode)
-underline <on|off>    Underline display (turn on/off underline)
-store    Store current terminal settings (save current terminal configuration)
-clear <all|rest>    Clear screen operation (clear all/remaining screen content)
-tabs <tab1 tab2 tab3 ...> (tabn = 1-160)    Set tab positions (specify tab positions, range 1-160)
-clrtabs <tab1 tab2 tab3 ...> (tabn = 1-160)    Clear specified tabs (clear tabs at specified positions, range 1-160)
-regtabs <1-160>    Set equidistant tabs (set equidistant tabs, range 1-160)
-blank <0-60|force|poke>    Screen blank time setting (set screen blank time, 0-60 minutes or force/wake)
-dump <1-NR_CONSOLES>    Dump screen content (dump screen content of specified console)
-append <1-NR_CONSOLES>    Append screen content (append screen content of specified console)
-file dumpfilename    Specify dump filename (set filename for dumping screen content)
-msg <on|off>    Message display control (turn on/off message display)
-msglevel <0-8>    Set message level (set message display level, 0-8)
-powersave <on|vsync|hsync|powerdown|off>    Power saving mode (turn on/vertical sync/horizontal sync/power down/off power saving)
-powerdown <0-60>      Power down time setting (set power down time, 0-60 minutes)
-blength <0-2000>    Blink cycle length (set blink cycle length, 0-2000 milliseconds)
-bfreq freqnumber    Blink frequency setting (set blink frequency, specify frequency value)
-version      Display version information (show version information of setterm command)
-help      Display help information (show help information of setterm command)
For more information, see setterm(1).

3. Examples

1. Modify terminal foreground and background colors, set terminal text color to red and background to black

setterm -foreground red -background black

2. Turn cursor display on/off

setterm -cursor off   # Hide cursor
setterm -cursor on    # Show cursor

3. Enable text blinking effect

setterm -blink on     # Turn on blinking
setterm -blink off    # Turn off blinking

4. Clear screen (clear all content), the clear command only scrolls the screen,<span>setterm -clear all</span> will actually clear the content.<span>setterm -clear rest</span> only clears the content from the cursor position to the end of the screen

setterm -clear all

5. Set tab positions

setterm -tabs 5 10 15 20  # Set tabs at columns 5, 10, 15, 20
setterm -regtabs 8         # Set equidistant tabs (every 8 columns)

6. Inverse display (black background white text ↔ white background black text) effect: similar to “high contrast mode”, suitable for bright environments.

setterm -inversescreen on   # Turn on inverse
setterm -inversescreen off  # Turn off inverse

7. Save/restore terminal settings

setterm -store                  # Save current settings to terminal
setterm -default                # Restore terminal default settings

8. Debug terminal type

setterm -term xterm-color       # Force set terminal type to xterm-color

9. Dump screen content to file

setterm -dump 1 -file /tmp/screen.dump  # Dump current console (e.g., /dev/tty1) content to file

10. Combined usage (Example: red blinking warning)

setterm -foreground red -blink on
echo "WARNING: System overload!"
setterm -blink off -foreground default  # Restore default

Notes

  1. Terminal compatibility: Some options (such as blinking, inverse) may not work due to differences in terminal emulators (such as GNOME Terminal, xterm).
  2. Permission requirements: Operating other consoles (e.g., -dump 1) requires root privileges.
  3. Persistence: Modifications made by setterm are only valid for the current terminal session and will be lost upon exit.

If more complex terminal control is needed, it can be combined with <span>tput</span> or <span>stty</span> commands.

4. Supplement

Compatibility

Since version 2.25, setterm supports long options with two hyphens, such as –help, and also supports legacy long options with a single hyphen, such as -help. In scripts, it is best to use the backward-compatible single hyphen rather than double hyphens. Currently, there are no plans or sufficient reasons to stop supporting single hyphen compatibility.

Defects

Differences between Minix and Linux versions are not documented.

See also

stty(1), tput(1), tty(4), terminfo(5)

Report bugs, for bug reporting, please use the following issue tracker:

https://github.com/karelzak/util-linux/issues

Availability, the setterm command is part of the util-linux package, which can be downloaded from the Linux kernel archive:

https://www.kernel.org/pub/linux/utils/util-linux/

5. Summary

Example screenshots do not show the effect very well, but by debugging the terminal style, the display and printing can be more outstanding, and the colors can look better.

Last but not least, feel free to communicate:
Follow the public account to leave a message, or leave a message directly below:

Leave a Comment