xvryda
Last Updated: December 26, 2018
·
20.62K
· oz123

Use your own vimrc when using sudo

I noticed that when I work on Red Hat servers I have my own vimrc when I type:

$ sudo vim /etc/some_conf_file

I like this option to be able to work with my own configuration file on shared servers. I also didn't want to force other administrators to use my own vimrc.
However, this great little feature was missing on some Debian Servers I worked on. So after a little bit of search I found the answer:

$ sudo -E vim /etc/some_conf_file 

What does it do? From the man page:

-E    The -E (preserve environment) option indicates to the security policy that the user wishes to preserve their existing environment variables.  The
      security policy may return an error if the -E option is specified and the user does not have permission to preserve the environment.

Hence, you should use this option with care, and don't use it hastily as an alias.
Know another nifty way to start VIM with your own vimrc without linking /root/.vimrc to ~/.vimrc? I would be happy to know about it!

6 Responses
Add your response

10006

Yes. You can use Vim -u <vimrc> option.

over 1 year ago ·
10007

you mean as user root ?
Could be problematic, because most of the time in environment the HOME partition is mounted with rootsquash. Hence as root I don't have access to my own vimrc in /home/oz/.vimrc

over 1 year ago ·
15383

I've settled on sudoedit as my preferred solution. The editor invoked here is run as the original user rather than as root. Original source here http://stackoverflow.com/a/21488404

over 1 year ago ·
15787

Instead of sudo vim, you can use sudoedit. This is safer — a user can run arbitrary shell commands from vim, so sudo vim can introduce a security risk. sudoedit opens a temporary copy of the file for you to edit with normal vim (with your own vimrc and everything) and then just uses sudo priveledges to replace the file you're editing with the temporary one.

over 1 year ago ·
25984
alias rvim='sudo -E vim'

Problem solved, or like one other commenter said:

alias rvim='sudo vim -u ~/.vimrc'
over 1 year ago ·
26281

vim -u won't help with plugin's, sudo -E seem's like a necessary evil to go with, or you can always sync the user vim conf with the root vim conf

over 1 year ago ·