Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to turn on color syntax highlighting in vi or vim

Status
Not open for further replies.

lcs01

Programmer
Aug 2, 2006
182
US
I've been using Ubuntu over a year now. I always use vim to do the progaming work. I know vim has a feature with color syntax highlighting. But I don't know how to make it work.

I did lots of research online and tried different things in my .vimrc and .gvimrc files. However, I just could not make the color highlighting work!!

I know all other settings in my .vimrc file are working, except 'syntax on' takes no effects at all.

The following is my .vimrc:
Code:
set nocompatible      " We're running Vim, not Vi!
set ai nu ic lisp sm ht=2 ts=2 sw=2
[COLOR=red]syntax on[/color] [b]" this line not working[/b] 
map z :w
map q :q
map wq :wq
map qq :q!
abb taht that
abb teh the
abb waht what

To me, 'syntax on' would turn the color highighting. But it does not work. I put the same line in .gvimrc, it does not work either.

I also tried ':syntax on' in a vim session. Not working!!

BTW, I must admit that I don't know the difference between .vimrc and .gvimrc. I am not even sure whether I can have both of them in my $HOME dir.

I also tried the suggestions at this site:


Vim Color Editor HOW-TO (Vi Improved with syntax color highlighting)


No luck.

Could someone help? Thanks!!
 
Do you have the syntax files installed? By default, Ubuntu ships with a package they call tiny-vim, which is a very minimal vim installation. To get the syntax highlighting files, you need to have the vim-runtime package installed.
 
Can you give the output of ``env''?

Also, it would be useful to have the output of ``vim --version''
It may be that the syntax highlighting was not included at compile time in your distribution's vim.

It is unlikely for a popular distribution like Ubuntu, but let us eliminate that.

mtodorov69
 
Thank you, AdaHacker and mtodorov69, for your help.

To AdaHacker, how can I tell if vim-runtime package is installed? Where to get this package?

To mtodorov69, $ENV is not set at all. What value should it be? And here is the output of 'vim --version':

Code:
[b]% vim --version[/b]
-bash: vim: command not found
[b]% vi --version[/b]
VIM - Vi IMproved 6.3 (2004 June 7, compiled Aug 22 2005 09:39:14)
Included patches: 1-21, 23-24, 26, 28-34, 36-37, 39-40, 42-43, 45-46, 81-82
Compiled by <bugzilla@redhat.com>
Tiny version without GUI.  Features included (+) or not (-):
-arabic -autocmd -balloon_eval -browse +builtin_terms -byte_offset -cindent
-clientserver -clipboard -cmdline_compl -cmdline_hist -cmdline_info -comments
-cryptv -cscope -dialog -diff -digraphs -dnd -ebcdic -emacs_tags -eval
-ex_extra -extra_search -farsi -file_in_path -find_in_path -folding -footer
+fork() -gettext -hangul_input +iconv -insert_expand -jumplist -keymap -langmap
 -libcall -linebreak -lispindent -listcmds -localmap -menu -mksession
-modify_fname -mouse -mouse_dec -mouse_gpm -mouse_jsbterm -mouse_netterm
-mouse_xterm +multi_byte -multi_lang -netbeans_intg -osfiletype -path_extra
-perl -printer -python -quickfix -rightleft -ruby -scrollbind -signs
-smartindent -sniff -statusline -sun_workshop -syntax -tag_binary
-tag_old_static -tag_any_white -tcl +terminfo -termresponse -textobjects -title
 -toolbar -user_commands -vertsplit -virtualedit -visual -viminfo -vreplace
+wildignore -wildmenu -windows +writebackup -X11 -xfontset -xim -xsmp
-xterm_clipboard -xterm_save
   system vimrc file: "/etc/vimrc"
     user vimrc file: "$HOME/.vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H     -O2 -g -pipe -m32 -march=i386 -mtune=pentium4 -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64
Linking: gcc   -L/usr/local/lib -o vim       -lselinux -ltermcap -lacl

Again, your help is highly appreciated.
 
I guess I showed some dumbness in my previous post. What a shame! I forgot how to print env's, but I recalled it:

Code:
[b]% printenv[/b]
HOSTNAME=myhost
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=::ffff:###.###.###.### 42894 22
CVSROOT=:pserver:cvs:/home/cvs
OLDPWD=/home/xxx/
SSH_TTY=/dev/pts/0
USER=cyan
LS_COLORS=no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40
;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*
.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*
.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*
.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=

00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=
00;35:*.tif=00;35:
MAIL=/var/spool/mail/xxx
PATH=/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin
INPUTRC=/etc/inputrc
PWD=/home/xxx/yyy
EDITOR=vi
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/xxx
LOGNAME=xxx
PRINTER=upstairsprinter
CLASSPATH=...
SSH_CONNECTION=::ffff:###.###.###.### 42894 ::ffff:10.41.1.172 22
LESSOPEN=|/usr/bin/lesspipe.sh %s
G_BROKEN_FILENAMES=1
_=/usr/bin/printenv
 
