2.2.12. Customizing PrusaSlicer g-code

Customize gcode in PrusaSlicer for greater control of the 3D printing process.

PrusaSlicer allows customizing g-code to be inserted at different times during a print:

  • Start G-code is inserted at the start of a print job. It is used to heat the nozzle and bed, perform mesh bed leveling, print a prime line and any other steps that need to be done before a print starts.

  • End G-code is inserted at the end of a print job. It is used to turn of heaters, park the nozzle and turn off stepper motors.

  • Before layer change G-code is inserted into a print job before each new layer is printed. It is usually left blank, but can be used to insert comments or change settings such as temperatures when printing temperature towers.


If you are calibrating your filament settings for a Prusa i3 Mk3-series printer, read up on extrusion multiplier adjustment notes for the PrusaSlicer default system presets. Slicer Placeholders

If you’re coming to 3D printing with a background in programming, you’re probably looking for variables. These are known as “placeholders” in 3D printing, and are only interpreted and applied at slice time. You can’t create new placeholders, and you can’t change the values of placeholders. They are essentially immutable objects, or constants.

  • Placeholders are a feature of the slicer, not the g-code language.

  • The available placeholders depend entirely on the slicer you are using. Every slicer supports different features and configuration settings. Equivalent features may not be available between slicers.

  • Placeholder names for identical settings differ between slicers.

  • The syntax for using placeholder varies with every slicer. In PrusaSlicer, placeholder names must be surrounded by square brackets ([]). In Ultimaker Cura, placeholder names must be surrounded by curly braces ({}).

Unfortunately, this means that you will need to spend some time learning the supported features and syntax for every slicer. PrusaSlicer placeholders

PrusaSlicer offers a large number of placeholders that you can use in custom g-code.

  • Profile settings for printers, prints, and filaments are available as placeholders. Any value that can be set in a profile is available.

  • A small number of slice-time placeholders are available to identify the current layer number or z-height during slicing. Configuration placeholders

Most settings in PrusaSlicer can used as placeholders. There are two ways to identify the available setting names.

  • When using PrusaSlicer, hover your mouse over a setting field, the pop-up tooltip help will provide a summary of the setting and identify a parameter name.

  • If you open a .gcode file sliced with PrusaSlicer, the settings used for slicing are listed at the end of the file. Any parameter name (to the left of the equals sign (=) can be used as a placeholder.

When writing gcode, enclose placeholders within square brackets (e.g. [layer_height]). At slice time, the placeholder will be replaced with the value of that setting in the generated gcode. (An exception is when using placeholders in conditional checks. See below.)

Here are the most commonly used PrusaSlicer settings placeholders.

Table 2.1 PrusaSlicer Settings Placeholders




Bed Temperature


Bed temperature for all layers, over-ridden by first layer setting.

First Layer Temperature


First Layer Bed Temperature


Prusa Printer Model


Nozzle diameter


Used at print time to compare slicer and current printer nozzle sizes. Currently limited to 0.25, 0.40 and 0.60mm nozzle sizes.

Current layer number


Used only in layer change gcode.

Current layer height


Used only in layer change gcode. Slice-time placeholders

A number of values can be used to insert values into your gcode while slicing. These are useful, for example, to insert gcode based on the current layer number or layer height.

Here are the most commonly used PrusaSlicer slice-time placeholders.

Table 2.2 PrusaSlicer Settings Placeholders




Current layer number


The height of the current layer in mm.

Current layer height


The height of the top of current layer in mm. Conditional g-code

PrusaSlicer – originally based on Slic3r – supports some basic conditional g-code generation. You can do to a very limited amount of “programming” using PrusaSlicer’s if, elsif, else, or endif syntax.

A few notes on syntax:

  • Each if, elsif, else, or endif statement must be enclosed within curly braces ({}).

  • Any gcode appearing between statements will be included if the preceding condition is true.

  • A single line of gcode can appear between conditions on a single line.

  • Multiple lines of gcode can appear on multiple lines, surrounded by conditions.

  • Placeholders within condition checks (within curly braces) are not surrounded by square brackets ([]).

  • Else-if conditions are written as elsif.

Here’s a working example showing basic if-else syntax:

1{if printer_model=~/.*MK3S.*/}
2M117 Mk3S detected, no warmup required
4M117 Mk3 Waiting for PINDA warmup
5M860 S35 ; wait for PINDA temp to stabilize

Here’s another example showing if-elsif syntax:

 1{if layer_z <= 10}M104 S235
 2{elsif layer_z <= 20}M104 S230
 3{elsif layer_z <= 30}M104 S225
 4{elsif layer_z <= 40}M104 S220
 5{elsif layer_z <= 50}M104 S215
 6{elsif layer_z <= 60}M104 S210
 7{elsif layer_z <= 70}M104 S205
 8{elsif layer_z <= 80}M104 S200
 9{elsif layer_z <= 90}M104 S195
10{elsif layer_z <= 100}M104 S190
11{elsif layer_z <= 110}M104 S185


  • Finish conditional g-code

  • Operators

  • Regular expressions

  • Functions (min, max, int)

Contact and feedback

You can find me on the Prusa support forums or Reddit where I lurk in many of the 3D printing-related subreddits. I occasionally drop into the Official Prusa 3D discord server where I can be reached as bobstro (bobstro#9830). You can email me directly at projects@ttlexceeded.com.

Last edited on Apr 03, 2021. Last build on Feb 28, 2023.