I'm trying to make a script to determine whether emacsclient should run with -nc or -t. The script is like this (based on this version):

if [ -z "$DISPLAY" ]; then 
    IS_GRAPHICAL=$(emacs --batch -Q --eval='(if (fboundp '"'"'tool-bar-mode) (message "true") (message "false"))' 2>&1) 
if $IS_GRAPHICAL; then 
    emacsclient -a "" -nc "$@" 
    emacsclient -a "" -t "$@" 

However the emacs --batch -Q ... line takes as much time as using a bare emacs instance. If I comment out this line, I get nothing instead of a clean screen when this is executed under a tty. Is it possible to make the detection faster?

I have already started an emacs daemon using a systemd user unit. I want emacsclient to:

  • stay in the terminal (that is, do not open a GUI window) if called with a file (e.g. called by git-commit) or when a graphical environment isn't present (e.g. called while in tty) and continue running until exit (C-x C-c)
  • open a GUI window when called without a file (behave like bare emacs call; when I do this with emacsclient it tells me that input file is required) and return immediately.
  • I don't understand what you're trying to do here. What is this test for tool-bar-mode supposed to accomplish? Feb 17, 2016 at 1:01
  • @Gilles Uh.. The script is mainly from ArchWiki and I don't know either. Feb 17, 2016 at 1:46
  • emacs is pretty good at understanding its environment, I'm guessing it's only the " in terminal while launched in terminal" part that's needs to be forced, yes?
    – bsd
    Feb 17, 2016 at 10:36
  • @bsd Perhaps yes. When I use emc as $EDITOR git exits when trying to record commit message (aborting due to empty commit message) Feb 17, 2016 at 10:45
  • Are you using Emerge? If so, there is a paragraph in the Emacs section of the Arch Wiki "Using Emacs as git mergetool"
    – bsd
    Feb 18, 2016 at 9:57

1 Answer 1


That script from the Arch Wiki

This invocation of emacs that checks (fboundp 'tool-bar-mode) is testing whether your build of Emacs includes GUI support. It's possible to build Emacs without it to save a little bit of RAM and disk space¹, but it's uncommon. Unless you care about compatibility with Emacs builds that lack GUI support, forget about this bit.

You are thus left with

if [ -n "$DISPLAY" ]; then
  emacsclient -a "" -n -c "$@"
  emacsclient -a "" "$@"

And even that level of complexity is necessary only if you want to have the call to emacsclient return immediately if it opens a new GUI window. (If it's running in the terminal, then it has to keep running until you finish using that terminal.) It's up to you to decide whether you like this behavior. If you don't then you can simply run

emacsclient -a "" "$@"

And if you start Emacs with your session anyway, e.g. as a systemd unit, then you don't need to tell emacsclient to start it. So we're left with

emacsclient "$@"

which I cannot simplify further.

Your added requirement

  • stay in the terminal (…) if called with a file (…) or when graphical environment isn't present (…)
  • open a GUI window when called without a file (…) and return immediately

For that you need to test whether the script was called with arguments. I'm going to assume that if you pass any emacsclient option (e.g. --eval), that counts as an argument.

if [ $# -ne 0 ]; then
  emacsclient -nw "$@"
elif [ -n "$DISPLAY" ]; then
  emacsclient -c -n
  emacsclient -c

¹ No GUI support means you don't need to have the requisite libraries installed. The emacs program can't start if the libraries it eneds aren't present, even if it isn't going to use the GUI features in that particular session.

  • Maybe I have not expressed myself clearly; question edited above; sorry :) Feb 18, 2016 at 5:10
  • This way I never get a GUI window from emacsclient. Also I can't just get an empty emacs window (e.g. for C-h t) from merely an emccall. Feb 18, 2016 at 10:09
  • @JohnsonSteward Well I sure couldn't have guessed those requirements, I'm not a mind reader. I've added an implementation of your requirement to my answer. Feb 18, 2016 at 10:54
  • Uh.. You still ignored something in the question. If I do emc blabla in gmrun, I should get a GUI window. Feb 18, 2016 at 12:00
  • @JohnsonSteward No, that's not what you asked for. You wrote “stay in the terminal (…) if called with a file” and that's what I implemented. If you want to change your requirements, ask a new question and be sure to state your requirements clearly. Feb 18, 2016 at 13:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .