[OS X 10.11, MacPorts Emacs 24.5, OS X Terminal, lots of stuff in emacs and shell init files]

I almost always use Emacs.app (GUI). Sometimes I want to open Emacs in a terminal. When I open Emacs from a terminal I get a bright green background that in combination with my usual theme is unusable -- I can't even read the messages at the bottom of the window.

I have tried this with various Terminal Profiles but they all do the same. I have tried emacs -bg while but that doesn't work either.

How do I control the background color of Emacs opened from the terminal?

  • You wrote that used emacs -bg while, is it mistype there or in your command in console: whiTe or whiLe? – Konstantin Morenko Jul 25 '16 at 18:07
  • good catch, but yeah, it was "white" – Mitchell Model Jul 26 '16 at 21:10

Few themes work well with emacs run within a terminal because you are dealing with a much smaller color palette. Unless you have enabled 256 colors in your terminal, you are likely working with just 8 colors when you fire up emacs in a terminal. An easy way around this issue is to conditionally load your color theme selections only when you are working with the gui version of emacs. The window-system variable contains the info you want, however use of that variable in situations such as this is deprecated in favor of the function display-graphic-p.

In your .emacs file try guarding your color theme initialization using something like the following:

(when (display-graphic-p)) ; color theme initialization code goes here)

display-graphic-p returns non-nil if the current display is a graphic display, and it returns nil if it is a terminal. See the docs if you are interested: C-h f display-graphic-p. For more info on window-system variable: C-h v window-system.

Doing this will cause the default color theme to be loaded when you start emacs in a terminal. While not very pretty, it works with 8 and 256 colors.

  • I have 256 colors. I have been using Emacs for so long I never even got around to adopting themes ‚— I just use customizations and explicit background/foreground colors, and let syntax highlighting do the rest. I guess it's time I moved in to the 90s :-). I have use window-system to select which set of keybindings I want to add to a common set, but I'll use display-graphic-p as you suggest when I start to use themes. – Mitchell Model Jul 26 '16 at 21:14
  • So although your information is useful and probably solves this problem for many people I don't think it solves it for me. Any idea where the bright green is even coming from? – Mitchell Model Jul 26 '16 at 21:21
  • @Mitchell Using emacs in the gui allows for far more colors than the 256 colors you have enabled in the terminal. I happen to use emacs in a terminal at work, and I get some wonky results when I enable (most) color themes. Although you are not using color themes, it could be that the color that you have selected for your background does not translate well to the 256-color environment of the terminal. – mwood Jul 27 '16 at 1:19
  • @MitchellModel Just now I tried setting the background color when invoking emacs without the gui as you did, but I was able to alter the background color that way. I do not currently have color themes or color settings of any kind enabled. Perhaps there is some conflict between what is in your init file and the command line option you are passing emacs. I ran it with the following command just now: emacs -nw -bg white – mwood Jul 27 '16 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.