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?


Short answer

  • 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 file

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

  1. Command (e.g. :q quit)
  2. Line editing (e.g. ^G abort)
  3. less environment variables (e.g. LESS_TERMCAP_me = \033(B\033[m)

Put variables LESS_TERMCAP_* in the lesskey file

$ cat > lesskey.txt << EOF
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)

$ 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...

Fix it yourself

  1. Download source code archive from:

  2. Inverse two lines in file main.c at line 113

     * Process command line arguments and LESS environment arguments.
     * Command line arguments override environment arguments.
    is_tty = isatty(1);


     * 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_* */
  3. Install dependencies required for the build


    If ./configure fails, identify the missing dependencies.
    In my case I have installed ncurses-devel to get header term.h.

  4. Build

  5. Keep your own-compiled less somewhere, for example to a personal directory ~/bin

    mkdir -p ~/bin
    cp ./less ~/bin

    Do not forget to set the environment variable PATH in your ~/.profile or if you prefer any other file as ~/.bashrc, ~/.login...

Extra tips

  • 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
  • lesskey is for function-keys. This answer is relevant only for adjusting video-highlights. – Thomas Dickey Jul 8 '17 at 19:23
  • Hi @ThomasDickey. Thank you for checking new answers and for your feedback. The command line lesskey is used to store commands and variables in a configuration file for command less. I am pretty sure the OP requests how to put the environment variables LESS_TERMCAP_* within the lesskey file. Please read the section Put your variables LESS_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 – olibre Jul 8 '17 at 19:30
  • manual page says "lesskey - specify key bindings for less" – Thomas Dickey Jul 8 '17 at 19:32
  • Yes @ThomasDickey, you are right, but it is also for the environment variables used by less. The lesskey file has three sections: Command (e.g. :q quit), Line editing (e.g. ^G abort) and the less environment variables (e.g. LESSCHARSET = latin1). Do I miss-understand something? Cheers – olibre Jul 8 '17 at 19:40
  • Hi @ThomasDickey. I think I understand what you mean: the OP has requested a solution to have a terminal-independent lesskey file. And your answer explain how to manage multiple lesskey files depending on the targeted terminal. Therefore I have updated my answer and created a link to your answer. Am I correct? Cheers – olibre Jul 8 '17 at 20:08

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.