Skip to main content

Splice Replace

splice-replace is a Python utility to extract, process, and replace text in a file by running an external script on the extracted text.

Usage

./splice_replace.py <script_to_run> <file_path> <start_line> <start_col> <end_line> <end_col>
./splice_replace.py 'sed s/big/small/' myfile.txt 2 10 4 15

Example

Use sed on section of file:

echo "01 First Line" > myfile.txt
echo "02 Second Line" >> myfile.txt
echo "03 Third Line" >> myfile.txt
echo "04 Fourth Line" >> myfile.txt
echo "05 Fifth Line" >> myfile.txt

./splice_replace.py 'sed s/Second/2nd/' myfile.txt 2 1 2 17

Which results in the file:

01 First Line
02 2nd Line
03 Third Line
04 Fourth Line
05 Fifth Line

Use sort on section of file:

echo "01 MUPPET CHARACTERS" > myfile.txt
echo "02 HERE IS A LIST OF MUPPET CHARACTERS ALPHABETICALLY" >> myfile.txt
echo "03" >> myfile.txt
echo "Kermit" >> myfile.txt
echo "Gonzo" >> myfile.txt
echo "Beaker" >> myfile.txt
echo "Animal" >> myfile.txt
echo "Fozzie Bear" >> myfile.txt
echo "09" >> myfile.txt
echo "10 THAT CONCLUDES THE LIST" >> myfile.txt

./splice_replace.py 'sort' myfile.txt 4 1 9 1

Which results in the file:

01 MUPPET CHARACTERS
02 HERE IS A LIST OF MUPPET CHARACTERS ALPHABETICALLY
03
Animal
Beaker
Fozzie Bear
Gonzo
Kermit
09
10 THAT CONCLUDES THE LIST

Installation

From source:

git clone
cd splice-replace
pip install -r requirements.txt
python setup.py install

Using pip:

pip install splice-replace

Motivation

Intellij IDEA has a feature called "External Tools" which allows you to run external scripts. You can pass the script special variables like $FilePath$, $SelectionStartLine$, $SelectionStartColumn$, $SelectionEndLine$, and $SelectionEndColumn$. I wanted to be able to configure the external tool to run a script on a selection of text in a file such that the script receives that selection as stdin and gives the replacement as stdout. IntelliJ IDEA does not support this feature directly, so I wrote this utility.

For example if you wanted to run the sort command on a selection of text in a file, you could configure an external tool to run. First, you would create the external tool in Settings -> Tools -> External Tools, then give it a name like "sort", set the program to /path/to/splice-replace and the arguments to "sort" $FilePath$ $SelectionStartLine$ $SelectionStartColumn$ $SelectionEndLine$ $SelectionEndColumn$.

Then you could select a section of text in a file and run the external tool "sort" on that selection.

Of course, you can replace sort with your own utility or script.

Metadata

Release files for splice-replace 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for splice-replace 0.1.2
File Size Uploaded
splice_replace-0.1.2.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for splice-replace 0.1.2
File Interpreter ABI Platform
splice_replace-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.6 kB

Release files / splice_replace-0.1.2.tar.gz

Download URL splice_replace-0.1.2.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
3002fb52d2f5f351a4c9fc56d52ae1be515d9488c9c2880ded8a9d113d2eafee
BLAKE2b-256 checksum
How to use checksums
0ffe9d9be12166eba027c1d14462618521a92e65134a398e1047c55577a1610b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release files / splice_replace-0.1.2-py3-none-any.whl

Download URL splice_replace-0.1.2-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
58a77a5f8bc218ea8dfd8a6ab274e0c8261d529be8b6f9303cb35fd7bc885f8a
BLAKE2b-256 checksum
How to use checksums
e4aa5c1ac51156e9261c8efdc833b13e51d01186f8b49240b3ddf70e22bd8730
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.4

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page