# pyskills.edit


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

``` python
from fastcore.test import test_eq
```

``` python
#TODO @llmtool
```

    <unknown>:23: SyntaxWarning: invalid escape sequence '\s'

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/pyskills/blob/main/pyskills/edit.py#L51"
target="_blank" style="float:right; font-size:smaller">source</a>

### file_edit

``` python

def file_edit(
    f, name:NoneType=None
):

```

*Call self as a function.*

``` python
_tmp = TemporaryDirectory()
_test_path = f'{_tmp.name}/test.txt'
_test_content = 'alpha\nbeta\ngamma\ndelta\n'
def _test_txt(): return Path(_test_path).read_text()
Path(_test_path).write_text(_test_content)
_test_path
```

    '/var/folders/51/b2_szf2945n072c0vj2cyty40000gn/T/tmp2epejvuy/test.txt'

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/pyskills/blob/main/pyskills/edit.py#L66"
target="_blank" style="float:right; font-size:smaller">source</a>

### insert_line

``` python

def insert_line(
    text:str,
    insert_line:int, # The 1-based line number after which to insert the text (0: before 1st line, n>=1: after nth line)
    new_str:str, # The text to insert
):

```

*Insert new_str at specified line number*

``` python
res = await file_insert_line(_test_path, 0, 'first')
test_eq(_test_txt().splitlines()[0], 'first')
print(res)
```

    @@ -1 +1,2 @@
    +first
     alpha

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/pyskills/blob/main/pyskills/edit.py#L80"
target="_blank" style="float:right; font-size:smaller">source</a>

### str_replace

``` python

def str_replace(
    text:str, old_str:str, # Text to find and replace
    new_str:str, # Text to replace with
    start_line:int=None, # Optional 1-based start line to limit search
    end_line:int=None, # Optional 1-based end line to limit search
    n_matches:int=None, # Max replacements (None=all)
    re_filter:str=None, # If provided, only process lines matching this regex (like g// in ex)
    invert_filter:bool=False, # Invert the filter (like g!// in ex)
):

```

*Replace occurrence(s) of old_str with new_str*

``` python
res = await file_str_replace(_test_path, 'beta', 'BETA')
assert 'BETA' in _test_txt(), f"Expected 'BETA' in file"
print(res)
```

    @@ -2,3 +2,3 @@
     alpha
    -beta
    +BETA
     gamma

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/pyskills/blob/main/pyskills/edit.py#L108"
target="_blank" style="float:right; font-size:smaller">source</a>

### strs_replace

``` python

def strs_replace(
    text:str, old_strs:list, # List of strings to find and replace
    new_strs:list, # List of replacement strings (must match length of old_strs)
    start_line:int=None, # Optional 1-based start line to limit search
    end_line:int=None, # Optional 1-based end line to limit search
    n_matches:int=None, # Max replacements per string (None=all)
    re_filter:str=None, # If provided, only process lines matching this regex (like g// in ex)
    invert_filter:bool=False, # Invert the filter (like g!// in ex)
):

```

*Replace multiple strings simultaneously*

``` python
res = await file_strs_replace(_test_path, ['gamma', 'delta'], ['GAMMA', 'DELTA'])
test_eq(_test_txt().splitlines()[-2:], ['GAMMA', 'DELTA'])
print(res)
```

    @@ -3,3 +3,3 @@
     BETA
    -gamma
    -delta
    +GAMMA
    +DELTA

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/pyskills/blob/main/pyskills/edit.py#L139"
target="_blank" style="float:right; font-size:smaller">source</a>

### replace_lines

``` python

def replace_lines(
    text:str, start_line:int, # Starting line number to replace (1-based indexing)
    end_line:int=None, # Ending line number to replace (1-based, inclusive, negative counts from end, None for single line)
    new_content:str='', # New content to replace the specified lines
):

```

*Replace line range with new content*

