Setup neovim treesitter on windows using scoop and zig

Author: Sanket Sonavane

Publish Date: 2024-07-10

Last Updated: 2024-07-10

I have been following Josean Martinez neovim guide to setup neovim on windows. I have been using lazy package manager.

The problem we face on windows is we need a C compiler. Its required to compile the parsers needed for nvim-treesitter

A solution is well documented here but it contains a lot of approaches and in this blog we pick the one which uses zig. Windows support · nvim-treesitter/nvim-treesitter Wiki

I wanted a quick and easy approach which worked great for me that is using zig as my compiler of choice. The installation is fairly straight forward, if you have scoop package manager installed on your machine and if not we will go through those steps as well, as installing scoop is also easy.

scoop install

We will be following the official steps to install scoop listed on their home page. Scoop

start windows powershell.

run the following command in powershell

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

then run

Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression

once install is successful you can check using following command

scoop --version

by default only the main bucket is installed we also need to add extras and versions bucket. a bucket contains apps that can be installed using scoop adding more buckets increases the list of packets that can be installed.

scoop bucket add extras
scoop bucket add versions

install zig using scoop

scoop install zig

zig is now installed and upon completion it suggests that we also install extras/vcredist2022 using scoop. so now we will install the same using the following command. this will download the installer and windows will prompt us to proceed with the installer we click on in the installation popup.

 scoop install extras/vcredist2022

on success install scoop informs you that extras/vcredist2022 is installed successfully and now you can delete the installer using the following command.

scoop also informs you that for changes to take place we should restart our machine.

so lets first uninstall vcredist2022 which is just the installer which was used and now we dont need this installer any further. so lets remove the installer from scoop using following command.

scoop uninstall vcredist2022

now close everything and restart your machine.

test zig in working correctly

we will now test the zig compiler is installed successfully by following steps from here Getting Started ⚡ Zig Programming Language

open terminal and navigate to a project directory

mkdir hello-world
cd hello-world
zig init

now it will spawn a new zig project. lets run the build command to build this project

zig build run

your should get the following output

All your codebase are belong to us.
Run `zig build test` to run the tests.

then zig is installed correctly now and neovim will be able to leverage this to compile tressitter parsers on windows.

testing this in neovim

we are not covering neovim + treesitter setup steps those are very well documented in Josean Martinez neovim guide.

What I would like to share is my treesitter.lua file from plugin folder wheres I selected two parsers which were earlier not working and now after zig being installed they work fine.

return {
  "nvim-treesitter/nvim-treesitter",
  event = { "BufReadPre", "BufNewFile" },
  build = ":TSUpdate",
  config = function()
    -- import nvim-treesitter plugin
    local treesitter = require("nvim-treesitter.configs")
    -- configure treesitter
    treesitter.setup({ -- enable syntax highlighting
      highlight = {
        enable = true,
      },
      -- enable indentation
      indent = { enable = true },
      -- ensure these language parsers are installed
      ensure_installed = {
        "markdown",
        "markdown_inline",
      },
    })
  end,
}

In this plugin file you can see that I have chosen to install markdown and markdown_inline parsers. so as soon as I open any markdown file in neovim for the first time post setting treesitter neovim would compile the parsers and then load them. now this step executes successfully with zig installed. neovim leverages zig for this.

you should see a message like so if everything went well and your markdown file should also open up properly.

treesitter parser installed successfully

references

articles

youtube

reddit