2

I wrote a little zsh function to allow me to run emacs from the command-line:

function emacs() {
    /Applications/Emacs.app/Contents/MacOS/Emacs $@ &
}

This works perfectly, but it seems to start emacs as the last window in my Cmd-tab list. Is there any way to make it take focus when it's started?

Jason Baker
  • 8,382
  • 9
  • 34
  • 50

2 Answers2

4

The emacs way to open files into emacs is to use emacsclient from the command line. This requires that emacs is running the emacs server (using M-x server-start or put (server-start) into your .emacs file).

I've got this in my .bashrc (adapt for zsh as appropriate):

alias em='emacsclient -n'

I'm running GNU Emacs 23.1.50.5 (built from CVS last week sometime) and this version brings my emacs window to front.

If you're opening and exiting emacs for each file you edit, you're not taking full advantage of emacs' possibilities. I typically start emacs shortly after a reboot and it stays open until the next Mac software update requires a reboot.

Doug Harris
  • 27,333
  • 17
  • 78
  • 105
2
open -a Emacs.app foo.txt

This will open the file "foo.txt" in Emacs.app. If you want to send command arguments to Emacs, use the args switch.

Richard Hoskins
  • 12,245
  • 10
  • 49
  • 52
  • The problem with this is that I was intending the command-line options ($@) to go to emacs. With this approach, they go to open. – Jason Baker Nov 21 '09 at 18:00
  • Scratch that. Apparently this *does* work if you invoke emacs like this: `open -a /Applications/Emacs.app/Contents/MacOS/Emacs --args $@` – Jason Baker Nov 21 '09 at 18:01
  • I thought the arguments were files you intended to open with the One True Editor. If not, then yes, you need the '--args' switch. – Richard Hoskins Nov 21 '09 at 19:43
  • Hey Jason...did you figure out any way to get this to work without using the open command? – Arunabh Das Sep 19 '11 at 21:04
  • Actually nevermind. I think I can live with using that open command even though I think it is lame and reminiscent of kindergarten. – Arunabh Das Sep 19 '11 at 21:06