angelplusultra
angelplusultra

Reputation: 61

How to configure NeoVim Treesitter in VimScript?

I'm trying to configure treesitter syntax highlighting for my neovim config but the docs only show examples with Lua and i am using Vimscript. If you're using VimScript they redirect you to an example of calling a Lua function within VimScript but I don't understand how it works.

From their docs:

Following examples assume that you are configuring neovim with lua. If you are using vimscript, see :help lua-heredoc. All modules are disabled by default and need to be activated explicitly in your init.lua, e.g., via

lua-heredoc:


      Executes Lua script {script} from within Vimscript. {endmarker} must NOT
      be preceded by whitespace. You can omit [endmarker] after the "<<" and use
      a dot "." after {script} (similar to |:append|, |:insert|).
  
      Example: >
          function! CurrentLineInfo()
          lua << EOF
          local linenr = vim.api.nvim_win_get_cursor(0)[1]
          local curline = vim.api.nvim_buf_get_lines(
                  0, linenr - 1, linenr, false)[1]
          print(string.format("Current line [%d] has %d bytes",
                  linenr, #curline))
          EOF
          endfunction

       Note that the `local` variables will disappear when the block finishes.
       But not globals.

I'd like to make this Lua code work in VimScript:

require("nvim-treesitter.configs").setup({
    ensure_installed = { "javascript", "typescript", "lua", "vim", "json", "html", "rust", "tsx" },
    sync_install = false,
    auto_install = true,
    highlight = {
        enable = true,
    },
})

Upvotes: 1

Views: 1772

Answers (1)

TornaxO7
TornaxO7

Reputation: 1309

Just add the following to any .vim file which will be loaded:

lua << EOF
require("nvim-treesitter.configs").setup({
    ensure_installed = { "javascript", "typescript", "lua", "vim", "json", "html", "rust", "tsx" },
    sync_install = false,
    auto_install = true,
    highlight = {
        enable = true,
    },
})
EOF

and it'll be normally called as if you'd be in a lua file. You can imagine that as a wrapper vor neovim which says at lua << EOF: "Ok bro, after this line, there'll be lua code and not vimscript stuff. Got it?" and if it reaches the bottom EOF: "Alright, the input for the lua command ends here. We can keep going after this line with vimscript!"

Other example

Take a look into that as an example:

lua << EOF
print("Hello from lua")
EOF

If you open up neovim in a new file now then you can see a Hello from lua in your messages (:msg).

(Opiniated) Recommondation

I highly recommend to use an extra lua file where you write your lua-code to avoid those "wrappers" (lua << EOF and EOF) in your config file which makes it more readable in my opinion.

Upvotes: 1

Related Questions