可以在终端中突出显示制表符吗?

发布于 2024-08-09 20:18:54 字数 390 浏览 2 评论 0原文

使用终端序列,我可以在终端模拟器中为文本着色:

$ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒浓于脸红 2024-08-16 20:18:54

如果您不需要选项卡是选项卡,您可以通过 Expand(1) 预处理文本。

If you don't need the tabs to be tabs, you could preprocess your text through expand(1).

那片花海 2024-08-16 20:18:54

我找到了答案:不,选项卡无法在符合标准的终端仿真器中突出显示。 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.

你怎么这么可爱啊 2024-08-16 20:18:54

因为我很难找到一个好的解决方案(并且此页面是第一个搜索结果)...这是我在 .bashrc 文件中的内容(来自 http://pastebin.com/Pn1fkkJq 进行修改):

catt() { # Highlight whitespace on the terminal -- rolfwr
    local C=`printf '\033[0;36m'` R=`printf '\033[0m'`
    #cat "$@" | sed -e "s/      /${C}▹▹▹▹▹▹▹▹$R/g" -e "s/ /${C}·$R/g" -e "s/$/${C}⁋$R/"
    #cat "$@" | sed -e "s/ /${C}·$R/g" -e "s/\t/${C} ▹▹ $R/g" -e "s/$/${C}⁋$R/"
    cat "$@" | sed -e "s/ /${C}·$R/g" | expand | sed -e "s/ \( *\)/${C}▹\1$R/g" -e "s/$/${C}⁋$R/"
    #cat "$@" | sed -e "s/ /${C}.$R/g" | expand | sed -e "s/ \( *\)/${C}>\1$R/g" -e "s/$/${C}P$R/"
}

希望这对其他人也有帮助。

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):

catt() { # Highlight whitespace on the terminal -- rolfwr
    local C=`printf '\033[0;36m'` R=`printf '\033[0m'`
    #cat "$@" | sed -e "s/      /${C}▹▹▹▹▹▹▹▹$R/g" -e "s/ /${C}·$R/g" -e "s/$/${C}⁋$R/"
    #cat "$@" | sed -e "s/ /${C}·$R/g" -e "s/\t/${C} ▹▹ $R/g" -e "s/$/${C}⁋$R/"
    cat "$@" | sed -e "s/ /${C}·$R/g" | expand | sed -e "s/ \( *\)/${C}▹\1$R/g" -e "s/$/${C}⁋$R/"
    #cat "$@" | sed -e "s/ /${C}.$R/g" | expand | sed -e "s/ \( *\)/${C}>\1$R/g" -e "s/$/${C}P$R/"
}

Hope this will help someone else also.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文