Making emacs theme and terminal theme play along nicely

I have been using emacs for more than six months now and still had a few things which were annoying me as I had not really completely configured it. Today I fixed all the things which were annoying me.

When editing from the command line, I use to resort to using nano or more recently vim. I was using nano as the text editor for editing my git logs, patches and as an editor for mutt too. Why not use emacs for all of them? Well if you open single emacs instances all the time, it takes time to load the .emacs or init.el file and even if you invoke it as emacs -Q -nw it is not really worthwhile. What is required is to run emacs in daemon mode. I created a systemd service file to start emacs in daemon mode at startup with this script taken from the Arch wiki.

Description=Emacs: the extensible, self documenting text editor

ExecStart=/usr/bin/emacs --daemon
ExecStop=/usr/bin/emacsclient --eval "(kill emacs)"


This results in emacs automatically being started in daemon/server mode. Added a few aliases to my .zshrc file as below.

alias e="emacsclient -t"
alias ec="emacsclient -c"

Now I can open emacs from command line as “e filename”. And it will open quickly now. So all good? No, still one problem remained. I use solarized theme for my terminal and mutt while I use zenburn for emacs. On opening a file as above, the color themes I guess superimpose or mix and result in a very bad display of text. If I run emacs -Q -nw to open a file it would be alright, as the -Q option does not lead emacs to load it’s init.el file. I now needed a way to tell emacsclient to not apply any theme while being opened on a terminal. After some searching, adding the below snippet to init.el resulted in what I wanted. So if I run a GUI emacs, zenburn theme is used, if I use emacs in terminal, no background theme gets applied.

(defun on-frame-open (frame)
  (if (not (display-graphic-p frame))
      (set-face-background 'default "unspecified-bg" frame)))
(on-frame-open (selected-frame))
(add-hook 'after-make-frame-functions 'on-frame-open)

And viola……