How to run a emacs without loading `.emacs` nor `.emacs.d`? - macos

When I have a trouble with Emacs settings (that is quite often), I move setting directory and restart emacs.
But I think there is a better way to do it.
I'm using emacs 24 in osx. How can I launch Emacs without loading user settings?

The -q option (alias for lets you invoke Emacs from the command line without running the startup script.
The other relevant options are:
--no-init-file, -q load neither ~/.emacs nor default.el
--no-site-file do not load site-start.el
--quick, -Q equivalent to -q --no-site-file --no-splash
On OS X, you can invoke Emacs like this:
/Applications/Emacs.app/Contents/MacOS/Emacs -Q ~/myfile.txt &

Related

How to start emacs from the terminal with an emacs command that runs soon after emacs is opened

Is there a way to launch Emacs from terminal and execute some Emacs command automatically soon after Emacs is launched (the command to be executed inside of emacs is provided along with the Emacs-launching command executed from the shell.)
What I want to do exactly is to have a command to launch Emacs and then open a new empty buffer and activate org mode inside of this buffer.
I want something that might look like this
emacs -fs --command="evil-buffer-new && org-mode"
I want the -fs flag because I want Emacs to open in full-screen in this case.
Update
--eval flag didn't work. Forget about evil-buffer-new, I have tried something as simple as:
emacs --eval="(org-mode)" txt.txt
txt.txt is an empty text file created before executing the above command (and please don't ask me why I didn't use .org file extension).
after Emacs opened, org-mode wasn't active. I had to run pp-eval-expression then (org-mode) to activate it, and then it worked.
Am I missing something here? How about rephrasing the question like this:
How to open an empty text file (having .txt file extension) with Emacs from the terminal and have org-mode activated in that buffer automatically?
See C-hig (emacs)Action Arguments or even just run emacs --help -- there are several options for loading and evaluating arbitrary code.
--command="evil-buffer-new && org-mode"
More like:
--eval="(progn (evil-buffer-new) (org-mode))"
But you'll have to figure it out for yourself, because I don't know what evil-buffer-new is specifically.
You told an empty file is created before emacs is started. But instead of an empty file could you create a file with file-local mode variable specifying the org mode ? For example with bash:
#!/bin/bash
cat <<EOF >> "$1"
; -*- mode: Org;-*-
EOF
emacs "$1" &
Now the mode is always resolved correctly with normal major mode selection procedure.

emacs automatically open in mac

I was setting up with emacs on my macbook. In order to open the emacs from command line, I was follow other's suggestion to add an emacs script to my /usr/bin.
Now I can open graphic emacs from command line, but the problem is that every time when I open the terminal, the emacs is automatic run. I don't know why this happened.
Here is the script I added:
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$#"
I also use chmod +x /usr/bin/emacs after the script was added.
Please tell me what cause the problem happens.
If your using the standard OS X terminal, look under preferences->profile and the shell tab and make sure there isn't something set in the run on startup option.
If your using another terminal, such as iTerm, check the profiles.

How to launch GUI Emacs from command line in OSX?

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.
Call the following script "emacs" and put it in your PATH somewhere:
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$#"
That covers #2, #3, and #4.
For #1, put this somewhere in your .emacs file:
(x-focus-frame nil)
The emacsformacosx.com site now has a How-To page, which is where the top snippet came from. There's more info there about running emacsclient and hooking Emacs up to git mergetool.
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)
alias emacs='open -a /Applications/Emacs.app $1'
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:
alias ec='/Applications/Emacs.app/Contents/MacOS/bin/emacsclient'
Then add the following to your .emacs file to start the emacs server (which receives the emacsclient calls)
;;========================================
;; start the emacsserver that listens to emacsclient
(server-start)
Then you can type
ec .bashrc
to load a copy of .bashrc into an existing emacs session!
This improves on David Caldwell's answer by starting Emacs in the background:
#!/bin/sh
$(/Applications/Emacs.app/Contents/MacOS/Emacs "$#") &
As stated in the other answer, this covers #2, #3, and #4. For #1, put this somewhere in your .emacs file: (x-focus-frame nil).
Note that the following does not work for me -- it does not start Emacs in a directory specified on the command line (e.g. emacs .)
# NOT RECOMMENDED
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$#" &
I assume you either:
Start the emacs daemon on login
Have (server-start) in your .emacs
Don't mind having lots of separate copies of emacs running
If so, then I think this satisfies the original four criteria, plus one more:
The emacs window opens in front of my terminal window.
it will always open to the foreground (with x-focus-frame).
Typing "emacs" launches a GUI Emacs window. Finding files in that window will default to looking in the directory from where I started Emacs.
It will open an existing emacs window in dired mode.
Typing "emacs foo.txt" when foo.txt exists launches a GUI Emacs window with foo.txt loaded.
If emacs is already running and has a server, then it will open in the existing window and come to the foreground.
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.
Correct.
One extra:
Control returns to the terminal session immediately after typing the command.
~/bin/emacs
#!/bin/bash
EMACSPATH=/Applications/Emacs.app/Contents/MacOS
# Check if an emacs server is available
# (by checking to see if it will evaluate a lisp statement)
if ! (${EMACSPATH}/bin/emacsclient --eval "t" 2> /dev/null > /dev/null )
then
# There is no server available so,
# Start Emacs.app detached from the terminal
# and change Emac's directory to PWD
nohup ${EMACSPATH}/Emacs --chdir "${PWD}" "${#}" 2>&1 > /dev/null &
else
# The emacs server is available so use emacsclient
if [ -z "${#}" ]
then
# There are no arguments, so
# tell emacs to open a new window
${EMACSPATH}/bin/emacsclient --eval "(list-directory \"${PWD}\")"
else
# There are arguments, so
# tell emacs to open them
${EMACSPATH}/bin/emacsclient --no-wait "${#}"
fi
# Bring emacs to the foreground
${EMACSPATH}/bin/emacsclient --eval "(x-focus-frame nil)"
fi
On Mountain Lion, I am using Yamamoto Mitsuharu's port https://github.com/railwaycat/emacs-mac-port with the following alias:
alias emacs=/Applications/Emacs.app/Contents/MacOS/Emacs
and it satisfies all of your criteria.
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
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
open -a /Applications/Emacs.app <file-name>
combining this with David Jame's response I've created the following emax bash script and placed it in my path at ~/bin
#!/bin/bash
(open -a /Applications/Emacs.app "$#") &
Caveat: in order to get emacs to open the current directory in Dired by name mode, you need to use
emax .
Environment:
OS X Yosemite Version 10.10.2
GNU Emacs 24.4.2 (x86_64-apple-darwin14.0.0, NS apple-appkit-1343.14)
of 2014-11-13
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.
ec() {
emacsclient -n $# 2> /dev/null
if [[ $? == 1 ]]; then
open -a Emacs.app -- $#
fi
}
Usage
ec file [...]
Let's unpack what's happening:
pass all the ec arguments to emacsclient and don't (-n) wait for emacs before continuing.
If Emacs is already running, we're all done and you're editing.
swallow up the error message posted by emacsclient when there's no emacs running. (2> /dev/null)
Manually handle the exit code 1 ([[ $? == 1 ]])
open Emacs.app and pass file arguments to it (paths will be correctly opened.)
You're all done, and Emacs has opened your files.
The other answers here didn't quite work for me. In particular, on my machine, the bash script
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$#"
always opens emacs in the home directory. To get it to open in the current working directory, I had to do
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$PWD/$#"
instead.
Compile Emacs according to the following steps:
./configure --with-x --prefix=/usr
make
sudo make install
And your done! It may help to download and install XQuartz, but that's just my opinion.
This is my script for open emacs/emacsclient on osx.
#!/bin/bash
# Ensure (server-start) is added in your emacs init script.
EMACS=/Applications/MacPorts/Emacs.app/Contents/MacOS/Emacs
EMACSCLIENT=/Applications/Macports/Emacs.app/\
Contents/MacOS/bin/emacsclient
# test if client already exsit.
$EMACSCLIENT -e "(frames-on-display-list)" &>/dev/null
# use emacsclient to connect existing server.
if [ $? -eq 0 ]; then
$EMACSCLIENT -n "$#"
# open emacs.app instead.
else
`$EMACS "$#"` &
fi
In all of the above when using "open" - make sure you use the "--args" option
Do not do this:
alias emacs='open -a /Applications/Emacs.app $1'
Instead this:
alias emacs='open -a /Applications/Emacs.app --args $1'
the --args option prevents "open" from consuming various options intended for Emacs.
The top answer is good, but I wanted the emacs process to run in the background so I could still use my shell. This answer appeared to do what I wanted, but didn't start emacs in the right directory, meaning absolute paths were required (or hacks to append pwd to the paths which wouldn't work in all cases). Furthermore, simply using & meant that if I killed the terminal, emacs would also be killed.
I decided to use screen and a bash function, and the following solution works for me on macOS 10.15.6 and emacs 26.2 installed with brew:
function emacs() {
screen -d -m /Applications/Emacs.app/Contents/MacOS/Emacs "$#"
}
For the meaning of the -d -m command line flags, they have a special meaning when used together and so can essentially be thought of as one command line flag. The explanation is in the manpage:
Start screen in "detached" mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.
open_emacs() {
num=$(ps aux | grep -E "[E]macs-x86_64-10_14 --|[e]macs --" | wc -l)
if [ $num -eq 0 ]; then
echo "## starting emacs"
# Run in a subshell to remove notifications and close STDOUT and STDERR:
(&>/dev/null emacsclient -t -q &)
fi
}
alias e="open_emacs"
Following line (&>/dev/null emacsclient -t -q &) will start the emacs daemon if it is not running on the background.
macOS may have defined the app name starting with E (ex: Emacs-x86_64-10_14.app), based on that you can check whether the emacs daemon running on the background or not.
Just want to update a response to this question. Since it is still a relevant question, but now there is an easier solution:
brew install --cask emacs
When this installs Emacs, it does the behavior you requested, without further intervention. It even runs the Emacs Server on startup.
Files installed/linked by default:
ebrowse -> /Applications/Emacs.app/Contents/MacOS/bin/ebrowse
emacs -> /Applications/Emacs.app/Contents/MacOS/Emacs
emacsclient -> /Applications/Emacs.app/Contents/MacOS/bin/emacsclient
etags -> /Applications/Emacs.app/Contents/MacOS/bin/etags
BTW, this is now a recommended way of installing Emacs on MacOS:
https://www.gnu.org/software/emacs/download.html#nonfree

