Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The default version of vi that comes with Ubuntu Hardy is the 7.1 or 7.2. So I installed vim 7.3 from sources.

Then to make it convenient I added this to my .bashrc

alias vi='vim'

In order to let the 7.3 vim overwrite the vi which is old version. This works fine for me, but when I add sudo before that, something like

sudo vi /usr/local/nginx/conf/nginx.conf

It gaves me the old version

Anyone give me a clue why is it?

share|improve this question
up vote 2 down vote accepted

vi 7.2 and 7.3 do not exist. There is only vi 050325 and vim 7.2, two entirely separate programs with similar features. If you want vim, type vim.

The reason your alias does not work is because aliases are expanded by your shell, and the shell does not know what each program will be doing – it sees "sudo" as the command and "vi myfile" as arguments, and only attempts to expand the first word, "sudo", without daring to touch the arguments.

You can tell bash to expand aliases following certain commands, by adding an alias such as: (note the space inside quotes is required)

alias sudo="sudo "

The space will tell bash that the first word after "sudo" is a command that should be expanded – for example, "sudo vi ..." to "sudo vim ..." will work now.

The answers telling about /root/.bashrc are incorrect for several reasons. First, sudo executes the given command without running a shell. (This can be changed by using sudo -s cmd or sudo -i cmd, but if neither option is given, the default behavior is to execute the command directly, without a shell.)

Second, bash will only expand aliases when run in interactive mode, not when run as bash -c cmd, which is what sudo would use. (Again, this can be changed, but the default setting is to only expand aliases interactively.)

share|improve this answer
well, deleted mine. – Sathya Dec 17 '11 at 11:55

Aliases are per-user. Your first command assigned the alias only to your non-privileged user, and when you use sudo it's running as root. You should create the alias for the root user too.

share|improve this answer

When run sudo vi,

  1. sudo loads /root/.bashrc file. And it decides whether its an alias or original one (wich is vim).
  2. The vi part then parses /root/.vimrc if it exists. Otherwise /etc/vim/vimrc
share|improve this answer
sudo by itself does not process .bashrc or expand aliases; those two features are part of bash, which sudo does not run (it executes the command directly). – grawity Dec 17 '11 at 10:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .