Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I don't understand how to allow syntax highlighting in files I'm editing via vi when using sudo. When I 'sudo vi ' the terminal is only black & white of my terminal settings. In vi if I enter ':syntax on' nothing changes.

When I 'vi ' all the syntax is properly colored. Using RHEL 5.4, relevant env:

LS_COLORS=no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=01;32:.cmd=01;32:.exe=01;32:.com=01;32:.btm=01;32:.bat=01;32:.sh=01;32:.csh=01;32:.tar=01;31:.tgz=01;31:.arj=01;31:.taz=01;31:.lzh=01;31:.zip=01;31:.z=01;31:.Z=01;31:.gz=01;31:.bz2=01;31:.bz=01;31:.tz=01;31:.rpm=01;31:.cpio=01;31:.jpg=01;35:.gif=01;35:.bmp=01;35:.xbm=01;35:.xpm=01;35:.png=01;35:.tif=01;35:

SHELL=/bin/bash

TERM=xterm-color

share|improve this question
    
vi doesn't support color, so anything showing color is running vim or similar. –  Chris S Oct 8 '13 at 13:30

6 Answers 6

up vote 8 down vote accepted

Larks answer is probably the most likely. You may not wish to change your root accounts vi to vim permanently as if your resources are low vi is almost guaranteed to always work, I'm not so sure about vim.

You are probably using (color)

/usr/bin/vim 

under your normal user and

/bin/vi

under sudo or root. You can check by using:

which vi

once under your normal user and the other via sudo

sudo which vi

Do a

man which 

if you need more details

share|improve this answer
    
I didn't change the root account to use vim but just knowing that which showed that vi was aliased to vim as my user tells me to use sudo vim <file> instead of sudo vi <file> –  Scott Lundgren Dec 16 '10 at 21:06

On a RHEL system, /bin/vi is typically a minimal version of vim, without any syntax highlighting support. /usr/bin/vim is the full-featured editor. It is very likely that in your user environment, vi is an alias for vim. Try this:

sudo vim /some/file

Do you get syntax highlighting now?

share|improve this answer
    
Yes! and it seems sinping's answer was the one I needed to get the behaviour I wanted –  Scott Lundgren Dec 16 '10 at 21:04
    
Yep. Thanks for this! :) –  gideon Dec 21 '12 at 1:41

Your vi is probably /bin/vi from package vim-minimal which does not support syntax highlighting. sudo vi launches /bin/vi for you.

Bash only does alias expansion on the first word in your command line, so in:

sudo vi

vi is not substituted to vim even if you have that alias defined.

The solution is define another alias (for the user invoking sudo):

alias sudo='sudo '

Note the space after the second sudo.

Using this sudo, bash will do alias expansion for vi in sudo vi. In the alias section of Bash doc it says:

If the last character of the alias value is a blank, then the next command word following the alias is also checked for alias expansion.

After you enter your command, You can press Ctrl + Alt + E in bash to see the expansion result.

Credits:

https://bbs.archlinux.org/viewtopic.php?id=36796

http://www.shellperson.net/using-sudo-with-an-alias


Simply aliasing vi in /root/.bashrc will not work.

It may also be noted that vi is not aliased for root on some distros in /etc/profile.d/vim.sh:

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

Changing this will not solve the problem either.

share|improve this answer
    
Awesome trick to have aliases with sudo! –  Corey Henderson Feb 8 '13 at 17:05
    
Huge help. This was the problem for me. Thanks! –  Nathan Garabedian Mar 27 '13 at 18:28
    
Love this - simple and elegant!!! This IS the correct answer. –  FilmJ Jun 25 '13 at 16:41

By default in 5.4 vi is default. I forget what version that started in. This will add the necessary alias for you:

cat <<_EOF >>/root/.bashrc

alias "vi"="/usr/bin/vim"
_EOF
share|improve this answer

i found that in my version of vim (install with sudo apt-get install vim on debian 7) the syntax on command is defined on a per-user basis in ~/.vimrc. however by default the install did not create a /root/.vimrc file for sudo vito load. so i just copied ~/.vimrc into the /root/ dir and this did the trick: sudo cp ~/.vimrc /root/

share|improve this answer

1) Check if you're really using vim and not vi (which unfortunatelly is often on fresh installed systems)

2) Check your "/etc/vim/vimrc": syntax on should be on

3) Not all kinds of files have systax-highlighting infos availiable by default....

tsg

share|improve this answer
    
There was no /etc/vim/vimrc, but /etc/vimrc did have syntax highlighting by default: if &t_Co > 2 || has("gui_running") syntax on set hlsearch endif –  Scott Lundgren Dec 16 '10 at 21:02

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.