6

I work on a Mac 10.9.5 and use Emacs 24.4 (installed with macports).

This question is related to https://stackoverflow.com/questions/27778836/customizing-emacs-open-new-files-tabs-and-properly-storing-back-up-files?noredirect=1#comment43984459_27778836 .There I was advised to split the "whole-question" into different questions, and also post it here, so here is the 1st of 3 emacs related questions, , hopefully in a better format and in a more adequate stack exchange.

open an empty, brand new file in a new Emacs window, from terminal.

at the moment, when I type in my terminal (or XQuartz), for example:

>emacs new.py &
The file ~/new.py does not exist.

which, effectively, does not exist, but I would like Emacs to open an empty file called new.py. Of course, I can always get around by typing:

>emacs &

then start coding and save it as new.py. I simply would like to be able to do that from terminal. Also, once emacs is open, if I type >emacs& on terminal, I "land" in the file already open and I have to do : Cmd-N + C-x C-f to open a brand new file. So my first question is: Is there something to be added to my .emacs file so I open a brand new file from terminal?

6
  • Do we understand the emacs & command the same way? Launch command in background?
    – Nsukami _
    Commented Jan 6, 2015 at 13:22
  • It should just work. When I type emacs newfile.txt -Q (the -Q to prevent loading the init file to confirm that it's vanilla Emacs), Emacs pops open with a buffer for newfile.txt.
    – Dan
    Commented Jan 6, 2015 at 13:35
  • Hi Nsukami. Thanks for answering. Ignore the & if you want. Typing emacs new.py without the & does return the message new.py does not exist as well. For example, the fink emacs installation seems to do it by default.
    – Javier
    Commented Jan 6, 2015 at 13:36
  • @Dan, thanks. However, emacs newfile.txt -Q returns open: invalid option -- Q
    – Javier
    Commented Jan 6, 2015 at 13:42
  • Do you have emacs defined as an alias or a shell script? Seems like you are not invoking Emacs directly.
    – glucas
    Commented Jan 6, 2015 at 14:22

2 Answers 2

2

I suspect this has to do with the way Emacs is being packaged as an OS X GUI app (MacPorts) and called via an alias. I'm not on an OS X machine at the moment but it looks like the arguments you are passing from the shell are being processed by the OS X opencommand rather than Emacs itself.

The wiki page here http://www.emacswiki.org/emacs/EmacsForMacOS#toc20 suggests that what you want to do is:

open -a /Applications/MacPorts?Emacs.app <file-name> 

A potentially nicer option would be to start the Emacs GUI app and call (server-start) (say, in your init file) to start a server. Then you can use emacsclient from the shell when you want to open something. Take a look at the Emacs Server manual page for details.

EDIT

For future readers who don't skim the comments below, @Javier solved the problem by defining his own emacs alias to call the app directly, e.g. in .tcshr

alias emacs  /Applications/MacPorts/Emacs.app/Contents/MacOS/Emacs 
3
  • Hi glucas, thanks for your answer. Still, open -a /Applications/MacPorts?Emacs.app new.txt returns open: No match, since new.txt does not exist (I want emacs to create it, as if I typed for example, mvim foo.txt). I will dig into the emacsclient as I have never used it, thanks again.
    – Javier
    Commented Jan 6, 2015 at 15:03
  • Looks like a common question, see stackoverflow.com/questions/18808201/… and the other SO thread it references.
    – glucas
    Commented Jan 6, 2015 at 15:09
  • Thanks for the post, problem solved. I just included in my .tcshr file a direct call to the application: alias emacs /Applications/MacPorts/Emacs.app/Contents/MacOS/Emacs and it works. Thanks again glucas
    – Javier
    Commented Jan 6, 2015 at 15:24
-1

You can use:

emacs -a new.py

This creates the new file and opens it in Emacs window.

1
  • -a is not a legal option here
    – Qudit
    Commented Mar 31, 2018 at 5:43

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.