Steering Zig Fmt

Lobsters Hottest Tools

Summary

A blog post describing two tips for using `zig fmt` effectively, highlighting its 'steerable' formatting approach where trailing commas and line breaks control layout decisions, and showcasing columnar array formatting.

<p>I really like the idea of accounting for newline + numbers per line! Any other similar features people have noticed and liked in other language formatters?</p> <p><a href="https://lobste.rs/s/auxtwd/steering_zig_fmt">Comments</a></p>
Original Article
View Cached Full Text

Cached at: 05/09/26, 06:36 AM

# Steering Zig Fmt Source: [https://matklad.github.io/2026/05/08/steering-zig-fmt.html](https://matklad.github.io/2026/05/08/steering-zig-fmt.html) May 8, 2026Two tips on using`zig fmt`effectively\. Read this if you are writing Zig, or if you are implementing a code formatter\. For me,`zig fmt`is better than any other formatter I used:`rustfmt`, the one in IntelliJ,`deno fmt`\.`zig fmt`is steerable\. For every syntactic construct, it has several variations for how it might be laid out\. The variation used is selected by looking at what’s currently in a file\. Easier to show a pair of examples: ``` f(1, 2, 3); // -> zig fmt -> f(1, 2, 3); ``` ``` f(1, 2, 3,); // -> zig fmt -> f( 1, 2, 3, ); ``` Depending on the trailing comma, function call is formatted on a single line, or with one argument per line\. The way this plays out in practice is that you*decide*how you want to lay out the code, add a couple of`,`, hit the reformat shortcut \([, pis mine](https://matklad.github.io/2024/10/08/two-tips.html#s-1)\), and`zig fmt`does the rest\. For me, this works better than the alternative of the formatter guessing\. 90% of great formatting are blank lines between logical blocks and tasteful choice of intermediate variables, so you might as well lean into key choices, rather than eliminate them\. I know of one non\-trivial formatting customization point: columnar layout for arrays: ``` .{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, }; ``` One would think that trailing comma would lead to a number\-per\-line layout, but, for arrays,`zig fmt`also takes note of the first line break\. In this case, the line break comes after the first three items, so we get three numbers per line, aligned: ``` .{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, }; ``` How cool is that\! Furthermore, with judicious use of`\+\+`, you can vary the number of items per line\. When I need to pass`\-\-key``value`pairs to subprocess, I often go for formatting like this: ``` try run(&(.{ "aws", "s3", "sync", path, url } ++ .{ "--include", "*.html", "--include", "*.xml", "--metadata-directive", "REPLACE", "--cache-control", "max-age=0", })); ```

Similar Articles

Zig by Example

Hacker News Top

A hands-on introduction to the Zig programming language via annotated examples, covering basic to advanced topics. Inspired by Go by Example.

Zig Structs of Arrays (2024)

Hacker News Top

Explains how Zig's comptime and type reflection enable creating struct-of-arrays (SoA) data structures like MultiArrayList, which improve cache performance in high-performance applications.

Returning to Zig

Lobsters Hottest

The author describes their journey from Zig to Rust and back to Zig, exploring the trade-offs between stability and expressiveness in programming languages.

Writing a C Compiler, in Zig

Hacker News Top

A developer documents their experience building a C compiler named paella in Zig, following Nora Sandler’s tutorial series.

Zig by Example

Lobsters Hottest

Zig by Example is a hands-on introduction to the Zig programming language, featuring annotated example programs for version 0.16.