15

Here is my .vimrc

  1 syntax on
  2 set ts=4
  3 set number
  4 set smartindent
  5 set shiftwidth=4

However, I tried to edit HelloWorld.java and HelloWorld.c. Both have pure regular black font. No any highlighting!

I also tried :syntax on after the vim is open, but no luck.

\>vim -version
VIM - Vi IMproved 7.3 (2010 Aug 15)

\>cat /etc/*-release
openSUSE 11.4 (x86_64)
VERSION = 11.4
CODENAME = Celadon
JackWM
  • 10,085
  • 22
  • 65
  • 92

6 Answers6

19

When you edit the file, are you using

vim filename

This can matter. In some server configurations, if you do vi filename you get vim, but it's a very stripped down version of vim that is very much like the original vi (which does not, among other things, do syntax coloring). On a system configured in this way, if you instead type vim filename, you get the full featured vim.

I just worked through this with a person who was on a server that had the vim-minimal package installed as well as another vim package. I suspect (but did not verify that) the vim-minimal package installed its executable as /bin/vi.

The difference was very clear when you looked at the actual files (i.e. ls -l /bin/vi vs ls -l /usr/bin/vim)--one was about ten times the size. Both of them were actually vim, same version number and everything, but the /bin/vi one was compiled with very few features enabled.

To make it even more confusing:

vi existing.pl

opened the .pl file, gave no syntax coloring

vi [enter]

gave the vim splash screen, and from there

:e existing.pl

opened the file with syntax coloring on.

A comment from Jan Wilamowski suggests checking by doing:

vi --version

If that shows that the syntax feature was not compiled in, try

vim --version

and see if it is compiled in there.

msouth
  • 832
  • 11
  • 21
  • 1
    Amazing. I've spent hours over the past couple of years trying to get syntac highlighting working. It worked out of the box on the CentOS 4 server I installed in 2005, but on nothing since then. I've read scores of articles that all instructed me to add "syntax on" to .vimrc, but nothing worked until now. vim and vi are in fact different binaries. Kudos to you, good sir. – mdoyle Feb 17 '15 at 15:35
  • @mdoyle I just got a notice about this question and came by and re-read your comment. I wish I could give you all the question karma for the kind act of letting me know how much this helped you! Thank you for taking the time to type that out. It still makes me happy to think about it two years later. – msouth Mar 03 '17 at 05:06
  • 1
    Running `vi --version` gave me a feature list indicating that `syntax` was indeed not included. – Jan Wilamowski Mar 02 '22 at 05:22
  • thanks @JanWilamowski, I added this as a diagnostic tool in the answer. I appreciate you taking the time to comment. – msouth Mar 03 '22 at 15:05
7

You'll need to install the vim-data package on openSUSE for vim syntax colouring to work. Sounds strange, I know that this is not pulled in by default with the vim package but AFAIK it's for people who want to create tiny base installs. Package vim-data contains the runtime files.

Also make sure your remote environment has an appropriate TERM variable set TERM=screen-256color, TERM=xterm, TERM=xterm-256color should all work just fine with ssh and ssh with screen/tmux.

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
Graham
  • 71
  • 1
  • 3
3

If all above have been done and you see some underlines and bold instead of actual colors... this might work for you:

export TERM=xterm-color
user185899
  • 31
  • 2
  • Thank you! This fixed syntax highlighting for me; tmux sets `TERM` to `screen-256color`, which vim doesn't seem to like. – Haldean Brown Mar 10 '17 at 16:59
2

in your .vimrc, I don't see filetype setting. you could try to add:

filetype plugin indent on 

to your vimrc.

if you don't have set nocp, add this line too.

if you read :h filetype

:filetype on

Each time a new or existing file is edited, Vim will try to recognize the type
of the file and set the 'filetype' option.  This will trigger the FileType
event, which can be used to set the syntax highlighting, set options, etc.
Kent
  • 189,393
  • 32
  • 233
  • 301
2

For some strange reason on MacOS, 'syntax on' must be the first line in your .vimrc file. The line appears to be ignored if placed elsewhere in the file.

Gonzo
  • 21
  • 3
  • This fixed my error. To add to the answer though, if you have the "syntax on" set on any other line number, though it will complain of "-bash: syntax: command not found", you'll start seeing colours in your vi/vim editor. – Testing123 Sep 05 '19 at 20:57
2

One item not mentioned is :set syntax=<type>, e.g. :set syntax=markdown.

This has been successful in instances where other techniques above were not.