Mac os x: How to have makefile reference /Applications/application

OS Mac OS X 10.6.6
emacs 23.2.1
I'm currently trying to install magit on carbon emacs using their proposed make solution. I'm an almost complete makefile newby so I might be on the complete wrong path altogether.
Makefile displays:
%.elc: %.el
$(BATCH) --eval '(byte-compile-file "$<")'
which gets expanded to:
emacs -batch -q -no-site-file -eval "(setq load-path (cons (expand-file-name \".\") load-path))" --eval '(byte-compile-file "magit.el")'
What I do understand. However it comes back with:
Cannot open load file: subst-ksc
What after a bit of googling reveals some missing libraries. I also realized that the emacs referenced in the expanded command is pointing to /usr/bin/emacs which is one big file, I don't see the normal lisp / site-lisp / etc directories. I also know that my prefered emacs is carbon emacs located in /Application/Emacs.app which just opens when clicking on it, or alternatively, I can open it in with the terminal:
open /Application/MyApp.app
Finally the title question: How can I make Makefile using the Emacs.app on the applications directory? I hope someone here knows the answer, or can point me to alternatives.
regards,
Jeroen.
bash-3.2$ EMACSLOADPATH=/usr/share/emacs/22.1/site-lisp:/usr/share/emacs/22.1/lisp emacs - batch -eval '(print "hi")'
"hi"
bash-3.2$ emacs -batch -eval '(print "hi")'
Cannot open load file: subst-ksc
bash-3.2$
bash-3.2$ emacs -batch -eval '(print emacs-version)'
Cannot open load file: subst-ksc
bash-3.2$ unset EMACSLOADPATH
bash-3.2$ emacs -batch -eval '(print emacs-version)'
"22.1.1"
I had the same error Cannot open load file: subst-ksc on OS X Maverick running
cask exec emacs --no-site-file --no-site-lisp --batch \
\
-f batch-byte-compile projectile.el
I solved it with the method given here.
Replace the /usr/bin/emacs file by the following script:
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$#"
With that manner the emacs command will run your actual Emacs.

