bash 如何在bash shell中设置光标颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37557309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set cursor color in bash shell
提问by rbaumann
I have found that I can get my cursor to blink by including the following instruction in my .bashrc file:
我发现我可以通过在我的 .bashrc 文件中包含以下指令来让我的光标闪烁:
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.
但我确实在我的终端模拟器软件中找到了一种解决方法,如下所示。感谢您的反馈,特别是关于选择合适的转义码跨终端类型的部分。
采纳答案by rbaumann
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.
后来我发现我可以更改光标颜色,不是在 bash 中,而是在终端模拟器程序中。就我而言,该程序是 MobaXTerm。我发现了以下顺序:设置 - 终端 - 光标。此时,选择光标颜色会使 bash shell 中的光标以所需颜色显示。所以现在在我在 xterm 窗口的 bash 环境中使用 vim 编辑的文件中,我看到一个闪烁的绿色块光标,这正是我所需要的。
Please check the following short demonstration clip: Blinking Green Block Cursor in bash and vim
请查看以下简短的演示剪辑: 在 bash 和 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.
实现这就是我的问题,而不是关于如何在屏幕上显示彩色文本,正如所指出的,已经在别处回答了。所以我的问题不是重复的。无论如何,事实证明我的 xterm 仿真软件 Mobaxterm 允许我设置光标颜色,而我的 .bashrc 文件中的转义序列允许我让它闪烁。
回答by mrmemio29
Don't use echo escape characters. Use printf like so:
不要使用 echo 转义字符。像这样使用 printf:
printf '%b' '\e]12;red\a'