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'
root@localhost> sudo which vi
root@localhost> sudo -i which vi
root@localhost> sudo -E which 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.

2 Answers 2


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.


In case you always want to use vim instead of vi, you can just

$ sudo ln -s /usr/bin/vim /usr/local/bin/vi

(This assumes you have /usr/local/bin before /bin in your PATH.)

Granted, it's a (system-local) hack, but quite simple and effective.

  • Bad practice to create symlinks on the fly. Better to either create a ~/bin/ directory and create a symlink in there and add ~/bin/ to PATH or to use the EDITOR variable and pass it along when sudoing by editing the sudoers file.
    – mmtauqir
    Apr 18, 2013 at 15:05
  • 1
    @mtahmed The ~/bin/ and PATH doesn't help much when the problem is around cross-user launches. I also have less reservations about symlinks (especially in .../local/... dirs), but YMMV. Apr 18, 2013 at 15:17

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.