4

When using less in OS X, how can I customize the colors with with things are displayed? When I run less within an ansi-term in Emacs the colors don't come out nicely.

$ less --version
less 418
Copyright (C) 1984-2007 Mark Nudelman
1

For OSX, this refers to Emacs 22.1.1; Fedora with Emacs 24 has the same behavior.

Emac's ansi-term supports 8 colors. That is all that ANSI specified, and "ansi-term" is named appropriately. The eterm-color terminal description in ncurses is used for this terminal type. Emacs sets TERM to this value (see source):

(defvar term-term-name "eterm-color"
  "Name to use for TERM.
Using \"emacs\" loses, because bash disables editing if $TERM == emacs.")

If you override TERM, e.g., to tell applications that it has more colors, then the extra colors will not be used. Likewise, hard-coded applications which ignore the terminal database cannot use extra colors either.

While Emacs can use more than 8 colors in a terminal, ansi-term does not. Reading the source, look for term-ansi-current-color, and see that there is only logic for 30-37, 39, 40-47 and 49:

   ;; Foreground
   ((and (>= parameter 30) (<= parameter 37))
    (setq term-ansi-current-color (- parameter 29)))

   ;; Reset foreground
   ((eq parameter 39)
    (setq term-ansi-current-color 0))

   ;; Background
   ((and (>= parameter 40) (<= parameter 47))
    (setq term-ansi-current-bg-color (- parameter 39)))

   ;; Reset background
   ((eq parameter 49)
    (setq term-ansi-current-bg-color 0))

You may be able to change the palette of colors used by your terminal, but there is no way to make ansi-term differ from that (it will use the first 8 colors even if Emacs can use 256).

Further reading:

For Emacs other than ansi-term:

Also:

  • Fantastic answer. Thanks! Coincidentally I asked this other question today in Emacs.SE before reading this answer you wrote. What you have here seems appropriate for that other Q as well! – Amelio Vazquez-Reina Jul 23 '16 at 19:22
0

I believe it's partially a function of your terminal type, for one.

$ echo $TERM
xterm-256color

This post might also be worth a look, titled: Piping Ls Through Less With Colors on Mac OS X. Also this setting might prove helpful.

  $ export LESS="-erX" 

  -or-

  $ export LESS="-eRX" 

You can consult the man page for less to find out more about the various switches above.

  • -R may be preferable over -r in this case. – Stéphane Chazelas Feb 6 '14 at 15:08
  • Thanks slm and @StephaneChazelas - How can I change those colors then? Sometimes, I run less within a shell within Emacs (where echo $TERM returns eterm-color). Within a regular terminal, I get xterm-256colorand within tmux I get screen. – Amelio Vazquez-Reina Feb 8 '14 at 20:20
  • 1
    @user815423426 - some of the coloring (ls output) is controlled through these files: /etc/DIR_COLORS*. I do not believe there is any central resource where you can control the colors, rather you have to control them per application such as emacs, vim, etc. – slm Feb 8 '14 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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