How to make a terminal alias that edits root owned files in a GUI text editor

aliasrootterminaltext-editor

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.

Best Answer

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'