9

When I want to switch between programs in Windows, I ordinarily use the windows key plus a number key to run a program from my taskbar. Most programs will either start running or, if there's already an instance, bring it to the foreground and focus on it. But Emacs (started with runemacs.exe) will create a new entry on the taskbar, and using the shortcut again will just create a new instance. As far as I can tell, I can't even switch between the different frames using Emacs commands (I tried C-x 5 o).

Is there a way to make it so subsequent uses of the windows hotkeys will just use the existing instance of Emacs and, if not, can I at least make it so I can easily navigate between the different frames? I tried this snippet:

(require 'server)
(unless (server-running-p)
  (start-server)) 

from the answer to this question, but it didn't make a difference.

7
  • Can you specify which snippet you are referring to?
    – nispio
    Commented Oct 15, 2014 at 23:48
  • That snippet makes sure that an edit server is created the first time that Emacs is run. Subsequent calls to Emacs which wish to connect to the same edit server must use emacsclientw.exe
    – nispio
    Commented Oct 15, 2014 at 23:55
  • @nispio Ah, that explains why it wasn't working. Is there a way to have everything run on the same edit server without needing to run from different executables?
    – resueman
    Commented Oct 15, 2014 at 23:56
  • This answer will probably work: emacs.stackexchange.com/a/158/93. When you provide runemacs.exe as an alternate editor, you can call emacsclientw.exe even when there is no edit server running.
    – nispio
    Commented Oct 15, 2014 at 23:59
  • 1
    See my comment to nispio 's answer regarding how to avoid creating a new frame.
    – Vamsi
    Commented Oct 16, 2014 at 0:36

2 Answers 2

8

Add this to your init file to make sure that an edit server is created when emacs starts

(require 'server)
(unless (server-running-p)
  (server-start)) 

Now make your windows shortcut point to emacsclientw.exe as follows:

C:\path\to\emacsclientw.exe -n -e "(raise-frame)" -a "C:\path\to\runemacs.exe"

This should focus an existing Emacs frame (if any), or else call runemacs.exe otherwise. (Thanks to Vamsi for help with tuning the command parameters)

7
  • This works how I want if I pass it a file to run, but if I just use the command as-is then it displays an error saying "file name or argument required."
    – resueman
    Commented Oct 16, 2014 at 0:10
  • @resueman Does a -c flag fix that problem?
    – nispio
    Commented Oct 16, 2014 at 0:27
  • 3
    @nispio One can use C:\path\to\emacsclientw.exe -n -e "(raise-frame)" -a "C:\path\to\runemacs.exe" to avoid creating a new frame and raise the previous frame.
    – Vamsi
    Commented Oct 16, 2014 at 0:35
  • @Vamsi but without the -c will it also expect a file to visit? (If you can't tell I'm not at a Windows machine right now.)
    – nispio
    Commented Oct 16, 2014 at 0:36
  • @Vamsi That works perfectly. @nispio it seems to work fine without -c as long as it has the -e expression.
    – resueman
    Commented Oct 16, 2014 at 0:43
12

I stumbled on another answer to the problem on SuperUser today. It seems more hackish, but the result is more consistent with other programs on Windows.

  1. Run runemacs.exe with no pre-existing icon in the taskbar.
  2. Right click on the running Emacs icon in the taskbar, and click on "pin this program to taskbar."
  3. Close Emacs
  4. Shift right-click on the pinned Emacs icon on the taskbar, click on Properties, and change the target from emacs.exe to runemacs.exe.
3
  • 1
    FWIW, that's what I do. I didn't understand that that's what you wanted, from your question. I thought you wanted to use emacsclient, among other things. BTW, in Properties you can also set the Start in directory and open Emacs to a particular file or directory. (I start Emacs with Dired in the Start in directory. IOW, I pass the same dir ("-quoted) as argument to runemacs.)
    – Drew
    Commented Oct 18, 2014 at 3:10
  • @Drew I probably didn't explain what I wanted very well. I'm still new enough that it's hard to keep everything straight. Thanks for the tips.
    – resueman
    Commented Oct 18, 2014 at 3:13
  • This is the most straightforward answer, thanks.
    – gsl
    Commented Mar 27, 2021 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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