# How to Disable the Ruff Formatter for a Block of Code


[Ruff](https://pydevtools.com/handbook/reference/ruff.md)'s formatter collapses extra whitespace, so a hand-aligned matrix or a column-formatted data table comes back as a single dense line after `ruff format`. The `# fmt: off` and `# fmt: skip` pragma comments tell the formatter to leave a region alone, exactly as Black does.

These comments control the **formatter** only. To stop the **linter** from reporting a rule, see [how to disable Ruff rules for a block of code](https://pydevtools.com/handbook/how-to/how-to-disable-ruff-rules-for-a-block-of-code.md).

## Wrap the Block in fmt: off and fmt: on

Put `# fmt: off` on its own line before the block and `# fmt: on` after it. The formatter preserves every line in between.

```python
# fmt: off
ROTATION = [
    1,  0,  0,
    0,  1,  0,
    0,  0,  1,
]
# fmt: on
```

Confirm the block survives a format pass:

```bash
uv run ruff format --check --diff .
```

The aligned matrix is left untouched. Code outside the `# fmt: off`/`# fmt: on` pair is still formatted normally.

## Skip a Single Statement with fmt: skip

For one line, end the statement with `# fmt: skip`. The formatter leaves that statement's spacing as written.

```python
grid = [1,  2,  3,  4]  # fmt: skip
```

`# fmt: skip` attaches to a whole statement, including a decorator, a function or class definition, or a `case` header. It cannot suppress one item inside a list or expression.

## Keep Suppression Comments Where They Work

`# fmt: off` and `# fmt: on` are enforced at the statement level, so placing either mid-expression does nothing. A `# fmt: skip` inside a list or argument has no effect either.

Ruff flags these dead comments with `RUF028` (invalid-formatter-suppression-comment):

```python
values = [
    1, 2, 3,  # fmt: skip  -> RUF028: this comment cannot take effect here
]
```

Keep `RUF028` enabled so a misplaced pragma surfaces in `ruff check` instead of silently letting the formatter rewrite code you meant to protect.

## Map yapf Comments to Ruff

Projects migrating from yapf can leave their `# yapf: disable` and `# yapf: enable` comments in place. Ruff treats them as equivalent to `# fmt: off` and `# fmt: on`.

## Learn More

- [Ruff format suppression](https://docs.astral.sh/ruff/formatter/#format-suppression)
- [RUF028: invalid-formatter-suppression-comment](https://docs.astral.sh/ruff/rules/invalid-formatter-suppression-comment)
- [How to disable Ruff rules for a block of code](https://pydevtools.com/handbook/how-to/how-to-disable-ruff-rules-for-a-block-of-code.md)
- [How to migrate from Black to the Ruff formatter](https://pydevtools.com/handbook/how-to/how-to-migrate-from-black-to-ruff-formatter.md)
