less doesn't highlight search

The highlighting in TTY1 works since it sets the TERM variable to a proper value.

If you're using screen:

Change the TERM variable to a proper value (e.g., screen or screen-256color). Check your personal ~/.screenrc or the system-wide /etc/screenrc and fix the corresponding line.

In my version of less, the value screen-256 should actually results in an error:

WARNING: terminal is not fully functional
-  (press RETURN)

If you're not using screen:

The screen* only get interpreted correctly by screen.

The correct value for TERM depends on your terminal emulator and should usually get set by it. The default of Ubuntu's three pre-installed terminal emulators is xterm.

Execute

grep -R TERM= ~/.* /etc 2> /dev/null

the check if TERM's value gets overridden in your shell's configuration files.


Well this is annoying. What is going on here? (in tmux under iTerm.app)

$ echo test > test ; echo $TERM
screen

and then with some recording

$ script withscreen
Script started, output file is withscreen
$ less -p test test
... q to quit and then exit the shell session ...

$ script withxterm
Script started, output file is withxterm
$ TERM=xterm less -p test test
... q and exit again ...

and now we look at the codes used

$ grep test withscreen | hexdump -C
00000000  24 20 6c 65 73 73 20 2d  70 20 74 65 73 74 20 74  |$ less -p test t|
00000010  65 73 74 0d 0d 0a 1b 5b  33 6d 74 65 73 74 1b 5b  |est....[3mtest.[|
00000020  32 33 6d 0d 0a 1b 5b 35  3b 31 48 1b 5b 33 6d 74  |23m...[5;1H.[3mt|
00000030  65 73 74 1b 5b 32 33 6d  0d 0a 1b 5b 33 38 3b 31  |est.[23m...[38;1|
00000040  48 1b 5b 33 6d 74 65 73  74 20 28 45 4e 44 29 1b  |H.[3mtest (END).|
00000050  5b 32 33 6d 1b 5b 4b 0d  1b 5b 4b 1b 5b 3f 31 6c  |[23m.[K..[K.[?1l|
00000060  1b 3e 24 20 5e 44 0d 0d  0a                       |.>$ ^D...|
00000069
$ grep test withxterm | hexdump -C
00000000  24 20 54 45 52 4d 3d 78  74 65 72 6d 20 6c 65 73  |$ TERM=xterm les|
00000010  73 20 2d 70 20 74 65 73  74 20 74 65 73 74 0d 0d  |s -p test test..|
00000020  0a 1b 5b 37 6d 74 65 73  74 1b 5b 32 37 6d 0d 0a  |..[7mtest.[27m..|
00000030  1b 5b 35 3b 31 48 1b 5b  37 6d 74 65 73 74 1b 5b  |.[5;1H.[7mtest.[|
00000040  32 37 6d 0d 0a 1b 5b 33  38 3b 31 48 1b 5b 37 6d  |27m...[38;1H.[7m|
00000050  74 65 73 74 20 28 45 4e  44 29 1b 5b 32 37 6d 1b  |test (END).[27m.|
00000060  5b 4b 0d 1b 5b 4b 1b 5b  3f 31 6c 1b 3e 24 20 65  |[K..[K.[?1l.>$ e|
00000070  78 69 74 0d 0d 0a                                 |xit...|
00000076
$ 

the 1b 5b ... codes may be rendered more intelligible by consulting the xterm control sequences documentation or one can fiddle around manually with the sequences to see which under TERM=xterm is causing the highlighting

$ printf "\033[7mtest\033[27m\n"
test

which the TERM=screen case does not do, per the control sequences docs that's an inverse

ESC [
     Control Sequence Introducer (CSI  is 0x9b).
...
CSI Pm m  Character Attributes (SGR).
...
            Ps = 7  -> Inverse.
...
            Ps = 2 7  -> Positive (not inverse).

and nearby from that document we might learn that the screen terminal \033[3m is for Italicized and \033[23m Not italicized.

This finding gives some options; we might configure the terminal to display italicized text, or we could instead try to make the screen terminal use the inverse codes instead of italics. (Some digging around in the less(1) docs did not show any clear "use inverse instead of italic" knobs to fiddle with.) (Also, some terminals may offer support for translating X to Y, check terminal docs for details.) (Or you could try a different terminal emulator and see what that one does...)

Wow italicized text is ugly. Let's try instead changing the codes screen uses to inverse. This obviously involves the terminfo (or possibly termcap) database, which can be exported via infocmp(1) and compiled by tic(1)

$ TERM=screen infocmp > ti.screen ; TERM=xterm infocmp > ti.xterm
$ fgrep '\E[7' ti.xterm
        rc=\E8, rep=%p1%c\E[%p2%{1}%-%db, rev=\E[7m, ri=\EM,
        smir=\E[4h, smkx=\E[?1h\E=, smm=\E[?1034h, smso=\E[7m,
$ fgrep rev= ti.screen
        nel=\EE, op=\E[39;49m, rc=\E8, rev=\E[7m, ri=\EM, rmacs=^O,
$ fgrep '\E[3m' ti.screen
        smso=\E[3m, smul=\E[4m, tbc=\E[3g,
$ 

So I'd guess the smso is being used given that xterm uses \E[7m and screen \E[3m; according to terminfo(5) this is "standout mode" and is paired with the reverse rmso; let's change those to what xterm uses...

$ TERM=screen infocmp | sed -e 's/smso=[^,]*/smso=\\E[7m/;s/rmso=[^,]*/rmso=\\E[27m/' > foo
$ tic -o ~/.terminfo foo
$ rm foo

Hey that looks better now (but will have to be done on all hosts for the screen or whatever terminfo file...)

Tags:

Less