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)