1. Overview

The Linux console is a text-based interface that allows users to interact with the operating system using commands and keyboard shortcuts. One of the features of the Linux console is the cursor.

Basically, the cursor is a small symbol that indicates the position where the next character will be typed or displayed. Further, the cursor can have different characteristics depending on our preferences, the console settings, and supported features.

In this tutorial, we’ll understand how to change the cursor shape, color, and blink rate of a Linux console. The commands used in this tutorial were tested in Ubuntu 22.04 with GNOME Version 42.5.

2. The Linux Console Cursor

The Linux console cursor can have three basic shapes:

  • block: solid or hollow rectangle that covers the entire character cell
  • underscore: horizontal line that spans the bottom of the character cell
  • vertical bar: vertical line that spans the left or right side of the character cell

Notably, the default shape of the Linux console cursor is usually a block, but it can vary depending on the console driver and the terminal emulator program.

Another reason for cursor differences is the current mode:

  • insert: append text at the cursor position
  • overwrite: replace the character below the cursor position

Knowing these conditions, let’s check how we can change the cursor manually.

3. Changing Cursor Shape

One way to change the cursor shape in the Linux console is to use escape sequences with the echo command.

Basically, escape sequences are special codes that control the behavior and appearance of the terminal. Further, escape sequences start with the Escape character (ASCII 27), followed by one or more characters that specify a command and parameters.

So, to change the cursor shape using escape sequences, there’s a general format:

$ echo -ne "\e[5 q"

In the command line above, the -n option suppresses the newline character at the end of the output. Further, the -e option enables the interpretation of escape characters.

The argument to echo has several parts:

  • \e: represents the Escape character
  • [: indicates the start of the command
  • 5: specifies the desired cursor shape via a predefined numeric code
  • q: end of the command

The table below shows the numbers and their corresponding cursor shapes for the GNOME terminal:

Number GNOME Terminal
0 Default
1 Block (blinking)
2 Block (steady)
3 Underline (blinking)
4 Underline (steady)
5 Bar (blinking)
6 Bar (steady)

Now, let’s see a specific example of changing the cursor shape:

$ echo -ne "\e[6 q"

This changes the cursor to a steady bar.

4. Changing the Cursor Color

The cursor color can be changed by using escape sequences as well:

$ echo -ne "\e]12;#RRGGBB\a"

Let’s break down this command:

  • 12: specifies the desired cursor color via a predefined numeric code
  • ;: separates the parameters
  • #RRGGBB: represents the desired color in a red-green-blue hexadecimal code or via a name
  • \a: end of the command

For instance, let’s change the cursor color to green in our terminal:

$ echo -ne "\e]12;#00FF00\a"

We can also change the cursor color to white:

$ echo -ne "\e]12;#FFFFFF\a"

Moreover, we can use the name of the color instead of the RGB value:

$ echo -ne "\e]12;yellow\a"

The cursor color should change to yellow after running this code snippet.

The cursor blink rate is how fast or slow the cursor alternates between visible and invisible states. Now, let’s explore different methods to change the cursor blink rate.

5.1. Using Escape Sequences

We can turn the cursor blink on by using escape sequences:

$ echo -ne "\e[?25h"

If the cursor blink is on, we can also turn it off:

$ echo -ne "\e[?25l"

Therefore, we can deduce the function of each character from the code snippets above:

  • ?: indicates a private mode
  • 25: corresponds to the cursor visibility
  • h: means set mode
  • l: means reset mode

A private mode control sequence is a type of ANSI escape code used to control various settings and behaviors of a terminal. Further, these sequences are considered private because they are specific to a particular terminal or terminal emulator and may not be universally supported by all terminals. Notably, private modes aren’t dictated by specifications but are still supported by most terminals.

We can also use other methods to turn on and off the cursor blink if the escape sequences don’t work or aren’t convenient.

5.2. Using gsettings

Basically, gsettings is a command that allows us to change various settings of the GNOME desktop environment:

  • themes
  • icons
  • fonts
  • cursor features

Of course, in this case, the last point is of interest to us.

In particular, gsettings can be used to turn on and off the cursor blink by using the org.{desktop_type}.desktop.interface schema with either true or false values for the cursor-blink key.

For instance, we can turn off the cursor blink in GNOME Terminal:

$ gsettings set org.gnome.desktop.interface cursor-blink false

We can now turn it back on:

$ gsettings set org.gnome.desktop.interface cursor-blink true

Notably, we can replace gnome with the relevant desktop environment such as cinnamon or mate for Cinnamon or MATE respectively.

Further, the cursor-blink-time key specifies the duration of one cycle of the cursor blink in milliseconds. The default value is 1200, meaning the cursor blinks every 1.2 seconds. Thus, we can change this value to any number between 100 and 2500 depending on how fast or slow we want the cursor to blink.

For instance, let’s change the cursor blink rate to 600 milliseconds in the GNOME Terminal:

$ gsettings set org.gnome.desktop.interface cursor-blink-time 500

This makes the cursor blink two times faster than the default rate.

Afterward, we can also change it back to the default:

$ gsettings set org.gnome.desktop.interface cursor-blink-time 1200

Thus, we restore the cursor blink rate to its default of 1.2 seconds.

6. Conclusion

In this article, we learned how to change the cursor shape, color, and blink rate of a Linux console using escape sequences. Further, we looked at how to use gsettings to change the cursor blink rate.

These techniques can help us customize the appearance and behavior of the cursor according to our preferences and needs. However, some methods may not be compatible with all terminals and consoles.

Comments are closed on this article!