• Čeština
  • Español
  • Italiano
  • Deutsch
  • Polski
  • Français
  • 日本語

G-code substitutions

Relevant for:
Last updated a year ago
This article is also available in following languages: 
This feature will be available in PrusaSlicer 2.4.1

PrusaSlicer offers simple G-Code post-processing by specifying find and replace pairs, with optional regex matching, case sensitivity and whole word matching, similar to what common text editors offer. Regular expressions are very powerful and versatile tool that allows to set up even quite complicated substitutions. If you need more complicated post-processing, it is possible to use an external post-processing script instead.

The feature accessible from Print Settings -> Output options.

The G-Code is processed before it is previewed, so your changes will be visible in the G-Code preview. It may be helpful to turn on 'Verbose G-Code' to make PrusaSlicer generate extra annotations, which you can then match with a regex.

Regular expression syntax

Regular expression syntax of Perl is supported as described in boost Perl regular expression syntax, where a "single line" modifier mimics the Perl's s/ modifier. With regular expressions active, the "Replace with" pattern supports the "extended" syntax as described in boost extended format syntax. Lookahead and lookbehind are supported by the regular expression engine, but only for fixed length patterns. With regular expression disabled, n, r, t, and placeholders are supported in both "Find" and "Replace with" patterns similarly to Notepad++'s "extended" syntax.

The G-code find/replace post-processor processes G-code in blocks, where a single G-code block is generated for each layer starting with the Z up to move to the next layer. Multiple find/replace patterns are applied one by one to one G-code block before processing the next G-code block. Matching multi-line patterns is possible, however only inside one G-code block, most likely inside a single layer. Regular expressions are powerful but tricky, thus we recommend the regular expressions 101 online playground to get familiar with regular expressions and to test and fine tune the regular expressions modifying G-code before entering them into PrusaSlicer. Don't forget to switch the regex flavor to ECMAScript in the regular expressions 101 online playground.



An example of a quite powerful G-code modification by a reasonably complex regular expression: Increase the extrusion rate of top solid infill from the default 95 percent to 98 percent.

(;TYPE:Top solid infill\n)(.*?)(;TYPE:|$)(?!Top solid infill)

Replace with:
${1}M221 S98\n${2}M221 S95\n${3}

Modifiers: regexp, single line

Was this article helpful?


Still have questions?

Still have questions?

If you have a question about something that isn't covered here, check out our additional resources.
And if that doesn't do the trick, you can send an inquiry to [email protected] or through the button below.

Contact us