Start Vim as the user with root privileges

permissionsrootusers

I have my custom Vim files in ~/.vim and settings in ~/.vimrc. However, sometimes I have to edit some files in /etc and such.
If I start Vim like this:

$ sudo vim /etc/rc.conf

I lose my config since Vim uses its default one. So: how can I run Vim with root privileges to edit files without losing my user's settings (which are in my home directory)?
I have tried:

$ su username -c "vim /usr/lib/python2.7/setuptools/dist.py"

but Bash gives me Permission denied. However, the above command works for example for: /etc/acpi/handler.sh. Why is that?

Note: username is not root.

Best Answer

Instead of sudo vim /etc/rc.conf use sudoedit /etc/rc.conf or sudo -e /etc/rc.conf. You may need to set the EDITOR environment variable to vim. This will run vim itself as the normal user, using your normal configuration, on a copy of the file which it will copy back when you exit.

Related Question