I'm trying to change the color of my cursor inside Emacs to another color dynamically.
I'm using emacs -nw
for the terminal Emacs. The terminal I'm using is Rxvt-Unicode (Urxvt).
Since Emacs can't (I think) make changes to the terminal, (set-cursor-color "red")
won't work. In order to change the cursor color of the terminal 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 is to run this command inside it.
M-x shell-command RET echo -ne '\033]12;red\007' RET
However, Emacs will escape the echoed string and print it at the bottom of the window, and 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 a way around this? I want to be able to change the color of my cursor depending on my current mode, which is why it needs to be dynamic. If anyone has any other suggestions I would be happy to hear them!
I think you want to use send-string-to-terminal
.
E.g. (send-string-to-terminal "\033]12;red\007")
.
This is a very hackish solution -- I hope someone will find a better one:
(suspend-emacs "echo -ne '\\033]12;red\\007'; fg\n")
It works by temporarily suspending the emacs process and stuffing commands into the underlying terminal to make the shell change the cursor color and resume emacs after that. However, this causes the screen to flicker while the emacs frame temporarily disappears.
Here is another very hackish 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 through the /proc/PID
pseudo-filesystem). I don't find this solution more elegant than the previous one, but at least it doesn't make the screen flicker.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With