7

when I open a terminal I have a black background with white characters. I also have a green placeholder that indicates to me where currently is the cursor.

This behaviour is the same during any perl script that is running in the terminal.

How can I change within the script the color of the cursor indicator (specifically, I want it to disappear for a while and then come back)?

I'm using Linux and bash.

1
  • This depends heavily on your operating system and terminal software you are using. Mac / Linux / Windows?
    – amon
    Commented Jul 15, 2012 at 17:30

4 Answers 4

4

Nothing portable, but certain terminals accept escape sequences to change the cursor colour.

For example xterm accepts OSC 12 to set the colour

$ echo -e "\e]12;red\a"
4

Short answer

echo -n -e '\e[?0c'       #DISAPPEAR
echo -n -e '\e[?16;0;64c' #REAPPEAR
echo -n -e '\e[?16;0;80c' #REAPPEAR w/highlighting

Long answer

As of today, the current version of agetty (contained in util-linux 2.27.1 [util-linux is the linux package providing most core commands, like login, su, mount, more, kill - to name a few] - and you should have it if your linux kernel version is >=4.4) has a different behavior than described in the yet-to-be-updated kernel.org documentation (Software cursor for VGA).

Consider

echo -n -e '\e[?byte1;byte2;byte3c'

byte1:

+---------------+---------------+
|  high nibble  |   low nibble  |
+---+-----------+-----------+---+
|msb|           |           |lsb|
+---+---+---+---+---+---+---+---+
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
+---+---+---+---+---+---+---+---+
                  |   |   |   |
                  |   |   |   +-+
                  |   |   |     |   These bits specify the 8
                  |   |   +-----+-> possible blinking HW carets
                  |   |         |   
                  |   +---------+
                  |
                  +---------------> When set, this bit enables SW
                                    caret instead of HW caret

byte2 (SW caret):

                +-----------------> A pretty useless mask applied
                |                   to bits in byte3
+---------------+---------------+
|  high nibble  |   low nibble  |
+---+-----------+-----------+---+
|msb|           |           |lsb|
+---+---+---+---+---+---+---+---+
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
+---+---+---+---+---+---+---+---+

byte3 (SW caret):

+---------------+---------------+
|  high nibble  |   low nibble  |
+---+-----------+-----------+---+
|msb|           |           |lsb|
+---+---+---+---+---+---+---+---+
| 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
+---+---+---+---+---+---+---+---+
  |   |   |   |
  |   |   |   +-------------------> Highlighting
  |   |   |
  |   |   +---------------------+
  |   |                         |
  |   +-------------------------+-> Color
  |                             |
  +-----------------------------+

Highlighting: highligths the character beneath the caret (e.g. changing it to white [true shiny white] instead of dark white [the light gray that is commonly the default for TTYs]).

Color: the color of the caret. Note that, compared to the usual order of ANSI color codes we all know and love, bits are reversed, so -for this triplet- the 7th is the Lsb while the 5th is the Msb.

So, while in the 70s ANSI defined the following color codes, setting a de-facto standard adopted universally for TTYs, miscellaneous linux terminals, consoles, and whatnot

000 (0) black
001 (1) red
010 (2) green
011 (3) yellow or dark yellow
100 (4) blue or dark blue
101 (5) magenta, purple or violet
110 (6) cyan or light blue
111 (7) white or dark white (light gray)

in this case we have the opposite

000 (0) black
100 (4) red
010 (2) green
110 (6) yellow or dark yellow
001 (1) blue or dark blue
101 (5) magenta, purple or violet
011 (3) cyan or light blue
111 (7) white or dark white (light gray)

(Octal value parenthesized)

So, here comes the list:

#Hardware carets (blinking and [dark ]white)
echo -n -e '\e[?0c' #default caret
echo -n -e '\e[?1c' #invisible caret
echo -n -e '\e[?2c' #underscore caret
echo -n -e '\e[?3c' #thicker underscore caret
echo -n -e '\e[?4c' #smaller block caret
echo -n -e '\e[?5c' #small block caret
echo -n -e '\e[?6c' #big block caret
echo -n -e '\e[?7c' #biggest block caret
#On my Linux machine, both 6 and 7 are the big rectangular full-size block caret

#Software carets (non-blinking and colored)
echo -n -e '\e[?16;0;0c'  #00001000 0 00000000 black (thus invisible on black background)
echo -n -e '\e[?16;0;128c'#00001000 0 10000000 red
echo -n -e '\e[?16;0;64c' #00001000 0 01000000 green
echo -n -e '\e[?16;0;192c'#00001000 0 11000000 yellow
echo -n -e '\e[?16;0;32c' #00001000 0 00100000 blue
echo -n -e '\e[?16;0;160c'#00001000 0 10100000 magenta
echo -n -e '\e[?16;0;96c' #00001000 0 01100000 cyan
echo -n -e '\e[?16;0;224c'#00001000 0 11100000 dim white
echo -n -e '\e[?16;0;16c' #00001000 0 00010000 black     + highlighting
echo -n -e '\e[?16;0;144c'#00001000 0 10010000 red       + highlighting
echo -n -e '\e[?16;0;80c' #00001000 0 01010000 green     + highlighting
echo -n -e '\e[?16;0;208c'#00001000 0 11010000 yellow    + highlighting
echo -n -e '\e[?16;0;48c' #00001000 0 00110000 blue      + highlighting
echo -n -e '\e[?16;0;176c'#00001000 0 10110000 magenta   + highlighting
echo -n -e '\e[?16;0;112c'#00001000 0 01110000 cyan      + highlighting
echo -n -e '\e[?16;0;240c'#00001000 0 11110000 dim white + highlighting
2
  • 1
    I love your answer. This is the most comprehensive answer I've seen on the Internet. But based on this answer I have only 3 bits for colours, but in my ubuntu i can set up any RGB colour I like, how is that? This is the method I found for this: echo -ne '\e]12;#'$1'\a' where $1 is a hex colour of 6 characters. I don't know how this works though
    – godzsa
    Commented Aug 23, 2016 at 20:06
  • 1
    @godzsa, this reply is about the linux terminal, and the escape code you're talking about works on xterm/vte/etc.
    – Chipaca
    Commented Feb 25, 2018 at 0:10
2

You can change the color of cursor if you change the color of the printed text. So you can't simple change the color of the cursor not changing the color of the text you are printing.

But you can make cursor invisible:

system("tput cinvis"); # make cursor invisible
system("tput cnorm");  # make cursor visible
2
  • Why are they related? Actually I have a green cursor and I print white text, on some other part of my program I print red or green text and the cursor is still green. However, thank you for your shell solution, but there's a possible typo: tput civis works on my system, while cinvis (as you said) complains about unknown terminfo capabilities.
    – Zagorax
    Commented Jul 15, 2012 at 18:15
  • It depends on the terminal (none of this answer is true of all terminals). Commented Aug 12, 2016 at 22:27
1

Have you tried something like this?

use Term::ANSIColor qw(:constants);

print RED, "Danger, Will Robinson!\n", RESET;
1
  • This will print it in red. But will not affect the color of the current cursor indicator. I've tried it.
    – Zagorax
    Commented Jul 15, 2012 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.