Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Mac terminal.app: starting emacs, possibly as sudo

66 views
Skip to first unread message

Marius Hofert

unread,
Oct 16, 2011, 6:26:17 PM10/16/11
to Emacs help
Hi,

If I start "emacs" from the Mac terminal (Terminal.app), I get a rather old GNU Emacs 22.1.1 (/usr/bin/emacs) instead of the wonderful new GNU Emacs 23.3 I installed.
1) How can I have the new emacs version opened when typing "emacs" in the terminal?
2) Is it possible to start emacs in "sudo mode", so starting emacs via "sudo emacs" in the terminal (and then being root when opening a shell from within emacs)?
I know I can have 1) by typing "open -a Emacs.app", but that does not allow 2).

Cheers,

Marius

Perry Smith

unread,
Oct 16, 2011, 7:26:48 PM10/16/11
to Marius Hofert, Emacs help
This may take a little experimentation. I have emacs 23.something compiled for Mac. I have an application bundle in /Applications called Emacs.app.

If I run /Applications/Emacs.app/Contents/MacOS/bin/emacs, it starts up in the terminal. If I run /Applications/Emacs.app/Contents/MacOS/Emacs it runs as a GUI (like open -a Emacs.app).

By running the bin/emacs, you can do sudo before hand and it will give you a root shell prompt (I just tested it.) Be aware that it will be using root's .emacs or .emacs.d files -- not yours.

The final thing you need is to either make an alias in your .bashrc file -- something like:

alias emacs=/Applications/Emacs.app/Contents/MacOS/bin/emacs

or you can change your PATH to point to /Applications/Emacs.app/Contents/MacOS/bin before /usr/bin

I'm not sure the alias will work with sudo. Changing the path might be better. aliases are weird critters.

HTH,
pedz


Peter Dyballa

unread,
Oct 16, 2011, 7:43:26 PM10/16/11
to Marius Hofert, Emacs help

Am 17.10.2011 um 00:26 schrieb Marius Hofert:

> 1) How can I have the new emacs version opened when typing "emacs" in the terminal?

Either reorder PATH or use the whole path to the executable! Or use a shell alias...

> 2) Is it possible to start emacs in "sudo mode", so starting emacs via "sudo emacs" in the terminal (and then being root when opening a shell from within emacs)?

It won't work with the NS and "AppKit" variants (do you know of any "native" mac OS X GUI application that you can launch with sudo and then runs with elevated privileges?), it works with the X client and also without windowing system (in terminal).

--
Mit friedvollen Grüßen

Pete

If all else fails read the instructions.
- Donald Knuth


Marius Hofert

unread,
Oct 17, 2011, 3:34:22 AM10/17/11
to Peter Dyballa, Perry Smith, Emacs help
Dear Perry, Dear Peter,

thanks a lot for helping.

I tried sudo /Applications/Emacs.app/Contents/MacOS/bin/emacs and I am indeed root in the shell :-)
There are some things that are strange, though: Option/Alt is not Meta anymore, so when I wanted to start a shell with M-x ... I obtained a "=" symbol (or rather, an "approximate" symbol). I realized that ESC is now Meta.
=> Are there any other significant changes I should be aware of?
=> Is it possible to get the same/similar behavior as in the GUI, maybe through simply redefining root's .emacs and .emacs.d as my own? (bad practice?)

Well, maybe I should rethink the whole process: When do I want to open emacs as root? From time to time, I open a file and modify it. When I try to save it, I get the read-only warning. This means I would like to be su to modify this file. It's probably best to just open it with C-x C-f /sudo::path-to-file... But by closing and reopening all my modifications to the file are lost. How can one avoid this?


Cheers,

Marius

Michael Albinus

unread,
Oct 17, 2011, 4:07:08 AM10/17/11
to Marius Hofert, Emacs help
Marius Hofert <marius...@math.ethz.ch> writes:

> Dear Perry, Dear Peter,

Hi,

> Well, maybe I should rethink the whole process: When do I want to open
> emacs as root? From time to time, I open a file and modify it. When I
> try to save it, I get the read-only warning. This means I would like
> to be su to modify this file. It's probably best to just open it with
> C-x C-f /sudo::path-to-file... But by closing and reopening all my
> modifications to the file are lost. How can one avoid this?

