3

[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?

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

2 Answers 2

4

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.

4
  • 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. Jul 26, 2016 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? Jul 26, 2016 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, 2016 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, 2016 at 1:35
0

Something like this in your ~/.emacs will leave the terminal foreground and background untouched, i. e. emacs will use your terminal colors:

(custom-set-faces
 ;; custom-set-faces was added by Custom.                                   
 ;; If you edit it by hand, you could mess it up, so be careful.            
 ;; Your init file should contain only one such instance.                   
 ;; If there is more than one, they won't work right.                       
 '(default ((((type tty))))))

This basically says that when the display type is tty, don't set attributes on the default face.

You can use this more generally to pick different attributes for X11 vs. terminal:

(custom-set-faces
 ;; custom-set-faces was added by Custom.                                                                                                                                                                 
 ;; If you edit it by hand, you could mess it up, so be careful.                                                                                                                                          
 ;; Your init file should contain only one such instance.                                                                                                                                                 
 ;; If there is more than one, they won't work right.                                                                                                                                                     
 '(default ((((type tty)))
             (t (:background "#6F4700" :foreground "yellow" :slant normal
                 :weight normal :width normal :height 140 :foundry "default"
                 :family "monospace"))))
 '(font-lock-comment-face ((((type tty)) (:foreground "brightgreen"))
                            (t (:foreground "green1")))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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