``` python
res = await file_replace_lines(_test_path, 2, 3, 'two\nthree\n')
test_eq(_test_txt().splitlines()[1:3], ['two', 'three'])
print(res)
```

    @@ -1,4 +1,4 @@
     first
    -alpha
    -BETA
    +two
    +three
     GAMMA

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/pyskills/blob/main/pyskills/edit.py#L155"
target="_blank" style="float:right; font-size:smaller">source</a>

### del_lines

``` python

def del_lines(
    text:str, start_line:int, # Starting line number to delete (1-based indexing)
    end_line:int=None, # Ending line number to delete (1-based, inclusive, negative counts from end, None for single line)
    re_filter:str=None, # If provided, only delete lines matching this regex (like g// in ex)
    invert_filter:bool=False, # Invert the filter (like g!// in ex)
):

```

*Delete line range*

``` python
res = await file_del_lines(_test_path, 1)
test_eq(_test_txt().splitlines()[0], 'two')
print(res)
```

    @@ -1,2 +1 @@
    -first
     two

``` python
_nb_path = f'{_tmp.name}/test.ipynb'
_tnb = Notebook(new_nb([_test_content, 'other cell']))
_tnb.save(_nb_path)
_cid, _oid = _tnb[0].id, _tnb[1].id
def _nb_src(): return Notebook.open(_nb_path)[_cid].source
_cid, _oid
```

    ('8bf920d2', 'aca4e9d0')

``` python
res = await cell_insert_line(_nb_path, _cid, 0, 'first')
test_eq(_nb_src().splitlines()[0], 'first')
print(res)
```

    @@ -1 +1,2 @@
    +first
     alpha

``` python
res = await cell_str_replace(_nb_path, _cid, 'beta', 'BETA')
assert 'BETA' in _nb_src(), f"Expected 'BETA' in cell"
print(res)
```

    @@ -2,3 +2,3 @@
     alpha
    -beta
    +BETA
     gamma

``` python
res = await cell_strs_replace(_nb_path, _cid, ['gamma', 'delta'], ['GAMMA', 'DELTA'])
test_eq(_nb_src().splitlines()[-2:], ['GAMMA', 'DELTA'])
print(res)
```

    @@ -3,3 +3,3 @@
     BETA
    -gamma
    -delta
    +GAMMA
    +DELTA

``` python
res = await cell_replace_lines(_nb_path, _cid, 2, 3, 'two\nthree\n')
test_eq(_nb_src().splitlines()[1:3], ['two', 'three'])
print(res)
```

    @@ -1,4 +1,4 @@
     first
    -alpha
    -BETA
    +two
    +three
     GAMMA

``` python
res = await cell_del_lines(_nb_path, _cid, 1)
test_eq(_nb_src().splitlines()[0], 'two')
print(res)
```

    @@ -1,2 +1 @@
    -first
     two

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/pyskills/blob/main/pyskills/edit.py#L225"
target="_blank" style="float:right; font-size:smaller">source</a>

### view_cell

``` python

def view_cell(
    fname:str, id:str, nums:bool=True
):

```

*Show cell source with optional line numbers*

[`view_cell`](https://AnswerDotAI.github.io/pyskills/edit.html#view_cell)
displays a cell’s source with line numbers, useful for checking content
before editing:

``` python
print(view_cell(_nb_path, _cid))
```

         1 │ two
         2 │ three
         3 │ GAMMA
         4 │ DELTA

------------------------------------------------------------------------

<a
href="https://github.com/AnswerDotAI/pyskills/blob/main/pyskills/edit.py#L230"
target="_blank" style="float:right; font-size:smaller">source</a>

### view_nb

``` python

def view_nb(
    fname:str, incl_out:bool=False
):

```

*Show notebook source as concise xml, optionally including output if
`incl_out`*

``` python
view_nb(_nb_path)
```

    '<nb path="test.ipynb"><code id="8bf920d2">two\nthree\nGAMMA\nDELTA</code><code id="aca4e9d0">other cell</code></nb>'