If you edit file /a/b/c and you cannot save it because it is owned by
root, you could save it by "C-x C-w /sudo::/a/b/c".

> Cheers,
>
> Marius

Best regards, Michael.

Marius Hofert

unread,
Oct 17, 2011, 4:29:05 AM10/17/11
to Michael Albinus, Emacs help
Dear Michael,

1) thanks a lot, that will definitely be useful in the future!

2) Concerning my last question: Is there any harm in "replacing" /usr/bin/emacs by /Applications/Emacs.app/Contents/MacOS/Emacs? This way one can easily use "sudo emacs" or "emacs -Q"... and start the GUI emacs with these features.

Cheers,

Marius

Michael Albinus

unread,
Oct 17, 2011, 4:32:57 AM10/17/11
to Marius Hofert, Emacs help
Marius Hofert <marius...@math.ethz.ch> writes:

> Dear Michael,

Hi Marius,

> 2) Concerning my last question: Is there any harm in "replacing"
> /usr/bin/emacs by /Applications/Emacs.app/Contents/MacOS/Emacs? This
> way one can easily use "sudo emacs" or "emacs -Q"... and start the GUI
> emacs with these features.

No idea, I do not use Mac.

Mario Lassnig

unread,
Oct 17, 2011, 4:49:33 AM10/17/11
to
On 11-10-17 10:32 , Michael Albinus wrote:
> Marius Hofert<marius...@math.ethz.ch> writes:
>
>> Dear Michael,
>
> Hi Marius,
>
>> 2) Concerning my last question: Is there any harm in "replacing"
>> /usr/bin/emacs by /Applications/Emacs.app/Contents/MacOS/Emacs? This
>> way one can easily use "sudo emacs" or "emacs -Q"... and start the GUI
>> emacs with these features.
>
> No idea, I do not use Mac.
>

I've been symlinking like this for ages now, and it works without a hitch.

>> Cheers,
>>
>> Marius
>
> Best regards, Michael.
>

Cheers,
Mario

Marius Hofert

unread,
Oct 17, 2011, 5:13:09 AM10/17/11
to Michael Albinus, Emacs help
okay, I tried:

cd /usr/bin
sudo mv ./emacs ./emacs.old # move pre-installed emacs
sudo ln -s /Applications/Emacs.app/Contents/MacOS/emacs emacs

Starting "emacs" from a new terminal then opened emacs, but ended up in a mess:
localhost:~ mhofert$ emacs
Warning: arch-dependent data dir (/Users/david/src/emacs-dev/ftp-versions/emacs-23.3/nextstep/Emacs.app/Contents/MacOS/libexec/emacs/23.3/x86_64-apple-darwin/) does not exist.
Warning: arch-independent data dir (/Users/david/src/emacs-dev/ftp-versions/emacs-23.3/nextstep/Emacs.app/Contents/Resources/share/emacs/23.3/etc/) does not exist.
Error: charsets directory (/Users/david/src/emacs-dev/ftp-versions/emacs-23.3/nextstep/Emacs.app/Contents/Resources/share/emacs/23.3/etc/charsets) does not exist.
Emacs will not function correctly without the character map files.
Please check your installation!
Warning: Could not find simple.el nor simple.elc

Any ideas?
/Applications/Emacs.app/Contents/MacOS/emacs works fine, though.

Cheers,

Marius

On 2011-10-17, at 10:32 , Michael Albinus wrote:

> Marius Hofert <marius...@math.ethz.ch> writes:
>
>> Dear Michael,
>
> Hi Marius,
>
>> 2) Concerning my last question: Is there any harm in "replacing"
>> /usr/bin/emacs by /Applications/Emacs.app/Contents/MacOS/Emacs? This
>> way one can easily use "sudo emacs" or "emacs -Q"... and start the GUI
>> emacs with these features.
>
> No idea, I do not use Mac.
>

Mario Lassnig

