vi vs vim on CentOS/RHEL

Here’s a useful tip for CentOS/RHEL (and possibly other distros) – by default, a lightweight version of vim is installed to /bin/vi, which is enough to get basic administration done, but doesn’t have a lot of useful features, like being able to copy data from one instance to another.

The solution is to install the vim-enhanced RPM, which installs vim to /usr/bin/vim. However, this introduces a new problem, since when you run “vi” you will get a different program to running “vim”.

Some people remove /bin/vi and replace it with a symlink to /usr/bin/vim – which works, but will be a nasty problem if the system ever boots without mounting /usr, and needs some administration. It’s also impossible to do if you don’t have root access to the system.

The best fix, is to add an alias to your bash config, by adding the following lines to ~/.bashrc:

if [ -f /usr/bin/vim ]; then
         alias vi='vim'
fi

Then run “source ~/.bashrc” to update your current terminal, or open a new terminal to take advantage of the change.

You will now find that if you run the vi command, the alias will actually run vim! Nice and easy, and it doesn’t require root privileges to setup, as it only changes a local user config file.

UPDATE – 30 Apr 2007

As pointed out by a reader, Redhat does infact provides an alias at installation time to accomplish this, in the file /etc/profile.d/vim.sh.

However, the alias will only be setup for users with an ID larger than 100 (excludes root which is ID 0), and the alias also does not check for the presence of the vim executable.

You could edit the file, and change the following:

if [ -n "$BASH_VERSION" -o -n "$KSH_VERSION" -o -n "$ZSH_VERSION" ]; then
  [ -x /usr/bin/id ] || return
  [ `/usr/bin/id -u` -le 100 ] && return
  # for bash and zsh, only if no alias is already set
  alias vi >/dev/null 2>&1 || alias vi=vim
fi

Into:

if [ -n "$BASH_VERSION" -o -n "$KSH_VERSION" -o -n "$ZSH_VERSION" ];then
  if [ -f /usr/bin/vim ]; then
         alias vi='vim'
  fi
fi

This would make a vim alias that would work for all users, and which would check for the existence of the vim binary first.

This entry was posted in Uncategorized and tagged . Bookmark the permalink.