Nodes/ComfyUI-Text-Utility/Replace Variables
ComfyUI Node

Replace Variables

Declare $variables at the top of your prompt and reference them everywhere

By watarika·Created 2 years ago·Updated 10 months ago· 3
Replace Variables
    • replaced_text
    text

    Simple idea: your prompt gets to declare variables. Feed it

    $animal="cat" $color="black" The $color $animal sleeps on the sofa.
    

    and you get back The black cat sleeps on the sofa. The definitions are pulled out of the text and every $name reference is swapped in. Change one definition, every use updates.

    Why bother? Because when you batch through prompts or reuse a workflow across subjects, you can define a character block or a style phrase once at the top of the box and reference it in a dozen places. It's the poor man's templating - and for prompt work, that's usually the right amount of machinery. This is the node to reach for when you keep pasting the same quality phrase into five spots and then forgetting to update four of them.

    How it works

    Two regexes do everything. One finds definitions - $name="value", where the name starts with a letter or underscore and the value is anything inside double quotes - and collects them into a dictionary. The other finds $name references and substitutes. Definitions are removed from the output, and the result is trimmed of the stray whitespace they leave behind.

    The detail worth knowing is in the reference pattern: it deliberately stops at a __ boundary. That means $variable references never clobber wildcard syntax - __haircolor__ passes through untouched because the matcher halts before the double underscore. This is a deliberate design choice so the $-system and Impact Pack's __-wildcards coexist, which matters if you're plumbing this into wildcard processing later (the pack's loop node exists exactly for that chain).

    Inputs and outputs

    • text (STRING, multiline) - definitions plus references plus the rest of your prompt.
    • replaced_text (STRING) - the substituted result, ready for a text encoder.

    Nothing else. One input, one output, no dependencies, no Impact Pack required.

    Gotchas

    • Values must be in double quotes. $animal=cat is not a definition, so $animal stays literal and goes to your encoder as-is.
    • Names are case-sensitive: $Color won't resolve from a $color definition.
    • Undefined references pass through unchanged. This node won't clean them up - if you want stray $vars stripped, that's the remove_undefined_variables toggle on the pack's "Replace Variables and Process Wildcard (Loop)" node.
    • The reference matcher stops at non-alphanumeric characters, so $animal's won't substitute - the apostrophe cuts it off. Keep references on their own.

    Where it shines

    Batch workflows where the subject changes but the scaffolding doesn't. Define the character once:

    $char="(white shirt, black skirt, red hair:1.2)" $shot="(cinematic lighting, depth of field:1.1)"
    $char standing on a bridge, $shot, medium shot
    

    Change $char and every generation updates with it. Simple, readable, and it never forgets to update the fourth copy.

    Installing

    Ships with watarika/ComfyUI-Text-Utility. ComfyUI Manager → search "ComfyUI-Text-Utility" → Install → restart, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/watarika/ComfyUI-Text-Utility
    

    No pip requirements, no models, nothing else to install - pure Python string work.

    Categorytext

    Inputs (1)

    NameTypeDefaultDescription
    textSTRING

    Outputs (1)

    NameTypeDescription
    replaced_textSTRING