insert_text

Operation inserting text on a given line

Example

The following example inserts text into a file named settings.py:

examples/operations/insert_text.yml
steps:
    "Find middleware tag in settings":
        name: find_tagged_line
        file_path: "examples/data/settings.py"
        tag: "# qwikstart: middleware"
    "Insert new middleware under tag":
        name: insert_text
        file_path: "examples/data/settings.py"
        text: '"django.contrib.auth.middleware.AuthenticationMiddleware",'

To set up this example, we first use the find_tagged_line operation to find the line number and column number where the tag "# qwikstart: middleware". These values are added to the global context and match variables expected by insert_text. The insert_text operation will then insert text below the tagged line:

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    # qwikstart: middleware
    "django.contrib.auth.middleware.AuthenticationMiddleware",
]

Required context

file_path

Path to file relative to the current working directory.

text

Text that will be inserted.

line

Line number where text will be inserted.

column

Column where the text will be inserted.

Optional context

line_ending

default: '\n'

Text appended to the end of inserted text.

match_indent

default: True

See also