开发者

Can one highlight tab characters in the terminal?

开发者 https://www.devze.com 2022-12-09 06:12 出处:网络
Using terminal sequences, I can colour t开发者_如何学JAVAext in a terminal emulator: $ echo -e \'\\e[37;41m--this is white on red--\\e[0m\'

Using terminal sequences, I can colour t开发者_如何学JAVAext 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?


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消