My favorite Zig feature
home
2026-06-09
My favorite Zig feature
Zig has many awesome features, but there’s one that’s absolutely no. 1 for me: zls formatting features.
Example: I was adding Oniguruma C source code files to build.zig. Relevant code looked like this:
mod.addCSourceFiles(.{<br>.root = b.path(onig_path),<br>.files = &.{<br>"regerror.c",<br>"regparse.c",<br>"regext.c",<br>"regcomp.c",<br>"regexec.c",<br>"reggnu.c",<br>// 40 more lines<br>});
I figured - this is fine - but I don’t like having screenful of C files.
…so I joined first 3 items. First line of the array became like this:
&.{<br>"regerror.c", "regparse.c", "regext.c",<br>// ...
Saved. You know what zls did1? IT MADE CODE BEAUTIFUL:
mod.addCSourceFiles(.{<br>.root = b.path(onig_path),<br>.files = &.{<br>"regerror.c", "regparse.c", "regext.c",<br>"regcomp.c", "regexec.c", "reggnu.c",<br>"regenc.c", "regsyntax.c", "regtrav.c",<br>"regversion.c", "st.c", "onig_init.c",<br>"unicode.c", "ascii.c", "utf8.c",<br>"utf16_be.c", "utf16_le.c", "utf32_be.c",<br>"utf32_le.c", "euc_jp.c", "sjis.c",<br>"iso8859_1.c", "iso8859_2.c", "iso8859_3.c",<br>"iso8859_4.c", "iso8859_5.c", "iso8859_6.c",<br>"iso8859_7.c", "iso8859_8.c", "iso8859_9.c",<br>"iso8859_10.c", "iso8859_11.c", "iso8859_13.c",<br>"iso8859_14.c", "iso8859_15.c", "iso8859_16.c",<br>"euc_tw.c", "euc_kr.c", "big5.c",<br>"gb18030.c", "koi8_r.c", "cp1251.c",<br>"euc_jp_prop.c", "sjis_prop.c", "unicode_unfold_key.c",<br>"unicode_fold1_key.c", "unicode_fold2_key.c", "unicode_fold3_key.c",<br>},<br>});
Small thing, but I have a thing for nicely formatted code. By the way: zls isn’t opinionated and it formats depending on where you put commas/newlines. Both pretty and flexible.
In my mind it’s ✨zig✨ now.
I have auto-formatting on save enabled ↩︎
Przemysław Alexander Kamiński
vel xlii vel exlee
cb | gl | gh | li | rss
Powered by hugo and hugo-theme-nostyleplease.