Skip to main content

A CLI utility for replacing multi-line strings in files. Supports textual replacements with wildcard matching indentation-aware replacements.

Project description

multi-line-replacer (mlr)

Copyright 2025 Caleb Evans
Released under the MIT license

tests Coverage Status

multi-line-replacer (mlr) is a CLI utility for replacing multi-line hunks of strings across one or more files. Matching is mostly textual, but wildcard matching is supported, and replacements are indentation-aware.

Installation

You can install multi-line-replacer via your preferred global package manager for Python:

# via pip
pip3 install multi-line-replacer
# via uv
uv tool install multi-line-replacer
# via pipx
pipx install multi-line-replacer

Usage

The workflow takes one or more files on which to run replacements, and then one or more "replacement rule" files with the -r flag:

mlr .github/workflows/*.yml -r example-rules/uv-gha.md

Each replacement rule must be a Markdown file with one or more pairs of GFM fenced code blocks (see documentation). Every odd code block represents the target text to replace, and every even code block represents the textual replacement. All other Markdown formatting is ignored, so feel free to add headings, explainer text, or anything else!

This rule replaces flake8 with ruff in a Github Actions linting workflow.

## flake8

```yml
- name: Run flake8
  run: flake8 MATCH_UNTIL_END_OF_LINE
```

## ruff

```yml
- name: Run ruff
  run: |
    uv run ruff check .
    uv run ruff format --check .
```

[!NOTE] The language specifier at the start of each code block is ignored by the utility. Still, it is highly recommended to specify so that syntax highlighting is enabled in your editor (i.e. it's for you, not the tool).

Wildcard Matching

There are two special wildcard variables:

  • MATCH_UNTIL_END_OF_LINE ([^\n]*)
  • MATCH_ALL_BETWEEN ([^\n]*?)

These variables can be used anywhere in any code block representing the target text to match. Because these names are unique enough, word boundaries are not required around them (e.g. vMATCH_UNTIL_END_OF_LINE is allowed).

More Examples

To better understand the expected rules format and what's allowed, please see the example-rules directory.

About

Multi-Line Replacer was built as my solution to an intermediate need I had while writing a large migration script. I had 17 Python projects using old tooling, and the script was written to migrate these projects to uv and ruff.

Part of this migration process necessitated performing textual replacements on multi-line hunks of code. Regular expressions and editors like VS Code could somewhat achieve this, although they required escaping special characters and carefully specifying indentation. In other words, those tools proved to be too rigid and inflexible.

Given these constraints, I conceived of a utility that could perform multi-line replacements with a friendlier authoring experience and greater indentation awareness. The implementation took several iterations to achieve positive results, but by the end, it contributed significantly to the successful migration of all 17 projects. From there, I decided to release it to the world as a more flexible and automated system for replacing multi-line hunks of code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

multi_line_replacer-1.0.0b6.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

multi_line_replacer-1.0.0b6-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file multi_line_replacer-1.0.0b6.tar.gz.

File metadata

File hashes

Hashes for multi_line_replacer-1.0.0b6.tar.gz
Algorithm Hash digest
SHA256 b2893c1d022ecc9ff810abecf465dafcf7884c3e275857bc1f424b026491b727
MD5 61834fb3be98c497b54074946a1be973
BLAKE2b-256 3c69562ca399ee434c1e4ee702c6cd5bde7ed4a0bb208f41f90ac6fb3d0c733b

See more details on using hashes here.

File details

Details for the file multi_line_replacer-1.0.0b6-py3-none-any.whl.

File metadata

File hashes

Hashes for multi_line_replacer-1.0.0b6-py3-none-any.whl
Algorithm Hash digest
SHA256 b28d4a363a7b72e26967bf3e05c21e331e1390ca90690a421ca53e25fa995d15
MD5 59e6828debf0eec8bc9cfb5f6ff6ea12
BLAKE2b-256 de1ec859acc253de88c6416cae1ae6fb9d36977dee2df141e33bb26b09971664

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page