I have customized the .emacs file a little to suit my taste, and it works fine when run in its own window.

But when I try to run it in my terminal using the command emacs -nw, it throws up the following error in a split view, with the other view running emacs properly on the console, albeit without a few of my customizations.

Warning (initialization): An error occurred while loading `/home/safiyat/.emacs':

error: X windows are not in use or not initialized

To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file.  Start Emacs with

How is it possible to have an error free load of the init file on both X and the console?

Thank you.

  • 1
    Using the condition (display-graphic-p) might be an answer. Please note that if you're using the daemon, emacs runs your init file in a non-GUI context but emacsclient will happily create GUI frames. Some care has to be taken if you want to have full featured GUI frames when using them, and still no error when using text-only. – YoungFrog Aug 31 '15 at 7:27
  • Adding (display-graphic-p) to the init file didn't help. Can't we add some checks in the init file, so that it detects the environment it is being run in? – reza.safiyat Aug 31 '15 at 7:33
  • 2
    That on its own won't do anything. Your init file is written in an actual programming language. So, you'll need to have to find the problematic constructs by bisecting it, then wrapping these in something like (if (display-graphic-p) (do-graphic-related-stuff) (do-textual-stuff)). – wasamasa Aug 31 '15 at 7:50
  • Thanks @YoungFrog and @ wasamasa for the help. It worked. Had to combine all the commands and functions in a progn though. – reza.safiyat Aug 31 '15 at 11:05

Thanks to the help by YoungFrog and wasamasa, I came up with this that solved my problem.

 (when (display-graphic-p)   ;; Return non-nil if emacs is running in a graphic display.
    (load-file (concat (file-name-as-directory cust-emacs-dir) "loadpaths/fullscreen.el"))
    (require 'fullscreen)
|improve this answer|||||
  • 1
    For an if without an else, you can use when instead of if. It's somehow more idiomatic and you don't have to worry about progn. In the same way, if you need an if without a then you can use unless. – T. Verron Sep 8 '15 at 7:47

Your Answer

