How to set vim as the default editor when I sudo

sudovivim

I'm working on a CentOS 5.9 machine, and I simply want to type

sudo vi somefile

and have my trusty vim with syntax highlighting, etc. This seems simple enough, but after many attempts, it's still not working.

root@localhost> which vi
alias vi='vim'
        /usr/bin/vim
root@localhost> sudo which vi
/bin/vi
root@localhost> sudo -i which vi
/bin/vi
root@localhost> sudo -E which vi
/bin/vi

I logged in as root to ensure that by default vi somefile invoked vim. It did. I also tried to preserve the environment with -E and run the login scripts with -i. That did not work. In both my .profile and /etc/environment, I export the EDITOR as vim, and set vi as an alias to vim.

I also commented out a env_reset line in /etc/sudoers.

What else should I try? I didn't think replacing vi with vim would be so difficult, and I really like to understand what is going on here.

Best Answer

That's what the command sudoedit is for. It creates a temporary copy that is edited in the users environment. It consults SUDO_EDITOR, VISUAL and the variable EDITOR to find a suitable binary. After the edit the copy overwrites the original file.

Related Question