0

Whenever I create html file with jinja code from terminal either using shell commands or directly using vim, the file type is always set to html and not jinja.html, which causes number of issues like jinja files not being syntax highlighted, snippets do not show up in completion popups.

I am using Glench/Vim-Jinja2-Syntax for jinja syntax highlighting. I do understand this plugin might be looking for certain keywords in the file to associate the file as jinja.html file. Even if I create a file like below in Vim, save it , close it with :wq and then open it again, still the file type is html only.

{%  extends "base.html" %}

{% block body %}

{% endblock %}

This is what set filetype? reports filetype=html .

However if I create the same file with some IDE like PyCharm or VsCode this does not happen, and after that if I open the file in Vim, then it sets the correct file type as jinja.html. My guess is PyCharm and VsCode might be adding some metadata to the file while creating the file which Vim is not doing when the file is created by Vim but I am not sure about this..

Another weird thing that I noticed, say I create a file named dummy.html with above jinja code with Vim and then save+close it, obviously Vim isn't setting the correct file type for it. Now the weird, even if I delete this file and then recreate it in PyCharm with same name i.e. dummy.html and load in Vim , Vim fails to set the correct file type.

I tested this multiple file names, means I created the file in Vim and then delete it and then create the file with same name in PyCharm and Vim does not set the file type. However if I use a new name while creating the file in PyCharm, a name which I haven't used while creating file with Vim , then Vim file sets the file type correctly to jinja.html.

So can anyone please help me how can I fix this. I apologize if the issue description is bit confusing but this issue is so weird that honestly I am not sure how to better the description.

  • @Ralf just updated the question to contain lot more detail – Rohit Bhanot Feb 11 at 13:52
  • I installed the Glench/Vim-Jinja2-Syntax plugin and created your example file. At first it was detected as html, as it has no content. After writing and :e! or on reopen Vim, it was detected as jinja.html and the filetype was correct. Tested on Vim 8.1.885 on Linux. I would recommend to strip down your Vim setup to only contain Vim-Jinja2-Syntax and test again. – Ralf Feb 11 at 18:42
0

Update:

What file type plugin are you using for Jinja? Vim doesn't ship with one so you must have installed it. Looking into it will probably give you some pointers. Also, there are several plugins, so you could try another one.

=====

If there is a difference depending on how the file is created, the file type recognition may be dependent on whether or not a byte order mark (BOM) is present (there is probably no BOM in files created on the terminal).

So a first workaround might be to add a BOM (or remove it if present) for offending files:

:set invbomb

You may have to save and re-read the file to refresh syntax highlighting:

:w | e

I'm afraid I have no immediate idea why the file type recognition might be BOM-dependent and how to remedy that.

  • Yeah, It could be due to that only, that was my guess that Pycharm or vscode might be adding some metadata to the file while creating which Vim is not doing. Also I tried setting invbomb and then reloading the file but it didn't work. – Rohit Bhanot Feb 11 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.