Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim modeline in a JSON file

Tags:

json

vim

tern

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?

like image 745
camden Avatar asked Jan 17 '17 23:01

camden


2 Answers

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).

like image 93
serolmy Avatar answered Sep 22 '22 18:09

serolmy


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
like image 29
Dan Lowe Avatar answered Sep 21 '22 18:09

Dan Lowe