Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I quickly edit root owned files like /etc/hosts and /etc/apache2/httpd.conf in a text editor such as TextEdit which would let me do modern text editor operations, without actually running the editor in sudo -b mode first, or without using nano, vim and other terminal based editors?

Ever since Lion came out, doing

$sudo open -e /etc/hosts

no longer works - the file remains locked when TextEdit opens it.

share|improve this question
up vote 1 down vote accepted

sudo and open provide all the functionality already for editing files of other users so you can just run

SUDO_EDITOR="open -FWne" sudo -e /etc/hosts

to edit /etc/hosts. Make sure to quit Textedit at the end (Cmd-Q) because otherwise sudo won't notice that you're done with editing.

To make life easier add the following to your .bashrc (or .alias if you have it)

alias sudoedit='SUDO_EDITOR="open -FWne" sudo -e'
share|improve this answer
Hmm. Doesn't work for me. This command opens up a sort of temporary clone of hosts in the form of hosts.5o3nev8 and edits that one. If I save it and re-run the command, it opens the same file (changes are there) but a different suffix is at the end. However, no matter what I do via SUDO_EDITOR, if I go and look at the real /etc/hosts, it stays the same - like it was before the first SUDO_EDITOR command. – Swader Jun 17 '12 at 15:51
It opens a temporary file but it should get copied back to the original after you quit Textedit (at least it does for me), see – patrix Jun 17 '12 at 15:59
You are correct. It is worth noting that the TextEditor instance needs to be killed completely in order to overwrite the original - if it stays on but the editing window is closed, the overwrite does not happen. – Swader Jun 17 '12 at 16:07

Here is a step by step solution:

  1. Download the free TextWrangler app ( )

  2. Create a .profile file in your home folder if it does not exist

    $ touch ~/.profile
    $ open -e ~/.profile
  3. Edit the file and give it the following aliases, along with any others you may wish to add:

    alias edit_hosts='sudo open -a /Applications/ /etc/hosts'
    alias edit_vhosts='sudo open -a /Applications/ /etc/apache2/extra/httpd-vhosts.conf'
    alias edit_httpd='sudo open -a /Applications/ /etc/apache2/httpd.conf'
    alias edit_alias='open -a /Applications/ ~/.profile'
    alias reload_alias='. ~/.profile'
  4. Save the file and run:

    $ reload_alias

Now when you run edit_hosts or the other edit_* aliases, the TextWrangler app should open the hosts file. It will still be locked, but this application can unlock it once you start editing, it will prompt you. You now have a one-line command to edit the root owned files of your choice.

You can also alias just the first part of the path:

alias edit='sudo open -a /Applications/'

which will let you execute commands such as

$ edit /etc/hosts

and still achieve the same results.

Edit: other answer looks to be a better solution as it does not require the download and installation of another application.

share|improve this answer
For completeness sake, the reload_alias command won't work unless you source the profile once with . ~/.profile – patrix Jun 17 '12 at 15:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.