可以在终端中突出显示制表符吗?
使用终端序列,我可以在终端模拟器中为文本着色:
$ echo -e '\e[37;41m--this is white on red--\e[0m'
这有效。
以下代码打印一个红色矩形,即空格字符具有红色背景和空前景:
$ echo -e '\e[37;41m \e[0m '
但以下代码打印两个由制表符分隔的红色矩形:
$ echo -e '\e[37;41m \t \e[0m '
也就是说,制表符不会收到红色背景突出显示。它采用默认的终端背景颜色。
为什么制表符不像空格那样突出显示?我可以用其他方式强制这个吗?
Using terminal sequences, I can colour text in a terminal emulator:
$ echo -e '\e[37;41m--this is white on red--\e[0m'
And this works.
The following prints a red rectangle, i.e. space characters have a red background and an empty foreground:
$ echo -e '\e[37;41m \e[0m '
But the following prints two red rectangles separated by a tab:
$ echo -e '\e[37;41m \t \e[0m '
That is, the tab character does not receive the red background highlighting. It takes the default terminal background colour.
Why isn't tab highlighted like space? Can I force this some other way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不需要选项卡是选项卡,您可以通过 Expand(1) 预处理文本。
If you don't need the tabs to be tabs, you could preprocess your text through expand(1).
我找到了答案:不,选项卡无法在符合标准的终端仿真器中突出显示。 Tab 并不是终端中真正的空白;它明确是一个光标移动字符,用于前进到下一个制表位。因此,实际上我的示例是“在红色背景上打印两个空格,前进到下一个制表位,然后在红色背景上再打印两个空格”。
解决方法是 ankon 建议在打印之前将制表符转换为空格。
I've found the answer: no, tab cannot be highlighted in a standards compliant terminal emulator. Tab is not really whitespace in the terminal; it is explicitly a cursor movement character to go forward to the next tab stop. So, in effect my example says "print two spaces on red background, move forward to the next tab stop, then print two more spaces on red background".
The workaround would be ankon's suggestion to convert tabs to spaces before printing.
因为我很难找到一个好的解决方案(并且此页面是第一个搜索结果)...这是我在 .bashrc 文件中的内容(来自 http://pastebin.com/Pn1fkkJq 进行修改):
希望这对其他人也有帮助。
Because it was hard for me to find a nice solution (and this page was first search result)... This is what I have in .bashrc file (from http://pastebin.com/Pn1fkkJq with modifications):
Hope this will help someone else also.