How do I launch GUI Emacs from the command line in OSX?
I have downloaded and installed Emacs from http://emacsformacosx.com/.
I'll accept an answer fulfilling all of the following criteria:
- The emacs window opens in front of my terminal window.
- Typing "emacs" launches a GUI Emacs window. Finding files in that window will default to looking in the directory from where I started Emacs.
- Typing "emacs foo.txt" when foo.txt exists launches a GUI Emacs window with foo.txt loaded.
- Typing "emacs foo.txt" when foo.txt does not exist launches a GUI Emacs window with an empty text buffer named "foo.txt". Doing ^X^S in that buffer will save foo.txt in the directory from where I started Emacs.
The other answers here didn't quite work for me. In particular, on my machine, the bash script
always opens emacs in the home directory. To get it to open in the current working directory, I had to do
instead.
Just built emacs with homebrew package manager according to this guide: http://www.emacswiki.org/emacs/EmacsForMacOS with brew install --cocoa emacs After that one should launch the .app version to get gui, which in my case was /usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS/Emacs
In your shell, alias the command 'emacs' to point to the OSX emacs application
In my shell (running the default bash), I have the following (in my .bashrc)
Then, typing emacs on the command line starts the emacs application.
I would, however, recommend that you open a copy of emacs and just keep it up and running. If that's the case, and you want to load a file into an existing copy of emacs, you can use the emacsclient by placing the following in your .bashrc:
Then add the following to your .emacs file to start the emacs server (which receives the emacsclient calls)
Then you can type
to load a copy of .bashrc into an existing emacs session!
Further improving on David James' response the following works for me:
Per instructions to open a file from a terminal found at http://www.emacswiki.org/emacs/EmacsForMacOS#toc20
combining this with David Jame's response I've created the following emax bash script and placed it in my path at ~/bin
Caveat: in order to get emacs to open the current directory in Dired by name mode, you need to use
Environment:
I assume you either:
If so, then I think this satisfies the original four criteria, plus one more:
it will always open to the foreground (with x-focus-frame).
It will open an existing emacs window in dired mode.
If emacs is already running and has a server, then it will open in the existing window and come to the foreground.
Correct.
One extra:
Control returns to the terminal session immediately after typing the command.
~/bin/emacs
Simple solution...
A lot of very complex solutions to this problem are posted here. That's fair because it seems non-trivial.
However, this solution works really well for me.
Usage
Let's unpack what's happening:
ec
arguments to emacsclient and don't (-n
) wait for emacs before continuing.2> /dev/null
)1
([[ $? == 1 ]]
)