Terminal emacs colors only work with TERM=xterm-256color

emacs

I've found that terminal emacs does not render the correct colors unless I explicitly set TERM=xterm-256color. I use gnome-terminal, and from what I understand, TERM should be set to gnome-256color. Similarly, I tend to use tmux a lot, which advises against any TERM setting other than screen-256color. Unfortunately, both of those settings (within their respective context – gnome-terminal or tmux) result in emacs having wrong colors, whereas vim displays colors correctly. However, if I export TERM=xterm-256color, the colors work just fine in emacs.

Can anyone explain what's going on, or offer a solution?

Update

Here's what I'm dealing with:

enter image description here

I can get the colors to look correct in the terminal by adding the following to my init.el:

(defun terminal-init-gnome ()
  "Terminal initialization function for gnome-terminal."

  ;; This is a dirty hack that I accidentally stumbled across:
  ;;  initializing "rxvt" first and _then_ "xterm" seems
  ;;  to make the colors work... although I have no idea why.
  (tty-run-terminal-initialization (selected-frame) "rxvt")

  (tty-run-terminal-initialization (selected-frame) "xterm"))

This feels really, really wrong though. There has to be a logical explanation for this…

P.S.

I have very little knowledge of terminfo and the precise role that $TERM plays in the process of color terminal behavior. If it's safe to always use xterm-256color (even when $TERM "should" be gnome-256color or screen-256color), I'll go with that.

Best Answer

Maybe I'm not understanding something, buy why don't you run emacs like this:

TERM=xterm-256color emacs -nw

This way Emacs has its own TERM setting that you know works. You can also make an alias or wrap this in shell-script.

Related Topic