In bash scripts we can use tput
to set terminal-independent less variables:
export LESS_TERMCAP_me=$(tput sgr0; )
Can we do the same thing with lesskey
?
Yes you can put variable LESS_TERMCAP_me
in the lesskey
file but it requires fixing the source code and building your own less
version (I explain that below).
No the lesskey
file cannot contain external commands as tput
to become terminal-independent. However, you may use several lesskey
files as recommended by the Thomas Dickey's answer.
lesskey
fileThe command lesskey
is used to convert a human-readable text content into a binary configuration file for command less
. This configuration file is named lesskey
file and is usually the file ~/.less
.
This files contains three sections:
:q quit
)^G abort
)less
environment variables (e.g. LESS_TERMCAP_me = \033(B\033[m
)LESS_TERMCAP_*
in the lesskey
file$ cat > lesskey.txt << EOF
#env
LESS = -iSrsM +Gg
LESS_TERMCAP_md = $(tput bold; tput setaf 6)
LESS_TERMCAP_me = $(tput sgr0)
LESS_TERMCAP_so = $(tput bold; tput setaf 3; tput setab 4)
LESS_TERMCAP_se = $(tput rmso; tput sgr0)
LESS_TERMCAP_us = $(tput smul; tput bold; tput setaf 7)
LESS_TERMCAP_ue = $(tput rmul; tput sgr0)
LESS_TERMCAP_mr = $(tput rev)
LESS_TERMCAP_mh = $(tput dim)
EOF
$ lesskey lesskey.txt # By default lesskey writes ~/.less
$ man bash # By default man uses less (pager)
The command less
reads the configuration ~/.less
too late:
just after searching for variables LESS_TERMCAP_*
.
This bug affects version 487 released on 29 Mar. 2017 (and surely previous versions).
I have just sent a bug report and the corresponding fix to maintainers. I will update this answer when the fix will be applied for the next less
release...
Download source code archive from:
Inverse two lines in file main.c
at line 113
Before:
/*
* Process command line arguments and LESS environment arguments.
* Command line arguments override environment arguments.
*/
is_tty = isatty(1);
get_term();
init_cmds();
init_charset();
init_line();
After:
/*
* Process command line arguments and LESS environment arguments.
* Command line arguments override environment arguments.
*/
is_tty = isatty(1);
init_cmds(); /* Load lesskey file before */
get_term(); /* getting variables LESS_TERMCAP_* */
init_charset();
init_line();
Install dependencies required for the build
./configure
If ./configure
fails, identify the missing dependencies.
In my case I have installed ncurses-devel
to get header term.h
.
Build
make
Keep your own-compiled less
somewhere, for example to a personal directory ~/bin
mkdir -p ~/bin
cp ./less ~/bin
PATH="$PATH:~/bin"
Do not forget to set the environment variable PATH
in your ~/.profile
or if you prefer any other file as ~/.bashrc
, ~/.login
...
See the TERMCAPS
used in a man-page
LESS_TERMCAP_DEBUG=1 man bash
Retrieve the man-page with TERMCAPS
encoding
MANPAGER='tee man.txt' man bash
Combine these two tips
MANPAGER='tee man.txt' man bash
LESS_TERMCAP_DEBUG=1 less man.txt
You can make less
read a different lesskey
file for each terminal type, e.g., by setting the LESSKEY
(or LESSKEY_SYSTEM
) environment in your shell initialization. The simple way of doing that would be to use the TERM
variable as part of the filename, and making your shell check for the existence of these terminal-dependent files before setting the LESSKEY
variable.
lesskey
is for function-keys. This answer is relevant only for adjusting video-highlights. – Thomas Dickey Jul 08 '17 at 19:23lesskey
is used to store commands and variables in a configuration file for commandless
. I am pretty sure the OP requests how to put the environment variablesLESS_TERMCAP_*
within thelesskey
file. Please read the section Put your variablesLESS_TERMCAP_*
in the lesskey file within my answer to understand what I mean. Please advice me what you suggest I can do to improve my answer. Cheers – oHo Jul 08 '17 at 19:30less
. Thelesskey
file has three sections: Command (e.g.:q quit
), Line editing (e.g.^G abort
) and theless
environment variables (e.g.LESSCHARSET = latin1
). Do I miss-understand something? Cheers – oHo Jul 08 '17 at 19:40lesskey
file. And your answer explain how to manage multiplelesskey
files depending on the targeted terminal. Therefore I have updated my answer and created a link to your answer. Am I correct? Cheers – oHo Jul 08 '17 at 20:08