Run GUI Applications from Terminal with root privilege So it has been well documented that GUI applications (like gedit or textedit) should NOT be run with sudo. Ubuntu et al get gksu and gksudo (and the like) so question: what do WE (Mac users) get? Given that the Darwin kernel is built on some *BSD code, I assume the same issues apply, but how do we go around this?
To edit /etc/hosts with Sublime Text:
sudo /Applications/Sublime\ Text.app/Contents/MacOS/Sublime\ Text /etc/hosts

If you have to do this on the regular basis, you can add this snippet to your ~/.bash_profile

#   sudoapp: Runs .app with root privileges
#   Usage: sudoapp /Applications/Name.app /etc/hosts
#   --------------------------------------------------------------------
    sudoapp () {
        sudo "$1/Contents/MacOS/$(defaults read "$1/Contents/Info.plist" CFBundleExecutable)" $2
    }


Apps running with root privileges will use /private/var/root as home folder, thus all config and temporary files owned by root that will be created in the process will stay where they should be - in the root home directory.
This is the same as logging in as root and running the app, but without the hassle of user switching.

This method works on 10.6 — 10.11

Update: Apple's own TextEdit refuses to start if run as root in 10.11 and newer, so I changed my example to use Sublime Text instead