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)?
1 Answer
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
?– FDinoffCommented Mar 5, 2016 at 22:51
sudo -e
.