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?

Tim
  • 101,790

2 Answers2

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.

Random832
  • 10,666
  • Thanks. What are tic and .ti files? – Tim Oct 23 '15 at 02: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 '15 at 02:40
  • What are they much more extensive for full-featured terminals than? – Tim Oct 23 '15 at 02: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 '15 at 02: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 '20 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.

Tom Hunt
  • 10,056