Since Emacs 23, you no longer need to care whether Emacs was started from inside X. Start Emacs however you like, possibly as a daemon. Open and close frames on any display as desired.
Start Emacs with
emacs --daemon
then when you want to edit a file, run
emacsclient MYFILE
or if you just want to create a frame, run
emacsclient -c
which will create an X11 frame if an X11 display is available, and a terminal frame otherwise.
If you prefer to start Emacs with a frame on a terminal, make sure that your init file includes
(server-start)
Run emacs
normally. If you've started it outside X, you'll get a terminal frame. You can at any point call emacsclient -c
with the DISPLAY
environment set to open a GUI frame. Once a GUI frame exists, you can close the terminal frame. The Emacs process will still have the original terminal as its controlling terminal, but you can send it to the background (Ctrl+Z then use the shell command bg
) and close the original terminal, and keep using the new GUI frame (and any other frame you care to create). What you can't do as far as I know is close the initial terminal frame but keep Emacs running in the background to open new frames later, i.e. you can't daemonize a running Emacs.
If for some reason you want to run a new instance of Emacs and you want it to use an X11 display and refuse to start otherwise, you can do this with a shell command:
emacs -d "$DISPLAY"
Explicitly passing the -d
option forces the use of an X11 display.