http://www.grokblok.com/articles/2007/12/13/emacs-shell-ansi-colors/
The problem is that I added those few lines to my .emacs file, but
nothing changed.
I had a look also at:
http://www.emacswiki.org/cgi-bin/wiki/ansi-color.el
but I did not copy the ansi-color.el file anywhere in my system (in case
it matters, I am running emacs22 on a Debian testing box).
I notice that issuing the command (within emacs shell):
ls --color=yes
give the nice colorized output I am looking for, hence I do not think
there is any substantial problem by default, just it seems that my
output is not colorized by default (though when I type M-x shell I read
the message
Loading ansi-color...done ).
Can anyone help me out with this?
Many thanks
Lorenzo
Lorenzo Isella <lorenzo...@gmail.com> writes:
Try that in your .emacs:
,----
| (autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
| (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
`----
--
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France
Hi,
And thanks for your suggestions. I am puzzled since I gave both a try
(actually one of them is precisely the same recommendation from the
wiki page), but the problem stays the same.
From the web
http://www.emacswiki.org/cgi-bin/wiki/ansi-color.el
I read
;;; Commentary:
;; This file provides a function that takes a string or a region
;; containing Select Graphic Rendition (SGR) control sequences (formerly
;; known as ANSI escape sequences) and tries to translate these into
;; faces.
;;
;; This allows you to run ls --color=yes in shell-mode. In order to
;; test this, proceed as follows:
;;
;; 1. start a shell: M-x shell
;; 2. load this file: M-x load-library RET ansi-color RET
;; 3. activate ansi-color: M-x ansi-color-for-comint-mode-on
;; 4. test ls --color=yes in the *shell* buffer
;;
;; Note that starting your shell from within Emacs might set the TERM
;; environment variable. The new setting might disable the output of
;; SGR control sequences. Using ls --color=yes forces ls to produce
;; these.
;;
;; If you decide you like this, add the following to your .emacs file:
;;
;; (autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
;; (add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
So it looks like there could be a problem with the TERM environment
variable (but do not ask me what this stands for).
I suppose (hope?) this is a one-liner for somebody really
knowledgeable about emacs.
Kind Regards
Lorenzo
ansi-color.el is part of emacs, it will be loaded with emacs, so you
have nothing to do with that.
Also:
be sure you have something like that in your .bashrc:
,----
| if [[ -f ~/.dir_colors ]]; then
| eval `dircolors -b ~/.dir_colors`
| else
| eval `dircolors -b /etc/DIR_COLORS`
| fi
`----
(Assume you have such a file)
The best is to have a per-user file : "~/.dir_colors"
Just copy it from /etc
You can also add this line in this file:
,----
| TERM dumb
`----
And now with the code i sent in precedent post or the code Peter sent,
it should work.
~$ env | grep TERM
TERM=xterm
COLORTERM=gnome-terminal
and this is my .bashrc file (or better: the part about aliases and
colors, which I think is relevant here):
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color)
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
'
;;
*)
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
;;
esac
# Comment in the above and uncomment this below for a color prompt
#PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
'
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD/$HOME/~}\007"'
;;
*)
;;
esac
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
#if [ -f ~/.bash_aliases ]; then
# . ~/.bash_aliases
#fi
# enable color support of ls and also add handy aliases
if [ "$TERM" != "dumb" ]; then
eval "`dircolors -b`"
alias ls='ls --color=auto'
#alias dir='ls --color=auto --format=vertical'
#alias vdir='ls --color=auto --format=long'
fi
# some more ls aliases
#alias ll='ls -l'
#alias la='ls -A'
#alias l='ls -CF'
and this is my /etc/profile file:
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
if [ "`id -u`" -eq 0 ]; then
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"
else
PATH="/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games"
fi
if [ "$PS1" ]; then
if [ "$BASH" ]; then
PS1='\u@\h:\w\$ '
else
if [ "`id -u`" -eq 0 ]; then
PS1='# '
else
PS1='$ '
fi
fi
fi
export PATH
umask 022
As to your suggestion:
> in case you need a drop-in solution for emacs, you can use the lines
> below.
>
> (comint-send-string (current-buffer) "alias ls='ls --color'")
> then do a
> (comint-send-input)
please, do not laugh, but I am a bit at a loss about the way I should
put this into my .emacs file. The part dealing with the shell is now:
(require 'shell-command)
(shell-command-completion-mode)
;; ;;Now I add another customization for the shell-mode
(autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
(custom-set-variables
'(comint-scroll-to-bottom-on-input t) ; always insert at the bottom
'(comint-scroll-to-bottom-on-output t) ; always add output at the bottom
'(comint-scroll-show-maximum-output t) ; scroll to show max possible output
'(comint-completion-autolist t) ; show completion list when ambiguous
'(comint-input-ignoredups t) ; no duplicates in command history
'(comint-completion-addsuffix t) ; insert space/slash after
file completion
)
; make completion buffers disappear after 3 seconds.
(add-hook 'completion-setup-hook
(lambda () (run-at-time 3 nil
(lambda () (delete-windows-on "*Completions*")))))
(add-hook 'shell-mode-hook '(lambda () (toggle-truncate-lines 1)))
(setq comint-prompt-read-only t)
What should I actually add to this?
Hope this is the conclusion, thanks again to everybody.
Cheers
Lorenzo
2008/9/29 Paul R <paul...@gmail.com>:
>
> Lorenzo> So it looks like there could be a problem with the TERM
> Lorenzo> environment variable (but do not ask me what this stands
> Lorenzo> for). I suppose (hope?) this is a one-liner for somebody
> Lorenzo> really knowledgeable about emacs. Kind Regards
>
> in case you need a drop-in solution for emacs, you can use the lines
> below.
>
> (comint-send-string (current-buffer) "alias ls='ls --color'")
> then do a
> (comint-send-input)
>
> --
> Paul
>
--
It is hard to want something you cannot describe.
Yes, probably. Just read your ~/.bashrc and /etc/profile. Both files
are source'd when your shell starts. I suspect something like an alias
from 'ls' to 'ls --color' to be set in a conditionnal switch. If your
term is dumb (env | grep TERM), the alias is not made. Just ensure by
the mean you want that this alias occurs even in emacs shell mode.
--
Paul
in case you need a drop-in solution for emacs, you can use the lines
Leave your .emacs as it is and create a file '~/.emacs_bash', insert
the following lines, save and you should have colors.
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
eval "`dircolors -b`"
alias ls='ls --color=auto'
-ap
I guess this is the output of the command run from within
gnome-terminal, not from emacs shell mode :)
Run it from shell mode you should read that the term is dumb.
Lorenzo> [ "$TERM" != "dumb" ]; then eval "`dircolors -b`" alias
Lorenzo> ls='ls --color=auto' #alias
Here we go. Emacs comint shell is dumb so the alias won't happen.
Lorenzo> please, do not laugh, but I am a bit at a loss about the way
Lorenzo> I should put this into my .emacs file. The part dealing with
Lorenzo> the shell is now:
I don't use M-x shell at all so I have hard time helping you more.
Reading shell.el code yield :
,---- see lines starting with !!!
| (interactive
| (list
| (and current-prefix-arg
| (read-buffer "Shell buffer: "
| (generate-new-buffer-name "*shell*")))))
| (setq buffer (get-buffer-create (or buffer "*shell*")))
| ;; Pop to buffer, so that the buffer's window will be correctly set
| ;; when we call comint (so that comint sets the COLUMNS env var properly).
| (pop-to-buffer buffer)
| (unless (comint-check-proc buffer)
| (let* ((prog (or explicit-shell-file-name
| (getenv "ESHELL") shell-file-name))
| (name (file-name-nondirectory prog))
|!!!!! NOTICE BELOW
| (startfile (concat "~/.emacs_" name))
| (xargs-name (intern-soft (concat "explicit-" name "-args"))))
| (unless (file-exists-p startfile)
| (setq startfile (concat "~/.emacs.d/init_" name ".sh")))
| (apply 'make-comint-in-buffer "shell" buffer prog
| (if (file-exists-p startfile) startfile)
| (if (and xargs-name (boundp xargs-name))
| (symbol-value xargs-name)
| '("-i")))
| (shell-mode)))
| buffer)
`----
So what we read here is that shell will try to evaluate (with your
shell) the file ~/.emacs_bash or ~/.emacs.d/init_bash.sh
So just drop in either file
alias ls='ls --color'
followed by a blank line (thx comint) and it should work
automagically.
By the way, have you tried eshell ? In most cases, it just works.
--
Paul
I just noticed that I have the alias for my ls --color=auto in my bashrc
but the emacs shell does not pick it up in debian. So maybe another rc
file needs this stuff?
I am not sure .bashrc is sourced when you start "shell". It doesnt seem
to be here on my debian system. I think you need a .emacs_bash. Which is
different from a few years ago. Maybe someone more knowledgeable can
comment.
This time ran from within emacs.
I had neither file ~/.emacs_bash nor ~/.emacs.d/init_bash.sh, but
creating ~/.emacs.d/init_bash.sh with the only content
alias ls='ls --color'
followed by a blank line (precisely as you said) got the job done.
Thanks again to the people on the list.
Cheers
Lorenzo
2008/9/29 Paul R <paul...@gmail.com>:
--
It is sourced here on my box with default settings, but as written
above, your alias statement it probably bypassed because of dumb
terminal.
--
Paul
> Leave your .emacs as it is and create a file '~/.emacs_bash', insert
> the following lines, save and you should have colors.
>
> PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033
> [00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
> eval "`dircolors -b`"
> alias ls='ls --color=auto'
This is only bash code – don't you think you need to prepare GNU
Emacs to *execute* the ANSI codes GNU ls will be emitting? This is
true for my GNU Emacs 22.3. Your example of a shell prompt can cause
problems in *shell* buffer – such a prompt is not easy to find, which
means that older commands also are not easy to find or recognised.
You can customise instead the variable comint-highlight-prompt.
There are also other versions of ls around that for example make
colourisation depending on terminal capabilities. Anyway, it's good
to know that a file ~/.emacs_<shell interpreter> exists that can be
used to setup *shell* buffer, for example switch off what's working
in xterm or such. Or switch on.
--
Mit friedvollen Grüßen
Pete <]
o __o |__ o HPV, the real
___o /I -\<, |o \ -\),-% high speed!
___/\ /\___./ \___...O/ O____.....`-O-'-()--o_________________