I am an emacs user and have written a few emacs configuration files.

I wanted to run the client-server feature of emacs using a single bash script that would take care of the emacs server and client. The script runs the server (if not already running) and then follows it up by running the client in the desired frame (terminal or graphic).

I have soft-linked the executable script to /usr/bin/emacs (overriding the default) so that running the emacs command does the job for me. The command emacs now fires up the script from the terminal as well as via Alt+F2. The problem is, binding a keyboard shortcut to the same command (or the script) doesn't work.

I am attaching the screenshot of my shortcut here. enter image description here

The shell script can be found here.

P.S.: The keyboard shortcut runs fine when /usr/bin/emacs is linked to the default emacs executable (at /etc/alternatives/emacs in my case).

  • 1
    Have you tried emacsclient -c -a “” it automatically spawns a daemon if it's not found. If it founds a running daemon it doesn't get past the -c and connects to it – Umar Ahmad Oct 10 '15 at 3:14
  • It sure works! Not sure how I missed it out on the man page. Will integrate this command in my script. :) – reza.safiyat Oct 10 '15 at 6:18

I tried recreating your issue and I guess I got close.

The problem I figured out that you're facing is with the if [[ -t 1 ]] condition.

According to tldp

This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a given script is a terminal.

So, the script will not be able to detect the program that invoked it via this conditional. Hence, this is not the accurate solution to your problem.

Now, internally, the keybindings in Ubuntu are executed via a fake terminal. In fact, the TERM variable is set to dumb, when a command via the keybindings is run.

So, to solve your problem, you could use the following code.

if [ $TERM == "dumb" ]

I can not guarantee that it will work in all the cases, but it works for the usage you described.
Hope it helps. :)

  • Though now I am facing a different issue, but it doesn't belong in the askubuntu network. Will post the link in the comments when I post it in the emacs network. – reza.safiyat Oct 13 '15 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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