Skip to content

Formatting code

nvs fmt rewrites Novis files in one fixed layout. A body is indented by four spaces. Each statement and each closing brace is on its own line. A plain string uses single quotes.

Inside a line, the two tokens decide if there is one space or no space between them. $a+$b becomes $a + $b. Several blank lines in a row become one.

The command has no style options, so every project gets the same layout. It has no line width, and a line break that you wrote stays. You can break a condition, a ternary or a chain of +, && or another operator. Then each part gets its own line, and the operator is at the start. A . chain keeps your lines. Running nvs fmt on a formatted file changes nothing.

Give nvs fmt a file to format it in place:

Terminal window
nvs fmt app.nvs

Give it a folder to format every .nvs file in that folder and in its subfolders:

Terminal window
nvs fmt src

In a folder, nvs fmt skips the files that a .gitignore or .ignore file names. It also skips each folder named target or vendor, and each folder whose name starts with a dot. Which files the editor reads has the details. A file or folder that you name in the command is always formatted.

A file with a syntax error is not changed. nvs fmt prints the error and continues with the next file.

--diff prints the changes and writes nothing. This file has a brace at the end of a line, two statements on one line and uneven indentation:

Terminal: diff
nvs fmt --diff cart.nvs--- cart.nvs+++ cart.nvs@@ -1,11 +1,15 @@ <?nvs-class Cart {-  public int $total = 0;-      public function add(int $price): void {-        $this->total = $this->total + $price; }+class Cart+{+    public int $total = 0;+    public function add(int $price): void+    {+        $this->total = $this->total + $price;+    } } Cart $cart = new Cart();-$cart->add(1200); $cart->add(350);-   echo $cart->total, "\n", "done";+$cart->add(1200);+$cart->add(350);+echo $cart->total, "\n", 'done';exit 1

Lines that start with - are removed, and lines that start with + are added. The exit code is 1, because the file would change.

--check writes nothing. It prints the name of each file that would change:

Terminal: check
nvs fmt --check cart.nvs order.nvsorder.nvsexit 1nvs fmt order.nvsnvs fmt --check cart.nvs order.nvs

The exit code tells you what nvs fmt --check found:

Exit code Meaning
0 Every file is formatted.
1 A file would change.
2 A file has a syntax error.

A file with a syntax error gives the exit code 2, and nvs fmt prints the error:

Terminal: parse-error
nvs fmt --check draft.nvserror: draft.nvs: not formatted, because it does not parse (1 error(s))error[E0102]: expected an expression  --> draft.nvs:2:14  |2 | int $count = ;  |              ^ not found hereerror: aborting due to 1 errorTo read what a code means and how to fix it, run `nvs agent show <code>`.exit 2

On a build server, run nvs fmt --check . in the root of your project. Most build servers stop the build when a command exits with a code that is not 0. The build then fails when a file is not formatted.

The editor extension formats a file with the same nvs fmt, so the editor and the command line always give the same result.

Read more in Reference: nvs fmt.