---
title: "Add a blank line before a YAML key in Rust"
url: https://recursiveai.org/oneyaml/rust/cookbook/insert-blank-line-before-yaml-key/
description: "Add a blank line before a YAML key in Rust without changing its value. Set the exact gap or put it above the key’s comment."
version: "1.2.0"
---

# Add a blank line before a YAML key in Rust

**oneyaml** is a free, open-source YAML library for Rust from Recursive AI. MIT or Apache-2.0, no unsafe code.

[crates.io](https://crates.io/crates/oneyaml) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-rust) · [Docs](https://recursiveai.org/oneyaml/rust/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/rust/cookbook/insert-blank-line-before-yaml-key/insert-blank-line-before-yaml-key.zip)

Find the key with `doc.get_mut("ports")`, then call `set_blank_lines_before(1)` to put a blank line above it.

```shell
cargo add oneyaml
```

Requires Rust 1.85 or later.

```rust
use oneyaml::Document;

type Error = Box<dyn std::error::Error>;
fn main() -> Result<(), Error> {
    let yaml = "name: 'api'\nports:\n  - 80\n";
    let mut doc = Document::parse(yaml)?;
    let mut ports = doc.get_mut("ports")?;
    ports.set_blank_lines_before(1)?;
    print!("{doc}");
    Ok(())
}
```

Running this snippet prints:

```text
name: 'api'

ports:
  - 80
```

## The full program

Unzip [the example](https://recursiveai.org/oneyaml/rust/cookbook/insert-blank-line-before-yaml-key/insert-blank-line-before-yaml-key.zip), then `cargo run`.

The download reads this `config.yaml` and writes `spaced.yaml` without changing `config.yaml`.

```yaml
name: 'api' # service name
ports:
  - 80
  - 443
```

```rust
use oneyaml::Document;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let yaml = std::fs::read_to_string("config.yaml")?;
    let mut doc = Document::parse(&yaml)?;
    doc.get_mut("ports")?.set_blank_lines_before(1)?;
    std::fs::write("spaced.yaml", doc.to_string())?;
    print!("{doc}");
    Ok(())
}
```

`cargo run` prints the contents of `spaced.yaml`:

```text
name: 'api' # service name

ports:
  - 80
  - 443
```

If adding the line would change a comment’s owner, oneyaml-rust returns `EditError::CommentReattaches` and leaves the text unchanged.

## Variations

**Remove the gap.** Use this YAML as `yaml` in the snippet at the top:

```yaml
name: 'api'

ports:
  - 80
```

Replace `ports.set_blank_lines_before(1)?;` with:

```rust
ports.set_blank_lines_before(0)?;
```

```text
name: 'api'
ports:
  - 80
```

**Put the gap above a comment.** In the snippet at the top, set `yaml` to this input, then replace the two lines that find and edit `ports` with the code below. There’s only one comment in this input.

```yaml
name: 'api'
# Public ports
ports:
  - 80
```

```rust
let comment = doc.comment_ids()[0];
doc.set_comment_blank_lines(comment, 1)?;
```

```text
name: 'api'

# Public ports
ports:
  - 80
```

## Related recipes

- [Set the leading comments above a YAML key](https://recursiveai.org/oneyaml/rust/cookbook/set-leading-yaml-comments/)
- [Find which YAML node owns a comment](https://recursiveai.org/oneyaml/rust/cookbook/explain-yaml-comment-owner/)

## The short answer

On the mutable value returned by `get_mut`, use `set_blank_lines_before(1)`. For a mapping value, [the edit is applied above its key](https://github.com/recursive-ai-oss/oneyaml-rust/blob/14445615f5a5b2895fdaa1da7719584ef15d9d6b/crates/oneyaml/src/document/edit.rs#L8557-L8574), not above the value. That matters when the value spans several lines, as `ports` does here. The count sets the final gap: calling it twice with `1` doesn’t add a second line.

## Do blank lines matter between YAML keys?

A blank line between ordinary block mapping entries is a visual separator. In the example, the name is still a string and `ports` is still the same list of integers. A blank line inside a text value is different. In particular, `|+` and `>+` keep trailing line breaks and empty lines in a block scalar (a multi-line text value). The [blank-line editor rejects changes next to a kept scalar](https://github.com/recursive-ai-oss/oneyaml-rust/blob/14445615f5a5b2895fdaa1da7719584ef15d9d6b/crates/oneyaml/src/document/comment_edit.rs#L1383-L1405) that could change that text. Don’t use this method to add a newline to a string.

A key also needs its own line for a positive gap. If your YAML has keys separated by commas inside `{…}` on one line, there isn’t a line break before each key for this edit to adjust.

## How do you add a blank line before a YAML comment?

If a leading comment introduces the next section, put the space *above the comment* so the explanation stays next to its key. Use the comment’s ID and `set_comment_blank_lines`, as in the second variation. This is for a comment on its own line, not for a `#` note at the end of a value line. The library checks the ownership of nearby comments for both edits: removing a gap can be refused too, for example if it would turn a document header into a comment attached to the first key.
