13

I have an open socket to a remote terminal. Using the answer to "Force telnet client into character mode" I was able to put that terminal into character mode.

My question is, how do I hide the cursor in the remote terminal using this method?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Tyler
  • 4,679
  • 12
  • 41
  • 60

4 Answers4

18

To expand upon mjh2007's answer, the following c/c++ code will implement sending the escape codes to the terminal, and is slightly more readable than raw hex numbers.

void showCursor(bool show) const {
#define CSI "\e["
  if (show) {
    fputs(CSI "?25h", stdout);
  }
  else {
    fputs(CSI "?25l", stdout);
  }
#undef CSI
}
joesdiner
  • 1,125
  • 9
  • 11
  • 2
    Do not hard-code the terminal control codes unless you have no other choice. Use ncurses to look up the appropriate codes for the current `$TERM` value unless you cannot use ncurses for some reason. – Chris Page Mar 10 '12 at 21:53
  • 11
    I agree that ncursrs would be best, but if for some reason you don't have access to that, this might be a "good enough" solution for some cases. – joesdiner Mar 11 '12 at 01:51
  • 1
    Powershell seems to respect this sequence if it's preceded by a SGR escape sequence or a cursor move sequence, but not if it's preceded by some other sequences including another instance of `?25`! `\e[0m\e[?25h` seems to work fine. – John Dvorak Feb 08 '20 at 17:54
  • 3
    As for the escape codes: `\e[?25h` and `\e[?25l`. To memorize it, remember that L stands for _Lehidden_ and H stands for _HonestlyTheCursorWillBeVisible_. – Samie Bencherif Jun 14 '22 at 01:20
  • 2
    Also, no offense or anything, but using ncurses for this is like downloading a web-browser to read a text file :) @ChrisPage – Samie Bencherif Jun 14 '22 at 01:22
13

This is something that the ncurses library can do for you.

The curs_set() function can make the cursor invisible.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
6

If the terminal you are using supports ANSI format you should be able to send the following escape codes:

Hide the cursor: 0x9B 0x3F 0x32 0x35 0x6C
Show the cursor: 0x9B 0x3F 0x32 0x35 0x68
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
mjh2007
  • 776
  • 1
  • 8
  • 17
  • I know this is a few years old but where did you get that command? Is there a link with other escape codes in? – Cybrix Aug 23 '11 at 23:04
  • 3
    Do not hard-code the terminal control codes unless you have no other choice. Use ncurses to look up the appropriate codes for the current `$TERM` value unless you cannot use ncurses for some reason. – Chris Page Mar 10 '12 at 21:54
  • 11
    Like my code is running on a PIC microprocessor w/ 8KB of ROM? – mjh2007 May 17 '12 at 03:15
3

If this is using the 'telnet' application then your app should send 'IAC WILL ECHO' to disable echoing on their remote side. This is useful for entering passwords or if your app is doing the echoing.

#define TEL_IAC "\377"
#define TEL_WILL "\373"
#define TEL_ECHO "\001"

char buf[4];
snprintf(buf, sizeof(buf), "%c%c%c" TEL_IAC, TEL_WILL, TEL_ECHO);
write(sock, buf, sizeof(buf));

Or

write(sock, TEL_IAC TEL_WILL TEL_ECHO, 3);

Hope this helps.

Bryan Drewery
  • 2,569
  • 19
  • 13