Changing colors used by `ls` does not work in Emacs “Shell” mode

colorsemacsls

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?

Best Answer

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.

Related Question