I'm using a specific centos7 image from AWS EC2 which, for some reason, does not have vim installed.

I already run ansible scripts on newly spun up servers, so I planned to add VIM via ansible. I want to set it up with the usual behavior of vi mapping to vim for all users except root.

Doing a yum install with ansible is easy. However, I'm not sure where I should put the "alias vi=vim" line. If I put it into /etc/bash.rc that would presumably also apply to root, which I don't want. What file(s) do I need to modify to get the 'standard' vim mapping behavior I'm use to?

  • I don't use centos, but with my distro vi is a symbolic link to vim, but that doesn't satisfy your everyone but root requirement. – Andy Dalton Apr 16 at 22:18

I don't know why you wouldn't want root to also have alias vi=vim but it's not hard to do.

You're using bash, so you can use bash's $UID or $EUID read-only variables to test if the user is root...so, building on your idea of putting an alias in /etc/bash.bashrc:

[ "$EUID" != "0" ] && alias vi=vim

With other bourne-like shells that don't have those variables, you can use id. e.g.

[ "$(id -u)" != "0" ] && alias vi=vim
  • 1
    For CentOS 7 (and other rhel derivatives) the file is /etc/bashrc, although it would be more appropriate to put in a file in /etc/profile.d/ rather than change a packaged file. Also, it’ll only be evaluated on login and not for every bash invocation. – jsbillings Apr 17 at 17:21
  • Supposedly the argument is that if something breaks badly enough that you need to be root to fix it you want the less complicated vi in case the break messes with VIM. Never really though it was that important since one can always manually run vi though. Mostly I just want the servers to behave the 'standard' way for centos that users are use to, and that would be VIM aliased for non-root accounts only. – dsollen Apr 19 at 13:38

It turns out I didn't need to do this anyways. It appears vim-enhanced yum package configures the vi to vim alias automatically when installed. So once I'd fully updated the yum packages I got the alias I wanted. Still not sure where the aliasing is done though.

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.