How can I open Emacs on Mac OS X such that it is loaded with my $PATH?

I'm running Snow Leopard, and trying to run Emacs such that when I start it up, the output of (getenv "PATH") is the same as the output in Terminal.app of echo $PATH.
In other words, I want to start up Emacs from /Applications/Emacs.app and have it start with my $PATH. I haven't been able to figure out how to do this within emacs, or with how I start emacs up. So I've spent most of my effort trying to come up with a shell script that I can wrap with something like Platypus or Appify.
So the closest thing I have right now is:
echo MYPASSWD | sudo -S -u USERNAME -i nohup /Applications/Emacs.app/Contents/MacOS/Emacs > /dev/null &
which fails because it seems that nohup throws away my $PATH, despite the -i flag. The following does not throw away my PATH but open a superfluous Terminal.app:
echo MYPASSWD | sudo -S -u USERNAME -i open /Applications/Emacs.app/Contents/MacOS/Emacs > /dev/null &
I've tried running this through do shell script in an AppleScript, also to no avail.
Am I missing something basic? It doesn't seem like this should be hard. Thanks!
I wrote a little elisp a while ago to parse the output of env and apply it to the Emacs environment because I didn't want to maintain a plist. The code's at http://paste.lisp.org/display/111574.
GUI applications are not launched by a traditional shell and do not inherit environment variables from the usual places like .profile, .bash_profile, etc.
http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPRuntimeConfig/Articles/EnvironmentVars.html%23//apple_ref/doc/uid/20002093-BCIJIJBH explains that ~/.MacOSX/environment.plist is the place you probably want to add your favorite equivalent of $PATH.
I ran into the same problem and here's how I worked around it. I created ~/.bashrc and populated it with:
source /etc/profile
source ~/.profile
You have to export the PATH to the location of the .app so it would be something like this
export PATH=$PATH:/path/to/the/program
That should allow you to just type EMACS and it will start up.

Resources