4

I have found that I can get my cursor to blink by including the following instruction in my .bashrc file:

echo -ne "\x1b[1 q"

But I also want to change the color of the blinking cursor. I know that my terminal supports color because I can set the prompt colors and print text in color, but I just can't change the cursor color. Any suggestions?

I'm adding the following comment, that I'm aware of how to change the color of text that is displayed on the terminal, but that is not the same as changing the color of the the cursor. So my question is not addressed in that other question.

But I did find a workaround in my terminal emulator software, provided below. Thanks for the feedback, especially the part about making the selection of the proper escape codes portable across terminal types.

Community
  • 1
  • 1
rbaumann
  • 129
  • 1
  • 1
  • 7
  • Aside: `echo -ne` is bad form; consider `printf` instead. See the POSIX spec for `echo` at http://pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html -- `-n` is permitted but with behavior undefined, and any behavior for `-e` other than printing the literal text `-e` on output is outright disallowed. While bash's echo is noncompliant by default, it isn't *always* noncompliant; with the `xpg_echo` and `posix` options set, for instance, it becomes compliant, so places where it deviates shouldn't be relied on. – Charles Duffy May 31 '16 at 23:51
  • 1
    ...the bigger concern, though, is that escape codes are terminal-specific. You shouldn't be hardcoding them in your software or your configurations to start with -- the right way to get them is through code that looks up the correct value for your current TERM environment variable. – Charles Duffy May 31 '16 at 23:53
  • 1
    check this `echo -e "\e]12;red\a"` – Mohamed Slama May 31 '16 at 23:54
  • Likewise, whether your terminal supports changing the cursor color is *also* terminal-specific, and looking up the specific code would require knowing the actual value of `TERM`. – Charles Duffy May 31 '16 at 23:54
  • 1
    @MohamedSlama, `printf %b '\e]12;red\a'`, if you want that to be portable to different shells. (It's already not portable to different terminals, but such portability isn't possible without doing a termcap/terminfo lookup). – Charles Duffy May 31 '16 at 23:55
  • you can learn more from here http://linuxgazette.net/137/anonymous.html – Mohamed Slama Jun 01 '16 at 00:02

2 Answers2

3

I found afterwards that I can change the cursor color, not in bash, but in the terminal emulator program. In my case that program is MobaXTerm. I discovered the following sequence: Settings - Terminal - Cursor. At that point, selecting a cursor color causes the cursor in the bash shell to be displayed in the desired color. So now in the files that I edit using vim in the bash environment in my xterm window I see a blinking green block cursor, which is what I needed.

Please check the following short demonstration clip: Blinking Green Block Cursor in bash and vim

Attaining that was what my question was about, not about how to display colored text on the screen, which as was pointed out is already answered elsewhere. So my question is not a duplicate. Anyway, it turned out that my xterm emulation software Mobaxterm allowed me to set the cursor color whereas the escape sequence in my .bashrc file allowed me to get it to blink.

rbaumann
  • 129
  • 1
  • 1
  • 7
2

Don't use echo escape characters. Use printf like so:

printf '%b' '\e]12;red\a'
mrmemio29
  • 386
  • 1
  • 8