Hi lcs01,

Your terminal is xterm which is sufficient for color display, but as I suspected, there is a -syntax switch in your vim --version output. Which means, sadly, that whoever is maintaining vim package at Redhat thinks you don't need syntax highlighting.

At my Debian sarge, vim is shipped with +syntax.

Are you able to download source and recompile vim?

./configure
make
make install

should be sufficient.

Good luck,

mtodorov69
 
Thanks for your advice, mtodorov69.

I have a root privilege so that I suppose I should be able to recompile it. However, is there an easy way to reset this env? I meant to set this one on in one of the system files? If so, how?

Thanks gain.
 
lcs01,

If it is a compile-time switch, the code for syntax highlighting might not have been included at all in your binary.

I am afraid a recompile is a must in this case.

Clean way to do this is to uninstall system's vim package and configure vim to install under /usr/local.

I.e.

% ./configure --prefix=/usr/local
% make

Greetz,
mtodorov69
 
Try [tt]syntax enable[/tt]

From [tt]:help syntax[/tt]
Code:
This command switches on syntax highlighting:

	[blue]:syntax enable[/blue]

--
-- Ghodmode

Give a man a fish and he'll come back to buy more... Teach a man to fish and you're out of business.
 
:syntax enable is synonimous to syntax on and cannot enable colouring if the code for doing the stuff wasn't enabled at compile time IMHO.

 
mtodorov,
Checking [tt]:help syntax[/tt] again shows that [tt]syntax enable[/tt] and [tt]syntax on[/tt] are not synonymous. However, either one should turn on the colors.

You're right about the compile-time option. I didn't read the thread carefully enough the first time.

lcs,
You probably don't need to go through the effort of compiling the source. Just make sure you install the right packages. [tt]dpkg-query[/tt] can tell you what you have installed. I think that you just have vim-tiny installed. Then [tt]apt-get[/tt] can install whatever you want that you don't have.

I use Vim heavily, so I have a lot installed ...
Code:
@home:~$ dpkg-query -l vim
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)
||/ Name                       Version                    Description
+++-==========================-==========================-====================================================================
ii  vim                        7.0-164+1ubuntu7.2         Vi IMproved - enhanced vi editor
vince@home:~$ dpkg-query -l *vim*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)
||/ Name                       Version                    Description
+++-==========================-==========================-====================================================================
un  gvim                       <none>                     (no description available)
ii  vim                        7.0-164+1ubuntu7.2         Vi IMproved - enhanced vi editor
ii  vim-common                 7.0-164+1ubuntu7.2         Vi IMproved - Common files
ii  vim-doc                    7.0-164+1ubuntu7.2         Vi IMproved - HTML documentation
un  vim-full                   <none>                     (no description available)
ii  vim-gnome                  7.0-164+1ubuntu7.2         Vi IMproved - enhanced vi editor - with GNOME2 GUI
un  vim-gtk                    <none>                     (no description available)
ii  vim-gui-common             7.0-164+1ubuntu7.2         Vi IMproved - Common GUI files
un  vim-lesstif                <none>                     (no description available)
un  vim-perl                   <none>                     (no description available)
un  vim-python                 <none>                     (no description available)
un  vim-ruby                   <none>                     (no description available)
ii  vim-runtime                7.0-164+1ubuntu7.2         Vi IMproved - Runtime files
un  vim-scripts                <none>                     (no description available)
un  vim-tcl                    <none>                     (no description available)
ii  vim-tiny                   7.0-164+1ubuntu7.2         Vi IMproved - enhanced vi editor - compact version

--
-- Ghodmode

Give a man a fish and he'll come back to buy more... Teach a man to fish and you're out of business.
 
Thank you, Ghodmode, for your help. But I guess that I have to ask some dumb questions again. :(

What is 'dpkg-query'? I googled it and got impression that it is a linux command? However, it does not look like a conventional unix/linux command to me. And I can not find it on my linux box using the command 'find'.
 
In spite of what the kind of people who chant RTFM!! all day long say, there are no dumb questions. Please never hesitate to ask :)

Always start with the man page :) [tt]man dpkg-query[/tt] will tell you a lot. Sometimes man pages will even tell you where to get more information.

dpkg-query is part of dpkg, a set of tools used to help manage the Debian package management system. Since Ubuntu is based on Debian, it has all of the same tools.

Specifically, dpkg-query has a lot of options that allow you to query the database that contains information about the packages installed on your computer.

--
-- Ghodmode

Give a man a fish and he'll come back to buy more... Teach a man to fish and you're out of business.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top