Skip to main content

Motoko formatting tools

Overview

The Motoko Prettier plugin can be used to format and validate Motoko source code files. It can be used through the Prettier CLI or through VS Code.

Alternatively, the Node.js package mo-fmt is a standalone Motoko formatter tool.

Motoko Prettier plugin

Installation

To install the Motoko Prettier plugin, first download and install Node.js.

Then, create a new Motoko project or navigate into an existing project directory. Learn more about creating a Motoko project.

Then, run the following command in the project directory:

npm install --save-dev prettier prettier-plugin-motoko

Using the Prettier CLI

You can format Motoko files through the Prettier CLI using the command:

npx prettier --write --plugin=prettier-plugin-motoko **/*.mo

To validate if your Motoko files are formatted properly, use the following command:

npx prettier --check --plugin=prettier-plugin-motoko **/*.mo

Using VS Code

The Motoko Prettier plugin works out of the box with the Motoko extension for VS Code.

It is also compatible with the the Prettier extension.

Ignoring code

You can set code to be skipped from formatting or being validated by using a prettier-ignore comment:

// prettier-ignore
func ignored<A>(a:A){a};

func formatted<B>(b : B) { b };

mo-fmt

Installation

To install mo-fmt, run the following command:

npm install mo-fmt

Then, format and validate the format of Motoko code files, run the commands:

mo-fmt **/*
mo-fmt -c **/*

References