Emacs 23 on OS X - use multiple instances or emacsclient?
Asked Answered
B

6

6

I wonder if anyone has any recommendations as to setup of emacs 23 on OS X. I have been using Linux/Windows for a while and there, I would just open many instances of emacs and they were self-contained - for instance, I could have two separate files in separate frames, each with their own R, Python, and/or shell running. I think this is frowned upon(?) - but it kept the working directory for each script file separate, which I loved.

I understand there is some advantage to setting (server-start) in my .emacs file and calling 'emacsclient' rather than 'Emacs' is now the preferred way(?). I found this thread which facilitates this to occur through an Applescript: Emacs 23, OS X, multi-tty and emacsclient

But I wonder if this the only way to go about it - when I open two emacs instances on OS X it gives me the error:

** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) 'Permission denied', port = 0x3803, name = 'org.gnu.Emacs.ServiceProvider'

but opens a new frame allows me to edit files. I understand that for vim on OS X there is something called 'mvim' which allows multiple instances of vim to be launched, but is there something similar for emacs? Sorry for the long-winded question - in summary, I guess these are the questions for which I would like to solicit your expertise:

(1) Is 'emacsclient' the way of the experts, and (2) if not necessarily, is there a way to get multiple instances of emacs 23 (not just frames) running on OS X?

Thanks!

Balfour answered 12/8, 2009 at 0:57 Comment(0)
P
6

Let me answer your questions indirectly.

As you know, emacsclient provides a service, allowing users to open documents from outside Emacs in an existing Emacs. Is this "the way" of the experts? Depends on the expert. I'm a heavy user of Emacs, but never use emacsclient. Why? Because I never leave Emacs in the first place. All the shells I run are inside Emacs (using a home-brewed screen package), I read mail in Emacs, etc. There's never a reason for me to type emacsclient at a command line.

Your usage model seems to be that you prefer having multiple Emacsen running, making it easier to keep track of which processes and files are being used together. And that makes sense. You could do it all in one Emacs if you wish, but it might take some changes in your usage model, or some customizations to help manage multiple interpreters/shells/whatever to your liking.

So, if you like multiple instances of Emacs running, then by all means, continue doing so.

Regarding the second question, it appears as though having (server-start) in your .emacs is causing problems - likely because there's a conflict with two Emacsen trying to use the same port. Check out the documentation for starting the server, and perhaps remove that line from your .emacs, and instead start the server once by using the --daemon command line option.

Then, at the command line, you have the choice of whether you want to start a new emacs (by typing emacs) or connect to the server you started (by typing emacsclient).

Pastrami answered 12/8, 2009 at 3:55 Comment(4)
Thanks for your response - I don't have (server-start) on when I get the bootstrap_register fail, and still get a warning (can't connect to server) when the server is running and I start another instance. I think this is an OS X app thing: osdir.com/ml/emacs.macintosh.osx/2005-07/msg00036.html, and the Carbon Emacs page foreshadows warnings when opening multiple instances. From what I gather my options are to (1) ignore the warnings and continue working in the second/third/... instance, or (2) become better at trying to manage multiple working directories.Balfour
to continue (hit 600char limit before) - thanks for the suggestion. Your suggestion does indeed work, albeit with warnings (and the first file I edit is associated with the server instance).Balfour
I needed to run emacsclient a few days ago to access my session at work from home so I could check the output of some commands running in a shell. I also like using it for $EDITOR so I can have a nice environment for editing crontabs, etc.Botfly
when I read "I never leave Emacs" I seriously wished I could do my web browsing from with it tooFairlead
M
7

If you want multiple instances (and not frames) of emacs use the following at the terminal prompt:

/Applications/Emacs.app/Contents/MacOS/Emacs &

(Generic form for any app) /Path_To_Application.app/Contents/MacOS/Application &
Montfort answered 30/3, 2011 at 18:14 Comment(2)
This might bite you; I would not be surprised if the multiple instances clobbered each other's temporary files. In my case, I have files/directories in .emacs.d such as auto-save-list, backups, bookmarks, ido.last, places, and projectile.cache.Toliver
Check this out if you want to run multiple instances with different configuration directories: How to start emacs with a custom user-emacs-directory (on the relatively new Emacs StackExchange, which is worth a look).Toliver
P
6

Let me answer your questions indirectly.

As you know, emacsclient provides a service, allowing users to open documents from outside Emacs in an existing Emacs. Is this "the way" of the experts? Depends on the expert. I'm a heavy user of Emacs, but never use emacsclient. Why? Because I never leave Emacs in the first place. All the shells I run are inside Emacs (using a home-brewed screen package), I read mail in Emacs, etc. There's never a reason for me to type emacsclient at a command line.

