Googled a lot and found curs_set()
or putp(tigetstr() )
can be used to hide/show the tty cursor. I'd like to minimize the dependency on other libs so I'm wondering if I can do this without using libtinfo
/libncurses
or calling external commands (like setterm
, tput
). Is there any ioctl()
command for this?

- 19,215
- 5
- 38
- 56
-
Related: https://stackoverflow.com/q/2649733/2371524 -- it's a feature of the terminal and although there are **some** standards for terminal escape sequences, there are a lot of variations. If you want your program to work reliably, just use a library. – May 03 '18 at 08:44
-
Possible duplicate of [Hide cursor on remote terminal](https://stackoverflow.com/questions/2649733/hide-cursor-on-remote-terminal) – Thomas Dickey May 03 '18 at 08:57
-
@ThomasDickey - it's the same Q as Felix Palmen mentioned in the comment. i'm trying to minimize dependency on other libs. – pynexj May 03 '18 at 09:08
-
printf("\033[?25l"); //invisible printf("\033[?25h"); //visible works in my terminal – purec May 03 '18 at 09:30
1 Answers
There are basically three approaches:
If your program is executed only on the console terminal (for example, because the machine is not network-connected), or by most common terminals only, you can assume UTF-8 character set and ANSI escape codes.
This means that to hide the cursor, you print
"\033[?25l"
to the terminal, and"\033[?25h"
to show the cursor.
For proper terminal support, you use the terminfo library.
You could read the terminfo database directly, but that would be pretty pointless, because if it is installed, you also have the curses functions used to access it (
tgetent()
/tgetnum()
/tgetflag()
).You should then also have your program be locale-aware, and use e.g.
iconv()
to convert between character sets, rather than assume UTF-8.
Use curses or ncursesw (with wide character support).

- 19,215
- 5
- 38
- 56

- 38,216
- 5
- 59
- 86
-
by mentioning UTF8 do you mean the ANSI escape sequence may not work for other locales? – pynexj May 03 '18 at 10:16
-
1@pynexj : no, the two are basically unrelated. The stream of characters to the terminal is composed of two components: items to control the terminal (such as change/move the cursor) and the actual "payload" characters to be displayed. The control sequences *mostly* start with an ESC, and *often* end in an `';'` The "payload" characters are just displayed as whatever the terminal thinks they should look like. (this *could* be utf8, and/or some encoding) – joop May 03 '18 at 11:20
-
@pynexj: The escape sequences are always the same, regardless of locale or character set used. It is just that if you care about what terminal the user is using, you should also consider that they may use a different locale and possibly a character set other than UTF-8 for the visible contents. Doing otherwise is like painting a surface without cleaning it first: the overall results tend to be unsatisfactory. – Nominal Animal May 03 '18 at 12:39
-
The escape codes are flipped. To hide cursor should be: `\033[?25l`. And to show: `\033[?25h`. – JohanPI Nov 13 '20 at 13:11
-
the `"h"` and `"l"` stand for high and low (cursor visibility) – Christoph Rackwitz Mar 28 '23 at 18:59