3

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?

0

3 Answers 3

4

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.)

1
3

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.

1

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
1
  • 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). Dec 17, 2011 at 10:02

You must log in to answer this question.

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