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.
| Letter | Meaning | Typical unit |
|---|---|---|
| X, Y, Z | Target position on each axis | Millimeters |
| E | Extruder position or filament length | Millimeters of filament |
| F | Feed rate for the move | Millimeters per minute |
| S | Value such as temperature or fan duty | Degrees Celsius or 0-255 |
| P | Duration or tool index | Milliseconds or index |
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.
| Command | Function | Practical note |
|---|---|---|
| G0 / G1 | Travel move / linear extruding move | Most lines in any file |
| G28 | Home the axes | Required before printing |
| G29 | Run automatic bed leveling | Needs a probe or saved mesh |
| G90 / G91 | Absolute / relative positioning | Wrong mode causes crashes |
| M104 / M109 | Set hotend temperature / set and wait | M109 blocks until reached |
| M140 / M190 | Set bed temperature / set and wait | Order affects warm-up time |
| M106 / M107 | Part cooling fan on / off | S value sets fan speed |
| M82 / M83 | Absolute / relative extrusion | Must match slicer setting |
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.
| Part | Typical content | Why it matters |
|---|---|---|
| Header comments | Slicer version, filament use, print time | Used by previews and print hosts |
| Start routine | Heating, homing, leveling, purge line | Sets reliable first-layer conditions |
| Layer body | Moves, extrusion, fan and retraction | Contains the actual part geometry |
| Layer change markers | Z move plus comment such as ;LAYER:12 | Enables pause, color change and scripts |
| End routine | Heaters off, part presented, motors off | Protects the machine and the print |
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.
| Symptom | Likely cause in the file | Fix |
|---|---|---|
| Nozzle digs into the bed | Wrong Z offset or missing leveling call | Add G29 or load saved mesh |
| Heavy over-extrusion | Extrusion mode mismatch M82 or M83 | Align slicer and firmware modes |
| Print starts cold | M104 used instead of M109 | Use wait commands in start routine |
| Head crashes into frame | File sliced for another machine | Reslice with the correct profile |
| Unknown command warning | Dialect not supported by firmware | Select matching G-code flavor |
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.