Emacs inside the terminal changes the cursor color dynamically

I am trying to change the cursor color inside Emacs to a different color dynamically.

I am using emacs -nw for an Emacs terminal. The terminal I'm using is Rxvt-Unicode (Urxvt).

Since Emacs cannot (I think) make changes to the terminal, (set-cursor-color "red") will not work. To change the color of the terminal cursor, I can run echo -ne '\033]12;red\007' . This changes the color of the cursor to red.

What I tried to do in Emacs was to run this command inside it. Mx shell-command RET echo -ne '\033]12;red\007' RET

However, Emacs will exit the echo line and print it at the bottom of the window, and will not actually make any changes to the cursor.

I really don't know what else I can do from here, I don't want to use the Emacs GUI. Does anyone know about this? I want to change the color of my cursor depending on my current mode, so it should be dynamic. If anyone has any other suggestions, I'd love to hear them!

+8
terminal emacs elisp
source share
2 answers

I think you want to use send-string-to-terminal .

eg. (send-string-to-terminal "\033]12;red\007") .

+5
source share

This is a very hacky solution - I hope someone finds the best:

 (suspend-emacs "echo -ne '\\033]12;red\\007'; fg\n") 

It works by temporarily pausing the emacs process and the blending commands in the base terminal to force the shell to change the cursor color and resume emacs after that. However, this causes the screen to flicker and the emacs frame to disappear temporarily.


Here is another very hacky and system-dependent solution:

 (shell-command (format "echo -ne '\\033]12;red\\007' > /proc/%d/fd/1" (emacs-pid))) 

It works (at least on Linux) by directly sending the ANSI escape sequence to the terminal (which is accessed via the /proc/PID pseudo file system). I do not think this solution is more elegant than the previous one, but at least it does not make the screen flicker.

+2
source share

All Articles