Recursive AI

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.

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

Terminal
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, 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

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, 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 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.

All oneyaml Rust recipes