DEV Community

Cover image for What's nvim-treesitter?
Async hronous
Async hronous

Posted on

What's nvim-treesitter?

(Don't mind the cover image, I'm testing out stuff :P)

๐Ÿ‘‹ Introduction

Do you don't like these default Neovim's Treesitter syntax?

Do you want it to look like:
why are you reading this

To:
Why are you reading this :(?

I'm not explaining why it's better. Mainly because it's smarter. (wow, that didn't make sense)

alright' nuf' talkin' and let's start ๐Ÿ‘ทโ€โ™€๏ธ configuring!

๐Ÿคจ Installing nvim-treesitter

If you've been following along your file structure should look like...

~/.config/nvim/
โ”œโ”€โ”€ init.lua
โ””โ”€โ”€ lua/
    โ”œโ”€โ”€ core/
    โ”‚   โ”œโ”€โ”€ init.lua
    โ”‚   โ””โ”€โ”€ options.lua
    โ””โ”€โ”€ keymaps/
        โ””โ”€โ”€ movement.lua
        โ””โ”€โ”€ ...
    โ””โ”€โ”€ plugins/
        โ””โ”€โ”€ colorscheme.lua
        โ””โ”€โ”€ ...
Enter fullscreen mode Exit fullscreen mode

Now we're going to create a new configs directory. (in the lua directory) and in the configs directory, we're going to create sub directories for each "category" of a plugin's configuration. (e.g: UI related configurations in the configs.ui directory, editor related configurations in the configs.editor directory.)

So now our file structure should look like:

~/.config/nvim/
โ”œโ”€โ”€ init.lua
โ””โ”€โ”€ lua/
    โ”œโ”€โ”€ core/
    โ”‚   โ”œโ”€โ”€ init.lua
    โ”‚   โ””โ”€โ”€ options.lua
    โ””โ”€โ”€ keymaps/
        โ””โ”€โ”€ movement.lua
        โ””โ”€โ”€ ...
    โ””โ”€โ”€ plugins/
        โ””โ”€โ”€ colorscheme.lua
        โ””โ”€โ”€ treesitter.lua
        โ””โ”€โ”€ ...
    โ””โ”€โ”€ configs/ <- This!
        โ””โ”€โ”€ ui/
        โ””โ”€โ”€ editor/
        โ””โ”€โ”€ ...
Enter fullscreen mode Exit fullscreen mode

Alright. Now just create a new module in out plugins/ directory named treesitter.lua (it could be anything)
And in it:

return {
  "nvim-treesitter/nvim-treesitter.lua",
  config = require("configs.editor.treesitter"),
  event = "BufReadPost"
}
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘ทโ€โ™€๏ธ Configuring

And in our configs/editor directory create a new treesitter.lua module (again)
In it SLAM this in:

return function()
  local status, treesitter = pcall(require, "nvim-treesitter.configs")
  if not status then return end

  treesitter.setup({
    auto_install = true, -- Auto-install missing parsers. (once you open a file that requires it.)
    highlight = {
      enable = true -- Enable beautiful syntax highlighting!!!
    }
  })
end
Enter fullscreen mode Exit fullscreen mode

Let's break this down. It pcalls a require statement that, wants a module called nvim-treesitter more specifically, nvim-treesitter.configs. It assigns it a local variable called treesitter.

Then it setups nvim-treesitter with some configuration.

Now quit Neovim (if you can) using :wqa (Of course, if you can.)
And BAM, SUPER smart, beautiful syntax highlighting with the catppuccin colorscheme! Wait... Did you say "catppuccin"?
Alright kthxbye, imma get some catppuccin-mocha. :wqa

Quick Plug

Top comments (0)