Your usage model seems to be that you prefer having multiple Emacsen running, making it easier to keep track of which processes and files are being used together. And that makes sense. You could do it all in one Emacs if you wish, but it might take some changes in your usage model, or some customizations to help manage multiple interpreters/shells/whatever to your liking.

So, if you like multiple instances of Emacs running, then by all means, continue doing so.

Regarding the second question, it appears as though having (server-start) in your .emacs is causing problems - likely because there's a conflict with two Emacsen trying to use the same port. Check out the documentation for starting the server, and perhaps remove that line from your .emacs, and instead start the server once by using the --daemon command line option.

Then, at the command line, you have the choice of whether you want to start a new emacs (by typing emacs) or connect to the server you started (by typing emacsclient).

Pastrami answered 12/8, 2009 at 3:55 Comment(4)
Thanks for your response - I don't have (server-start) on when I get the bootstrap_register fail, and still get a warning (can't connect to server) when the server is running and I start another instance. I think this is an OS X app thing: osdir.com/ml/emacs.macintosh.osx/2005-07/msg00036.html, and the Carbon Emacs page foreshadows warnings when opening multiple instances. From what I gather my options are to (1) ignore the warnings and continue working in the second/third/... instance, or (2) become better at trying to manage multiple working directories.Balfour
to continue (hit 600char limit before) - thanks for the suggestion. Your suggestion does indeed work, albeit with warnings (and the first file I edit is associated with the server instance).Balfour
I needed to run emacsclient a few days ago to access my session at work from home so I could check the output of some commands running in a shell. I also like using it for $EDITOR so I can have a nice environment for editing crontabs, etc.Botfly
when I read "I never leave Emacs" I seriously wished I could do my web browsing from with it tooFairlead
C
4

Here's a simple shell script that will start a separate instance, raise the Emacs window and give it focus:

#!/bin/bash

/Applications/Emacs.app/Contents/MacOS/Emacs \
    --eval '(select-frame-set-input-focus (nth 0 (frame-list)))' \
    "$@"

Notes:

  • This script makes emacs behave like the emacs on Linux that I'm used to.
  • There is probably a better way than '(nth 0 (frame-list))' to identify the frame to activate, but the above script does the right thing for me.
  • I do not have '(server-start)' in my .emacs file.
Caressa answered 28/11, 2012 at 5:7 Comment(0)
P
1

I use something like this in my .emacs to only call server-start if the server isn't already running:

(if (file-exists-p
 (concat (getenv "TMPDIR") "emacs"
         (number-to-string
          (user-real-uid)) "/server"))
nil (server-start))

Then a couple of changes to my .zshrc so that I can mindlessly run ec as my editor command from the shell:

# I use the Emacs package from emacsformacosx.com
alias emacs='open -a emacs'
alias emacsclient='/Applications/Emacs.app/Contents/MacOS/bin/emacsclient'
alias ec='emacsclient -a /Applications/Emacs.app/Contents/MacOS/Emacs'
export EDITOR='ec'
Plasia answered 9/3, 2011 at 9:53 Comment(3)
Great! I didn't know about the first expression -- thought checking to see if server was running would be more straightforward, but this seems to work.Balfour
there could very well be a better way to tell if the server is running or not, but this is the one the first came to my mind :)Plasia
The server-process variable is available for this. If you're using --daemon then you'll also want to check (daemonp), and to be aware that the server isn't started until your init file and any command-line arguments have been processed. Look near the end of the (command-line) function for details.Marroquin
A
0

I have created this shell script, which will allow you to open multiple emacs instances simultaneously.

To install, just copy the emacs-ide script to /usr/local/bin/ or any directory on your $PATH.

You can run multiple Emacs instances with multiple files or directories, as such:

emacs-ide ~/Projects/project-1
emacs-ide ~/Projects/project-2

The script also sets the name of the window to match the directory (project) or file being edited.

Allele answered 3/11, 2012 at 16:14 Comment(1)
The link to that emacs-ide shell script is brokenAguste
L
0

Using

open -a -n /Applications/Emacs.app

failed for me with a popup "Emacs quit unexpectedly." (OS X Yosemite 10.10.3, Emacs 24.3.1 x86_64-apple-darwin)

As a workaround, I installed Aquamacs as well as Emacs, so at least I can have two copies running.

Leund answered 20/3, 2018 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.