unread,
Oct 17, 2011, 5:37:53 AM10/17/11
to
On 11-10-17 11:13 , Marius Hofert wrote:
> okay, I tried:
>
> cd /usr/bin
> sudo mv ./emacs ./emacs.old # move pre-installed emacs
> sudo ln -s /Applications/Emacs.app/Contents/MacOS/emacs emacs
>
> Starting "emacs" from a new terminal then opened emacs, but ended up in a mess:
> localhost:~ mhofert$ emacs
> Warning: arch-dependent data dir (/Users/david/src/emacs-dev/ftp-versions/emacs-23.3
>/nextstep/Emacs.app/Contents/MacOS/libexec/emacs/23.3/x86_64-apple-darwin/) does not exist.
> Warning: arch-independent data dir (/Users/david/src/emacs-dev/ftp-versions/emacs-23.3
> /nextstep/Emacs.app/Contents/Resources/share/emacs/23.3/etc/) does not exist.
> Error: charsets directory (/Users/david/src/emacs-dev/ftp-versions/emacs-23.3/nextstep
> /Emacs.app/Contents/Resources/share/emacs/23.3/etc/charsets) does not
exist.
> Emacs will not function correctly without the character map files.
> Please check your installation!
> Warning: Could not find simple.el nor simple.elc
>
> Any ideas?
> /Applications/Emacs.app/Contents/MacOS/emacs works fine, though.
>

Ha! Silly me. I just found out that I'm actually not using the
/usr/bin/emacs -> /Applications/Emacs.app/Contents/MacOS/Emacs symlink
at all. It breaks for me as well like that.

That's what I'm actually using:
alias emacs='/Applications/Emacs.app/Contents/MacOS/emacs -nw'

Completely forgot about that.

Cheers,
Mario

Peter Dyballa

unread,
Oct 17, 2011, 6:34:36 AM10/17/11
to Marius Hofert, Emacs help

Am 17.10.2011 um 09:34 schrieb Marius Hofert:

> => Are there any other significant changes I should be aware of?

C-h i m Emacs RET and then navigate to the documentation of your GNU Emacs variant. Google is another option.

> => Is it possible to get the same/similar behavior as in the GUI, maybe through simply redefining root's .emacs and .emacs.d as my own? (bad practice?)

Root can launch its Emacs with the -u or --user option.

> But by closing and reopening all my modifications to the file are lost. How can one avoid this?

Then don't close it! With a few GB of swap you can keep a few (million) files open.

--
Mit friedvollen Grüßen

Pete

The problem with the French is that they don't have a word for « entrepreneur ».
– Georges W. Bush


Peter Dyballa

unread,
Oct 17, 2011, 6:36:00 AM10/17/11
to Marius Hofert, Emacs help

Am 17.10.2011 um 09:37 schrieb Marius Hofert:

> Oh, and something else: Instead of adding /Applications/Emacs.app/Contents/MacOS/bin/emacs to PATH (before /usr/bin), can't one just remove /usr/bin/emacs and create a link to /Applications/Emacs.app/Contents/MacOS/bin/emacs?
> There are also emacs-undumped and emacsclient in /usr/bin, not sure what they are for, though.

I wouldn't try to destroy the system. (But you're not me!)

--
Greetings

Pete 0
%-/\_//
(*)(*)


Peter Dyballa

unread,
Oct 17, 2011, 6:38:12 AM10/17/11
to Marius Hofert, Emacs help, Michael Albinus

Am 17.10.2011 um 11:13 schrieb Marius Hofert:

> Any ideas?

Try to understand the error messages! Record your understanding by writing a small notice or memo!

--
Greetings

Pete

Windows, c'est un peu comme le beaujolais nouveau: à chaque nouvelle cuvée on sait que ce sera dégueulasse, mais on en prend quand même, par masochisme.


Perry Smith

unread,
Oct 17, 2011, 9:33:08 AM10/17/11
to Marius Hofert, Emacs help
Marius,
I would agree with Peter. I try to not modify "Apple" stuff but find ways to work around it. I have no idea of some weird script somewhere uses Apple's stock emacs. The other obstacle is at some point, Apple will update the system, install a new emacs, and you will lose your changes.

As far as the keyboard: you are now going through two layers. When you type a key, the Terminal is interpreting that keystroke. I am sure that is slightly customizable but how much, I don't know. Then Terminal sends it to emacs as a sequence of keys. I believe you have most the modifiers at that point. Here is an example: with the GUI emacs, when you hold the control key down and hit an "a", emacs knows that entire sequence of key strokes. But when emacs is running inside Terminal, Terminal knows and sees the whole key sequence but sends just a Control-A to emacs. There will be a few dramatic impacts and probably a few subtle ones too.

