Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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. –  mtahmed Apr 18 '13 at 15:05
    
@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. –  Ingo Karkat Apr 18 '13 at 15:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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