Introducing movefmt: Code Formatter for Move on Aptos

Aptos Labs
Aptos
Published in
3 min readSep 20, 2024

by MoveBit and Aptos Labs

The community has been longing for a formatting tool for the Move language. In response, MoveBit has collaborated with Aptos Labs to develop movefmt, which makes Move code much easier to write, read, and maintain — greatly improving the development experience on Aptos.

Why are formatters popular in modern programming environments?

  • They handle the tedious task of making code match a chosen format style, e.g., respecting indentation rules, line breaks, etc.
  • They lead to consistently formatted code across a project, avoiding mixing of conflicting coding styles, and easing changes to code written by other team members.
  • They increase readability and auditability of code, freeing up readers to focus on semantic changes, rather than reformatting changes.

A code formatter automatically edits the superficial details of code formatting — spacing between code items, line breaks, and tabination — to make code more readable to other developers while leaving the program’s semantics intact. Even if a particular code style may not be agreed upon by everybody (for example, the use of spaces vs tabs), making a consistent choice across code files eliminates superficial differences that hide underlying logic.

Format your code

movefmt is integrated into the Aptos CLI. To begin using it, first install it using the CLI update command.

# Install movefmt for first time usage
aptos update movefmt

Similar to compilation and testing, you can use the following command to format the Move package:

# Format the Move package
aptos move fmt

movefmt also provides different options to configure how the code will be formatted. Here is the default configuration, which says 1) each line can have at most 90 characters, 2) the ident is 4 spaces, 3) each tab is identical to 4 spaces and 4) when a tab is inserted, it will be automatically replaced by 4 spaces.

max_width = 90
indent_size = 4
tab_spaces = 4
hard_tabs = false

To override the default option, users can either specify a configuration file movefmt.toml and put it in Move package directory or manually specify it in the command line:

# When formatting the code, set `max_width` to 80 and `indent_size` to 2
aptos move fmt --config max_width=80,indent_size=2

There may be some cases where you spent a lot of time formatting code a particular way for readability, and you may not like what movefmt did to your code. As an escape hatch for those cases, you can use the #[fmt::skip] attribute on a function to skip formatting code for that function only.

By using movefmt, the code becomes nicer to read and easier to maintain. Here is an example showing the difference before and after using it:

What’s next

Aptos Labs remains committed to improving the developer experience for builders using Move on Aptos.

Moving forward, we will maintain and update the formatter tool with all the new and upcoming features of the Move language. If you’re interested in shaping the style guidelines for Move, we would love to hear your comments and feedback at https://github.com/movebit/movefmt/issues.

--

--

Aptos Labs
Aptos

Aptos Labs is a premier Web3 studio of engineers, researchers, strategists, designers, and dreamers building on Aptos, the Layer 1 blockchain.