Zig NEWS

loading...

Setting up Visual Studio Code for writing Zig

Jarred Sumner
・2 min read

If you're googling VSCode development environment Zig, this post should help.

The first step is downloading & installing Zig.

Installing Zig

I personally suggest using Zig from the master on git, but Zig also has versioned releases.

With Homebrew (macOS):

brew install zig --HEAD
Enter fullscreen mode Exit fullscreen mode

With Snap (Ubuntu):

snap install zig --classic --edge
Enter fullscreen mode Exit fullscreen mode

You can read more about installing or download binaries separately

Zig Language Server (zls)

zls is the language server for Zig. It gives your editor autocomplete, better errors, and semantic highlighting.

It enables completions for functions & types.

image

To install zls, I suggest compiling it from source. Zig's compiler is fast, so it shouldn't take long.

git clone https://github.com/zigtools/zls
cd zls
git submodule update --init --recursive --progress
zig build -Drelease-fast
Enter fullscreen mode Exit fullscreen mode

Configuring zls

From your zls directory, run this:

./zig-out/bin/zls configure
Enter fullscreen mode Exit fullscreen mode

If you're asked this, press n or it will fail unless you run as sudo
image

Configuring Visual Studio Code

There are two extensions you will want to install.

  1. vscode-zig gives you syntax highlighting and is required for zig fmt to run automatically on save.
  2. zls-vscode sets up zls

Configuring vscode-zig

To auto format on save, set tiehuis.zig to the default formatter in your VSCode settings.json.

  "[zig]": {
    "editor.defaultFormatter": "tiehuis.zig"
  },
Enter fullscreen mode Exit fullscreen mode

This is powered by zig fmt. If you're familiar with JavaScript, zig fmt is like prettier (or like go fmt)

You can also enable build on save for higher quality error messages. For most projects, Zig's compiler is fast enough for that to work well.

  "zig.buildOnSave": true,
Enter fullscreen mode Exit fullscreen mode

Discussion (2)

Collapse
batiati profile image
Rafael Batiati

Very nice post! I would love to know more about setting up VSCode for debuging Zig with gdb/lldb.

Collapse
kristoff profile image
Loris Cro

I've seen somebody make this work, it required adding a tiny little bit of scripting to vscode (mainly to know where the test executable gets saved) and then it would work very easily, IIRC.