So, in particular, how the "chord" keys work, option, command, control, and shift (and also left and right shift, left and right command, etc) is controlled more by Terminal than it is by emacs. I hope all that makes sense.

Good luck,
pedz


Perry Smith

unread,
Oct 17, 2011, 9:34:58 AM10/17/11
to Michael Albinus, Emacs help
On Oct 17, 2011, at 3:07 AM, Michael Albinus wrote:

Marius Hofert <marius...@math.ethz.ch> writes:

If you edit file /a/b/c and you cannot save it because it is owned by
root, you could save it by "C-x C-w /sudo::/a/b/c".

Now there's a clever man... I would have saved it off, opened it with sudo,
and pasted the changes in.  I'd never thought to just save the changes
via sudo.


Stefan Monnier

unread,
Oct 17, 2011, 10:04:19 AM10/17/11
to
> 2) Is it possible to start emacs in "sudo mode", so starting emacs via
> "sudo emacs" in the terminal (and then being root when opening a shell from
> within emacs)?

I recommend you don't run large applications such as Emacs as root.
You can edit root-owned files from Emacs using Tramp's (aka
"/sudo::<filename>"), which is usually more convenient.
[ Tho I must admit I probably use Zile more often than Emacs to edit
root-owned files. ]


Stefan

Peter Dyballa

unread,
Oct 17, 2011, 11:28:20 AM10/17/11
to Perry Smith, Emacs help, Michael Albinus
You're opening and changing and saving that one file with "elevated privileges", just as some native Mac OS X application would when it asks you for the password. (Don't know what happens with the backup copy /a/b/c~ which GNU Emacs produces. And how the process in reality proceeds; it's easy to rename the file as the backup copy and open it afterwards in a buffer that has been renamed, so that you'll be saving saving it as a new file – which makes handling of date stamps very easy.)

--
Greetings

Pete

"Klingons do not believe in indentation - except perhaps in the skulls of their project managers."


Peter Dyballa

unread,
Oct 17, 2011, 11:18:46 AM10/17/11
to Perry Smith, Emacs help

Am 17.10.2011 um 15:33 schrieb Perry Smith:

> The other obstacle is at some point, Apple will update the system, install a new emacs, and you will lose your changes.

And this newly installed /usr/bin/emacs might follow down the sym-link…

> I am sure that is slightly customizable but how much, I don't know.

There are two areas: one in the preferences of the Apple Terminal, the other by choosing a value for the environment variable TERM – although the latter is rather meant for the software running in that terminal to tell it how to make sense of the ANSI and non-ANSI Esc sequences produced in that terminal.

> Here is an example: with the GUI emacs, when you hold the control key down and hit an "a", emacs knows that entire sequence of key strokes.

Not exactly. The GUI application will know that some modifier key(s) is/are pressed when a key event happens.

> But when emacs is running inside Terminal, Terminal knows and sees the whole key sequence but sends just a Control-A to emacs.

Which is one byte of the hex value 0x1 or octal \001 or decimal 1 (while the figure 1 has an ASCII value of 49 [o61, x31]).

--
Greetings

Pete

Let's face it; we don't want a free market economy either.
– James Farley, president, Coca-Cola Export Corp., 1959


Marius Hofert

unread,
Oct 17, 2011, 4:52:53 PM10/17/11
to Peter Dyballa, Emacs help
Dear Perry, Dear Peter,

thanks for your detailed explanations.
I finally used put in "export PATH=/Applications/Emacs.app/Contents/MacOS/:$PATH" in ~/.profile, so now I can call "emacs" from within the terminal (e.g., via sudo) and get the desired result.

Cheers,

Marius

PS: In the meantime, I also found a similar question/problem here:
http://superuser.com/questions/199570/warnings-errors-with-emacs-cocoa-23-2-on-osx-terminal

On 2011-10-17, at 12:36 , Peter Dyballa wrote:

>
> Am 17.10.2011 um 09:37 schrieb Marius Hofert:
>
>> Oh, and something else: Instead of adding /Applications/Emacs.app/Contents/MacOS/bin/emacs to PATH (before /usr/bin), can't one just remove /usr/bin/emacs and create a link to /Applications/Emacs.app/Contents/MacOS/bin/emacs?
>> There are also emacs-undumped and emacsclient in /usr/bin, not sure what they are for, though.
>
> I wouldn't try to destroy the system. (But you're not me!)
>
0 new messages