8

I have several script that will launch all the apps and files related to a specific project. But, it will launch multiple emacs instances, rather than simply cause the current emacs to open the requested files. I'd rather the current emacs simply opened the project text files in a new buffer.

Any ideas how I can do that?

17

M-x server-start inside the Emacs session, then use emacsclient -n file1 file2 ... to add files to the existing Emacs. There are additional options you might want to use, e.g. -c to open the files in a new window (frame).

  • thanks dood. That's what I need to know. I'm not checking 'right answer' yet, cause I heard that you should always keep it open for a bit to give others a chance to contribute. But I've already put your answer into some scripts. – bev Mar 12 '11 at 17:47
  • Normally, you'd want to put (server-start) in your .emacs file, rather than starting the server manually. I use (and window-system (server-start)) to avoid starting the server if I'm running emacs in a terminal. – cjm Mar 12 '11 at 17:57
  • In modern Emacs it's just as useful in a terminal; see the -t option. – geekosaur Mar 12 '11 at 17:58
  • @cjm - actually I do start the server in my .emacs file. I just didn't use it :-) – bev Mar 12 '11 at 18:52
4

Put (server-start) in your .emacs file.

Add this to ~/.bashrc

alias myedit='emacsclient --alternate-editor="" --no-wait $*' #quotes intentionaly left blank

then use myedit as your editor. You will have to use the -c option to bring up a window.

So you may do this:

myedit -c a-file
run-script #that uses myedit

or

run-script #that uses myedit
myedit -c
  • Richard, thanks for the input. I'm still puzzling out a few things with your script. First, since emacsclient doesn't have a -c option, I'm assuming that it's a bash thing, but don't know. Second, why not put emacs as the alternate editor? Thanks – bev Apr 6 '11 at 19:57
  • #$ emacsclient --help .... The following OPTIONS are accepted: ... -c, --create-frame Create a new frame instead of trying to use the current Emacs frame ... -a EDITOR, --alternate-editor=EDITOR Editor to fallback to if the server is not running If EDITOR is the empty string, start Emacs in daemon mode and try connecting again – ctrl-alt-delor Apr 26 '11 at 10:13
  • You do not need (and, in fact, probably do not want) to use positional parameters in the definition of an alias. Consider how alias foo='echo bar $*' expands in set quux; foo baz. If you want to use the positional parameters, try it as a shell function: myedit() { emacsclient … "$@"; } – Chris Johnsen May 18 '11 at 5:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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