19

I am particularly interested in the full Windows experience including:

  • Automatically starting a server if one is not already started
  • How to call emacs so that it utilizes the server (e.g. from command line or third party tools)
  • Context menu support (e.g. right-click on a file and allow it to be sent to emacs for viewing and editing)

Is such support available out of the box anywhere?

3 Answers 3

22

Automatically starting a server if one is not already started

This should be the same for any Emacs build. Add this snippet to your .emacs or .emacs.d/init.el.

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

And then just start emacs by running the runemacs.exe executable that comes with the build.

How to call emacs so that it utilizes the server (e.g. from command line or third party tools)

Emacs' Windows build comes with an emacsclientw.exe executable in the bin/ directory. Use the full path to that executable as your editor of choice on any third party tools you need to configure.

Context menu support (e.g. right-click on a file and allow it to be sent to emacs for viewing and editing)

  1. Right click on a file, select Open With -> Select Default Program.
  2. In the window that pops up, navigate to your emacsclientw.exe executable mentioned above and choose it.
  3. If you don't want Emacs to be the default, do steps 1 and 2 again, choosing a different program as the default.

emacsclientw.exe will now always be an option in the context menu.

6
  • Thanks - great point about the server, I wasn't thinking. Commented Sep 24, 2014 at 19:12
  • Regarding the emacsclient.exe, shouldn't that be emacsclientw.exe? Also, I thought you had to run this with the -a flag to specify an alternative editor if the server wasn't running. Commented Sep 24, 2014 at 19:17
  • In order to get the server to work properly with my configuration, I also had to add an environment variable EMACS_SERVER_FILE that pointed to my .emacs.d\server directory.
    – Ryan
    Commented Sep 30, 2014 at 0:10
  • @Ryan I was able to get this working without the need for the environment variable - just needed to ensure that the server was running. Commented Dec 30, 2014 at 15:21
  • Hmm, for me evaluating (server-running-p) returns :other on Windows (even after trying setting that EMACS_SERVER_FILE env var. Commented Nov 5, 2015 at 4:06
6

Automatically starting a server if one is not already started

I highly recommend setting an environment variable for ALTERNATE_EDITOR to runemacs.exe. This way you can always call emacsclient even if there is no server. Just add (server-start) to your init file. When a server does not exist emacsclient calls the ALTERNATE_EDITOR which is emacs in our case. Since emace starts up a server in the init file, any new calls to emacsclient will be instantaneous.

How to call emacs so that it utilizes the server (e.g. from command line or third party tools)

Use emacsclientw.exe -c -n "your_file" to open your_file . The -c argument creates a new graphical frame and -n allows you to close the frame without losing server state. You can even call it without a filename to open the *Scratch* buffer. Works from a desktop shortcut also. Using emacsclient.exe (note the missing w) will pop up a command prompt for a few seconds everytime you call it.

Adding to context menu

Emacswiki has a nice page on adding Emacs to the context menu here. It involves editing the registry node HKEY_CLASSES_ROOT\* to add an action to open with emacs.

1
  • The option -c tells Emacs explicitly to create a new instance/window, which is not what the OP wanted. Using emacsclientw.exe without options works better. Commented Apr 15, 2020 at 5:26
5

Others have answered the server part. Here is what I do, to get UNIX / GNU/Linux-like utilities such as grep and diff:

  1. Install Cygwin (one-time operation).
  2. Load cygwin-mount.el.
  3. Load setup-cygwin.el.

However, be aware that there are different versions of Cygwin. I use an older version (dunno which one - hard to tell, AFAICT). Dunno whether you will have problems with recent versions.

See also:

7
  • Note: Do not use the cygwin EMACS. Install the GNU build from ftp.gnu.org/gnu/emacs/windows
    – M Smith
    Commented Sep 24, 2014 at 18:30
  • I do not recommend to install Cygwin, as there can be undesirable side-effects (like breaking other gnu windows ports like emacs, gcc, ...), for just having some gnu tools. Instead install GnuWin32 ports. You can install the whole package or selected tools of your choice.
    – Seki
    Commented Sep 24, 2014 at 23:07
  • @Seki - Yes, Eli Zaretskii says the same thing as you. All I can say is that I have had no problem (and I have serious doubts wrt your claim of it breaking Emacs). But (a) I am using an old Cygwin release and (b) I don't use most of the UNIX / GNU Linux utilities provided by Cygwin.
    – Drew
    Commented Sep 25, 2014 at 1:27
  • @drew: perhaps excessive for talking about "breaking" emacs, "causing weird behavior" would have been better. For example Cygwin Make will brake the MinGW toolchain if you do not carefully check your path for Cygwin tools not being taken into account or taken by default if no other exist. It is not limited to Make, many other tools cannot mix and sometimes it is subtle to point the problem. So in short, either you have a native port of Emacs and it is better to have native GNU tools, or you can use the whole Cygwin distribution including its emacs.
    – Seki
    Commented Sep 25, 2014 at 8:36
  • @Seki: So it seems that your main point is that Cygwin and MingGW do not play well together. That's not surprising to me. (FWIW, I have nothing against using MingGW instead of Cygwin. For me it was simple to install Cygwin, and I don't use enough of it to have encountered problems.)
    – Drew
    Commented Sep 25, 2014 at 15:11

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.