2

I have the following LESS_TERMCAP settings:

# less config
export LESS=-R         
export LESS_TERMCAP_mb=$'\E[01;31m' # begin blinking
export LESS_TERMCAP_md=$'\E[01;31m' # begin bold    
export LESS_TERMCAP_me=$'\E[0m' # end mode          
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_so=$'\E[01;44;37m' # begin standout-mode
export LESS_TERMCAP_ue=$'\E[0m' # end underline
export LESS_TERMCAP_us=$'\E[01;33m' # begin underline
export LESSOPEN='| /usr/bin/highlight -O ansi %s 2>/dev/null'

When I put into ~/.profile, the less viewer gets absolutely messy. On the other hand, when I put into ~/.zshrc (I am using zsh), it works, why?

Since I was taught to keep the environment variables into non-login shell config files, such as ~/.profile, I want to do so.

Further information:

 tapyu@tapyu-ThinkPad-P73
 OS: Ubuntu 20.04 focal
 Kernel: x86_64 Linux 5.13.0-28-generic
 Uptime: 2h 25m
 Packages: 2559
 Shell: zsh 5.8
 Resolution: 2048x1152
 DE: GNOME 3.36.5
 WM: Mutter
 WM Theme: Adwaita
 GTK Theme: Yaru-dark [GTK2/3]
 Icon Theme: Yaru
 Font: Ubuntu 11
 Disk: 123G / 217G (60%)
 CPU: Intel Core i7-9750H @ 12x 4.5GHz [43.0°C]
 GPU: Intel Corporation UHD Graphics 630 (Mobile)
NVIDIA Corporation GP107GLM [Quadro P620] (rev a1)
 RAM: 4740MiB / 23681MiB

Best regards.

1 Answers1

1

Check the value of the LESS_TERMCAP_xxx variables:

print -lr ${(q)LESS_TERMCAP_mb}

(Or, if not using zsh: echo "$LESS_TERMCAP_mb" | od -tx1c)

Chances are that, as Thomas Dickey guessed, you'll see \$\\E\[01\;31m rather than $'\033'\[01\;31m ($ \ E [… rather than 033 […). This happens if .profile is invoked by a shell such as dash which doesn't understand the $'…' syntax. /bin/sh is dash on many popular distributions including Ubuntu.

One solution is to use another way of representing the escape character. You can use printf to generate it portably:

esc=$(printf \\033)
export LESS_TERMCAP_mb="${esc}[01;31m" # begin blinking
…

Alternatively, define these variables in your .zshrc. This has the advantage of letting you customize their content based on the terminal's capabilities. And then you may want to call tput instead of hard-coding escape sequences. (Documentation on LESS_TERMCAP_* variables? may help.)

Alternatively, define these variables using lesskey, which writes them into less's configuration file (~/.less). This makes more sense than putting them in the environment if you always set them to the same value, since they're a configuration of less specifically. However, this doesn't work in older versions of less.

  • I am using Ubuntu 2. /bin/sh is a symbolic link to dash, indeed 3. the command print -lr ${(q)LESS_TERMCAP_mb} prints $'\033'\[01\;31m, which contradicts your statement
  • – Rubem Pacelli Jan 03 '22 at 03:27
  • Honestly, I didn't see any advantage of using tput as it has weird-looking commands, just as the escaped command does... – Rubem Pacelli Jan 03 '22 at 03:29
  • your alternative to use esc=$(printf \\033) didn't work. It didn't make the less messy, but it didn't colorize properly either... – Rubem Pacelli Jan 03 '22 at 04:35