jinja.vim icon indicating copy to clipboard operation
jinja.vim copied to clipboard

Automation for syntax=on not working

Open akBeater opened this issue 8 months ago • 2 comments

Hello, first of all, thank you for the plugin. It really helps me! :)

I had to make some changes to make it work for me. I don't have any experience on customizing something like that, so I don't know if this is good code.

The Problem is, as you wrote, that you need syntax=on, but the following didn't worked for me:

if !get(b:, 'jinja_syntax_autocmd_loaded', v:false)
        if luaeval("vim.treesitter.language.get_lang('jinja')") == v:null
                autocmd FileType <buffer> if !empty(&ft) | setlocal syntax=on | endif
        endif
        let b:jinja_syntax_autocmd_loaded = v:true
endif

I handled it the following way in neovim:

  1. First of all, I set all "html" to "html.jinja", because I only write jinja code or html but never django:
vim.filetype.add {
  extension = {
    html = "html.jinja"
  }
}

  1. Secondly I wrote an autocommand to set syntax=on when I open the buffer and it has the filetype "html".
vim.api.nvim_create_autocmd("BufEnter", {
  desc = "set syntax=on on attached buffer",
  group = vim.api.nvim_create_augroup("JinjaSyntaxOn", {clear=true}),
  pattern = "*.html",
  callback = function ()
     vim.opt.syntax = "on"
  end,
})

You could enhance that, if you check for ft=html.jinja and only set it if this is true, but this is simple and works for me.

akBeater avatar Apr 19 '25 10:04 akBeater

I also have the following in my ftdetect/html.vim file:

autocmd! BufRead,BufNewFile *.html  set ft=html | call jinja#AdjustFiletype()

I think HTML is a special case because Vim tries to be clever and detect Django inside *.html files.

HiPhish avatar May 11 '25 19:05 HiPhish

@akBeater thanks for insight. I've modified your code to work with ansible yml.j2 files:

Here is my ftplugin/yaml.lua:

vim.api.nvim_create_autocmd("BufEnter", {
	desc = "set syntax=on on attached buffer",
	group = vim.api.nvim_create_augroup("JinjaSyntaxOn", { clear = true }),
	pattern =
	{
		"*.yml.j2",
		"*.yaml.j2",
	},
	callback = function()
		vim.opt.syntax = "on"
	end,
})

alteriks avatar Nov 15 '25 12:11 alteriks