3

I followed https://unix.stackexchange.com/a/94508/674 to change the colors used by ls. This works in bash.

When I open a buffer of "Shell" mode in Emacs (M-x shell), the change doesn't work. I wonder how I can apply the same change to Emacs "Shell" mode?

0

2 Answers 2

5

Some versions of ls need to know from the terminfo database what color codes are supported by the terminal.

First, create the following file (dumb-emacs-ansi.ti):

dumb-emacs-ansi|Emacs dumb terminal with ANSI color codes,
    am,
    colors#8, it#8, ncv#13, pairs#64,
    bold=\E[1m, cud1=^J, ht=^I, ind=^J, op=\E[39;49m,
    ritm=\E[23m, rmul=\E[24m, setab=\E[4%p1%dm,
    setaf=\E[3%p1%dm, sgr0=\E[m, sitm=\E[3m, smul=\E[4m, 

This is a Terminfo entry I created with support for Emacs' own brand of ANSI codes (supports only 8 colors, has bold, italic and underline as independent attributes). Compile it with tic dumb-emacs-ansi.ti.

Then add to your .bashrc:

if [ "$TERM" = dumb ] && [ "$INSIDE_EMACS" ]; then
    export TERM=dumb-emacs-ansi COLORTERM=1
fi

You can just run the export line in your existing shell session to update it without restarting bash.

5
  • Thanks. What are tic and .ti files?
    – Tim
    Oct 23, 2015 at 2:35
  • @Tim the .ti file is the source code for the terminfo database entry, tic compiles and installs it in ~/.terminfo. They're much more extensive for full-featured terminals, run infocmp xterm for an example.
    – Random832
    Oct 23, 2015 at 2:40
  • What are they much more extensive for full-featured terminals than?
    – Tim
    Oct 23, 2015 at 2:42
  • @Tim Well, on most terminals the application running in it can move the cursor, draw text anywhere, interpret the arrow keys, etc, and terminfo tells it how to do that. In Emacs shell mode, all it can do is type text, everything else is controlled by Emacs.
    – Random832
    Oct 23, 2015 at 2:50
  • I also had to to this in order for ls to emit colors IFF the standard output is tied to a terminal: alias ls="ls --color=auto". I found that git log would emit colors in the terminal, but not ls until I aliased it.
    – bgoodr
    Jan 28, 2020 at 19:32
2

Emacs shell mode is a dumb terminal that just exchanges strings with the shell; it doesn't allow terminal escape sequences like those which set colors. If you want a full terminal within emacs, use M-x term. (After doing this, you can quit out of it with C-c k; term grabs all the normal keys, since it is a full terminal emulator.)

EDIT: The suggestion to use term stands, but it seems that shell-mode does indeed allow colors, and ls somehow refuses to recognize this fact.

7
  • Actually... color setting sequences are just about the only ones it allows, but it does allow them. The trick is getting ls to recognize this fact.
    – Random832
    Oct 22, 2015 at 15:44
  • I just attempted ls --color=always within shell-mode. Colors nonetheless failed to appear.
    – Tom Hunt
    Oct 22, 2015 at 15:45
  • What version of Emacs are you on? It works for me. What does printf '\e[31mtest\e[m\n' do?
    – Random832
    Oct 22, 2015 at 15:46
  • Version is 24.5.1. test is output in what appears to be the correct color (some variant on yellow). Maybe ls is not getting the option somehow?
    – Tom Hunt
    Oct 22, 2015 at 15:48
  • Hmm, yes, and ls --color=always | xxd shows no escape sequences, while in a real terminal emulator it has them.
    – Tom Hunt
    Oct 22, 2015 at 15:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .