VPfB
VPfB

Reputation: 17267

Why am I having trouble to embed "set syntax" into a file?

I have read the help and examples, but still don't know what I'm doing wrong.

When I manually enter :set syntax=javascript, I get the syntax highlighting I want.

But when I edit the first line of my file to read:

/* vim: set syntax=javascript: */

nothing happens.

When I split that line into 3 lines:

/*
# vim: set syntax=javascript:
*/

I get some limited syntax highlighting, not as good as with manual command. I can write syntax=anything there and it makes no difference.

(Vim version 7.4.160, Centos 7)

Upvotes: 2

Views: 767

Answers (1)

Jim Stewart
Jim Stewart

Reputation: 17323

Lines of the format:

/* vim: set syntax=javascript: */

are called modelines in Vim. In order for Vim to process them, you must have the modeline option toggled on.

To see if modeline is enabled, run this ex command:

:set modeline?

If it returns nomodeline, you can enable it by adding the following in your ~/.vimrc:

set modeline

Vim will look for a modeline in the first 5 lines of the file, by default. You can set the number of lines that will be searched with e.g.:

set modelines=10

See :help modeline for details.

Upvotes: 4

Related Questions