2

I have many users who sudo to root to edit certain files. How can I allow each of them to use their own ~/.vimrc file once they have sudo'd to root? In other words. they each want to use their own color scheme, even though they are acting as root. Is there a way to have VIM use the original user's .vimrc file (before sudo)?

  • 6
    Teach them about sudo -e. – romainl Mar 5 '16 at 18:29
5

The original user is stored in the SUDO_USER environment variable. So your root's vimrc you could do something along the lines of:

if file_readable('/home/' . $SUDO_USER . '/.vimrc')
    source '/home/' . $SUDO_USER . '/.vimrc'
elseif file_readable('/home/' . $SUDO_USER. '/.vim/vimrc')
    source  $'/home/' . $SUDO_USER. '/.vim/vimrc'
endif

As mentioned in the comments, your users can also use sudoedit (aka. sudo -e). This will copy the file to a temporary location, chown it to the current user, and run EDITOR for it as the currently logged in user, and after EDITOR is finished, the temporary file is moved back to the original. This was designed to solve exactly this problem ;-)

  • Does this set the runtime path to include /home/$SUDO_USER/.vim? – FDinoff Mar 5 '16 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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