6

I'm trying to add the following vim modeline to my global .tern-config file:

// vim: set ft=json:
{
    plugins: {
    ...

However, the Tern server fails to start, giving the following error:

Failed to start server: 
Bad JSON in /Users/XXXXX/.tern-config: Unexpected token / in JSON at position 0

I suspect the reason for this error is JSON's lack of support for comments. I should note that the same modeline in my .eslintrc file works.

How do I include a vim modeline in my .tern-config file?

camden
  • 1,908
  • 19
  • 18
  • Use vim configuration to automatically set the filetype for .json files. Nobody likes to see modelines embedded in files, because they're not necessarily using the same editor as you... – larsks Jan 18 '17 at 00:01
  • `.tern-config` doesn't have a .json extension, though. I'm open to a better solution to get vim to recognize the file as json. – camden Jan 18 '17 at 00:11

2 Answers2

8

If one puts an object like this

"_vim_": { "modeline": "/* vim: set ft=json noet ts=4 sw=4: */" }

as first or last entry into the top-level object list of a json file it will be used as modeline by vim (as long as the line appears close enough at the beginning or end of the file, where "close enough" means: within the number of lines that vim scans for modelines according to its 'modelines' option which defaults to 5).

Also, the object's name ("_vim_") should be carefully chosen, so that -- at best -- it is ignored by the software that uses the file as input, or -- at least -- can be ignored by the software's users (i. e., it doesn't cause any side effect that would be considered as unwanted behaviour).

serolmy
  • 81
  • 1
  • 2
2

You won't able to do this in the file itself. JSON does not support comments, and it's a very unforgiving syntax.

This may work in some JSON files, like .eslintrc, but in others, you will be out of luck. The stricter JSON parsers will not allow it, so it depends on which parser the tool you're using at the moment is built on.

Rather than guessing which parsers are forgiving and which aren't, you are probably better off telling Vim how to do this using an autocmd.

autocmd BufNewFile,BufRead *.tern-config set filetype=json
Dan Lowe
  • 51,713
  • 20
  • 123
  • 112