G-code

On this page 7

G-code is the plain-text machine control language that tells a 3D printer, CNC mill or laser cutter exactly where to move, how fast to travel, which temperatures to hold and how much material to deposit. In 3D printing & making, it is the final output of the slicer: the tool that turns a 3D model into thousands of numbered instructions that the printer firmware executes one line at a time.

Within Slicers & print settings, G-code is where every choice becomes concrete. Layer height, infill, retraction and speed exist in the slicer as parameters, but the printer only ever sees coordinates, feed rates and extrusion values. Understanding the file therefore helps practitioners diagnose print defects, tune start routines and move jobs between machines safely.

How G-code drives a printer

G-code works as a sequential list of commands, each consisting of a letter-number code followed by parameters. The firmware, such as Marlin, Klipper or RepRapFirmware, reads each line, plans the motion and converts it into stepper motor pulses and heater control signals. The language descends from the RS-274 standard developed for numerically controlled machine tools, later formalized as ISO 6983, although 3D printer dialects have diverged considerably from that base.

A typical movement line reads G1 X120.5 Y84.2 E2.315 F1800. Here G1 is a linear move, X and Y are target coordinates in millimeters, E is the extruder axis expressed as filament length, and F is the feed rate in millimeters per minute. Because the printer remembers its modal state, later lines can omit parameters that do not change.

The table below lists the parameter letters that appear on almost every line of a printing file.

LetterMeaningTypical unit
X, Y, ZTarget position on each axisMillimeters
EExtruder position or filament lengthMillimeters of filament
FFeed rate for the moveMillimeters per minute
SValue such as temperature or fan dutyDegrees Celsius or 0-255
PDuration or tool indexMilliseconds or index
Common parameter letters in printer G-code

G commands and M commands

Printer files mix two families of instructions: G commands control geometry and motion, while M commands control machine functions such as heaters, fans and the motor drivers. A few additional letters, such as T for tool selection on multi-extruder machines, complete the vocabulary. Exact behavior depends on the firmware, so the same code can differ slightly between Marlin and Klipper.

The following commands account for the vast majority of lines in a sliced file and are worth recognizing on sight.

CommandFunctionPractical note
G0 / G1Travel move / linear extruding moveMost lines in any file
G28Home the axesRequired before printing
G29Run automatic bed levelingNeeds a probe or saved mesh
G90 / G91Absolute / relative positioningWrong mode causes crashes
M104 / M109Set hotend temperature / set and waitM109 blocks until reached
M140 / M190Set bed temperature / set and waitOrder affects warm-up time
M106 / M107Part cooling fan on / offS value sets fan speed
M82 / M83Absolute / relative extrusionMust match slicer setting
Frequently used commands in 3D printing

The distinction between absolute and relative extrusion deserves particular care. If the slicer writes relative E values while the firmware expects absolute ones, the extruder can push far too much or too little filament, producing a badly over-extruded or empty print within seconds.

Structure of a sliced G-code file

A sliced file follows a predictable structure of header, start routine, layer body and end routine. Slicers such as PrusaSlicer, UltiMaker Cura, Bambu Studio and OrcaSlicer also embed comments, beginning with a semicolon, that record settings, layer changes and estimated print time. Those comments are ignored by the firmware but are read by previews and printer interfaces.

The table summarizes what each part contains and why it matters.

PartTypical contentWhy it matters
Header commentsSlicer version, filament use, print timeUsed by previews and print hosts
Start routineHeating, homing, leveling, purge lineSets reliable first-layer conditions
Layer bodyMoves, extrusion, fan and retractionContains the actual part geometry
Layer change markersZ move plus comment such as ;LAYER:12Enables pause, color change and scripts
End routineHeaters off, part presented, motors offProtects the machine and the print
Main parts of a sliced printing file

Users customize the start and end G-code inside the slicer profile. A well-written start routine waits for the bed to stabilize, homes the axes, loads a bed mesh, and draws a purge line so the nozzle is primed before the first layer begins.

How slicer settings translate into G-code

Every slicer parameter is ultimately expressed as coordinates, feed rates and extrusion amounts. The slicer computes each E value from the extrusion width, layer height, path length and filament diameter, which is why an incorrect filament diameter setting silently distorts every line in the file.

  • Layer height appears as the step between successive Z values.
  • Retraction appears as a negative E move, often with a short Z hop, before travel.
  • Speed settings become F values, limited later by firmware acceleration and jerk.
  • Infill pattern and density determine the shape and spacing of interior G1 paths.
  • Pressure advance may be written as M900 in Marlin or SET_PRESSURE_ADVANCE in Klipper.

Some firmware features are implemented as macros or extended commands rather than standard codes. Klipper, for example, accepts named commands such as PRINT_START, which the slicer calls with temperatures as parameters, moving complex logic out of the file and into the printer configuration.

Limitations, risks and common mistakes

G-code is machine-specific and has no geometric meaning beyond the coordinates it lists. A file sliced for one printer assumes that machine’s build volume, nozzle, firmware dialect and kinematics; running it on another printer can cause collisions, overheating or failed adhesion. The format also stores no model, so editing the part requires reslicing the original mesh or CAD file.

The table connects typical symptoms with causes inside the file.

SymptomLikely cause in the fileFix
Nozzle digs into the bedWrong Z offset or missing leveling callAdd G29 or load saved mesh
Heavy over-extrusionExtrusion mode mismatch M82 or M83Align slicer and firmware modes
Print starts coldM104 used instead of M109Use wait commands in start routine
Head crashes into frameFile sliced for another machineReslice with the correct profile
Unknown command warningDialect not supported by firmwareSelect matching G-code flavor
Symptoms traced to G-code problems

Another risk is editing large files by hand without understanding modal state. Deleting a line that sets G91 or changes the feed rate alters every following move. For safe adjustments, practitioners prefer slicer post-processing scripts or firmware macros over manual text edits.

In simple terms

G-code is the set of step-by-step directions a 3D printer follows. Imagine giving someone a very long list of instructions such as move here, go this fast, squeeze out this much plastic, then move there. The printer does nothing on its own; it simply follows that list, line after line, until the object is finished.

The slicer writes this list for you from a 3D model. That is why every setting you choose in the slicer ends up hidden inside the G-code, and why a file made for one printer should not be sent to a different one.

A practical example

A small engineering workshop runs four identical printers with Klipper firmware and noticed that roughly one first layer in ten failed on PETG. Inspecting the G-code showed that the start routine used M140 and M104 without waiting, so printing began while the bed was still about 10 degrees Celsius below the 80 degree target.

The technician replaced those lines with a single PRINT_START macro call that heats the bed with M190, soaks it for five minutes, runs a quick probe, and only then heats the nozzle with M109 before drawing a purge line. The slicer profile was updated so every new file called the macro with the correct temperatures.

Over the following month, first-layer failures on PETG dropped to near zero, and the team gained a single place to adjust start behavior for all four machines without reslicing old jobs.