setfont Command in Linux



The setfont command in Linux is used to load EGA/VGA console screen fonts. It allows users to change the font and font size in the console (virtual terminal). This can be particularly useful for improving readability or for aesthetic preferences when working in a console environment.

Table of Contents

Here is a comprehensive guide to the options available with the setfont command −

Understanding setfont Command

The setfont command reads a font from a file and loads it into the EGA/VGA character generator. It can also load various mapping tables such as Unicode-to-font index maps and screen maps.

Why Use setfont?

The default console font may not be ideal for all users or situations. By using the setfont command, you can:

Improve readability by choosing a larger or clearer font. Customize the appearance of your console to match your preferences. Support additional characters and symbols, which may be necessary for certain languages or applications.

Syntax of setfont Command

The basic syntax of the setfont command is:

setfont [OPTION]... [FONTFILE]...

Here,

  • OPTION: Various options to modify the behavior of the command.
  • FONTFILE: The font file to be loaded.

setfont Command Options and Parameters

Here are some commonly used options with setfont:

  • -h or --help: Display help information.
  • -v or --version: Display the version of the command.
  • -V or --verbose: Enable verbose mode.
  • -o or --old: Use the old kernel interface.
  • -n or --no-remap: Do not remap the font.
  • -u or --unicode: Load the Unicode map.

Font Files

Font files used with setfont typically have extensions like .psf, .psfu, or .sfnt. These files contain the bitmap representations of the characters to be displayed on the console.

How to Use setfont Command in Linux?

To load a new font, you need to have a font file available. Suppose you have a font file named ter-v16b.psf.

  • Loading a New Font − Use the setfont command with the path to the font file.
  • Loading a Unicode Font − Use the -u option to load fonts with Unicode mappings.
  • Verifying the Current Font − Use the showconsolefont command to display the current font.
  • Setting the Default Console Font − Modify the /etc/default/console-setup file to set the default font at boot.
  • Changing Font Size − Specify a different font file with the desired size.
  • Using Custom Fonts − Load custom font files by specifying their paths.

You can load this font using the setfont command −

sudo setfont /usr/share/consolefonts/ter-v16b.psf
Font Files

This command sets the console font to the one specified in the ter-v16b.psf file. The change takes effect immediately.

Loading a Unicode Font

Some fonts include Unicode mappings, which allow for the display of a broader range of characters. To load a Unicode font, you can use the -u option. Suppose you have a Unicode font file named Lat2-Fixed16.psfu. You can load this font using the command:

sudo setfont -u /usr/share/consolefonts/Lat2-Fixed16.psfu
Loading a Unicode Font

This command loads the Lat2-Fixed16.psfu font file and its Unicode mapping.

Verifying the Current Font

To verify the current font settings, you can use the showconsolefont command:

showconsolefont
Verifying the Current Font

This command displays the current font loaded in the console, including its character set.

Setting the Default Console Font

To set the default console font that is loaded at boot time, you need to modify the system configuration file. For most Linux distributions, this file is located at /etc/default/console-setup. Open this file in a text editor:

sudo nano /etc/default/console-setup

Edit the FONTFACE and FONTSIZE variables to specify the default font and size:

FONTFACE="Fixed"
FONTSIZE="8x16"
Setting the Default Console Font

Save the file and reboot the system for the changes to take effect.

Changing Font Size

You can change the font size by specifying a different font file that has the desired size. For example, if you want to use a larger font size, you might choose a file like ter-v32b.psf:

sudo setfont /usr/share/consolefonts/ter-v32b.psf
Changing Font Size

This command loads the ter-v32b.psf font file, which has larger characters.

Using Custom Fonts

If you have a custom font file that you want to use, you can specify its path with the setfont command. For example:

sudo setfont /path/to/custom-font.psf
Using Custom Fonts

This command loads the custom font specified by the file path.

Scripting with setfont

The setfont command can be included in scripts to automate font settings. Here is an example of a script that sets a specific font and size at startup:

#!/bin/bash

# Set the console font
sudo setfont /usr/share/consolefonts/Lat2-Terminus16.psf

# Display the current font
showconsolefont

You can add this script to your startup applications to ensure the font is set each time you log in.

Scenario 1: Improving Readability

You find the default console font too small and difficult to read. You want to use a larger and clearer font.

Find a suitable font file, such as ter-v24b.psf. Load the font using the setfont command:

sudo setfont /usr/share/consolefonts/ter-v24b.psf
Improving Readability

Verify the change with the showconsolefont command:

showconsolefont
Improving Readability 1

Scenario 2: Setting a Custom Font at Boot

You have a custom font that you want to be used every time the system boots.

Copy the custom font file to the appropriate directory, e.g., /usr/share/consolefonts/. Modify the /etc/default/console-setup file to set the custom font as the default:

FONTFACE="CustomFont"
FONTSIZE="16"

Reboot the system to apply the changes.

Scenario 3: Supporting Additional Characters

You need to work with files that contain characters not supported by the default console font. Find a Unicode font file that supports the required characters, such as Lat2-Fixed16.psfu.

Load the font using the setfont command:

sudo setfont -u /usr/share/consolefonts/Lat2-Fixed16.psfu
Supporting Additional Characters

Verify the change with the showconsolefont command:

showconsolefont
Supporting Additional Character

Conclusion

The setfont command is a versatile tool for managing console fonts in Linux. By using this command, you can customize the appearance of your console, improve readability, and support additional characters and symbols.

Advertisements