What Is G-code?
G-code (also called RS-274 or CNC code) is the programming language used to control CNC (Computer Numerical Control) machines — including 3D printers, CNC milling machines, lathes, plasma cutters, laser engravers, and pick-and-place machines. It originated at MIT in the 1950s and was standardized as RS-274 by the Electronic Industries Alliance (EIA). The ISO version is ISO 6983.
For 3D printing specifically, G-code is the output of slicers (software that converts 3D models to print instructions): Cura, PrusaSlicer, Bambu Studio, Simplify3D, SuperSlicer. Every FDM (Fused Deposition Modeling) 3D printer reads G-code; laser cutters and CNC routers use dialect variants.
A G-code file is plain text — every line is either a command, a comment (enclosed in ;), or blank. Commands consist of a letter code followed by a number, with optional parameters.
G-code File Structure
; Generated by Cura 5.6.0
; Layer count: 142
; Filament used: 4.82m
; Print time: 2h 15min
M104 S215 ; Set extruder temperature to 215°C (no wait)
M109 S215 ; Set extruder temp and wait for it to reach target
M140 S60 ; Set bed temperature to 60°C (no wait)
M190 S60 ; Set bed temp and wait
G28 ; Home all axes (X, Y, Z to endstops)
G29 ; Auto bed leveling (if supported)
G1 Z5 F5000 ; Move Z to 5mm
G21 ; Set units to millimeters
G90 ; Absolute positioning mode
M82 ; Extruder absolute mode
G92 E0 ; Reset extruder position to 0
; === Start of actual print ===
G0 F9000 X50 Y50 Z0.2 ; Rapid move to start position, Z=0.2mm (first layer height)
G1 F1200 X100 Y50 E5.0 ; Feed move: print line to X100, extruding 5mm of filament
G1 F1200 X100 Y100 E10.0 ; Continue printing
; ... thousands more lines ...
M104 S0 ; Extruder off
M140 S0 ; Bed off
G91 ; Relative positioning
G1 Z10 ; Raise Z by 10mm
G90 ; Back to absolute
G1 X0 Y200 ; Move print head to front
M84 ; Disable motors
G-code Command Types
G Commands (Preparatory Functions)
| Command | Description |
|---|---|
G0 |
Rapid move (no extrusion, maximum speed) |
G1 |
Linear move (coordinated axes + optional extrusion) |
G2 |
Clockwise arc move |
G3 |
Counter-clockwise arc move |
G4 P500 |
Dwell (pause 500 ms) |
G20 |
Units: inches |
G21 |
Units: millimeters |
G28 |
Home axes (to endstops) |
G29 |
Auto bed leveling (mesh probing) |
G90 |
Absolute positioning |
G91 |
Relative positioning |
G92 E0 |
Set current position as origin (reset E) |
M Commands (Miscellaneous)
| Command | Description |
|---|---|
M0 |
Stop program (pause, wait for user) |
M82 |
Extruder: absolute mode |
M83 |
Extruder: relative mode |
M84 |
Disable stepper motors |
M104 S215 |
Set extruder temperature (no wait) |
M109 S215 |
Set extruder temperature and wait |
M140 S60 |
Set heated bed temperature (no wait) |
M190 S60 |
Set heated bed temperature and wait |
M106 S128 |
Set fan speed (0–255, here ~50%) |
M107 |
Fan off |
M117 Hello |
Display message on LCD |
M600 |
Filament change (pause, retract, wait) |
M900 K0.05 |
Linear advance factor |
Parameter Letters
| Letter | Meaning |
|---|---|
X, Y, Z |
Axis coordinates (mm or inches) |
E |
Extruder position (mm of filament to push/retract) |
F |
Feed rate (mm/min) |
S |
Spindle speed / temperature / value |
P |
Time (ms) or parameter |
I, J |
Arc center offsets (for G2/G3) |
R |
Arc radius |
Coordinate Systems
Absolute vs Relative Mode
- Absolute (
G90): All coordinates are from machine home (0,0,0).G1 X50moves to 50mm from origin. - Relative (
G91): All coordinates are relative to current position.G1 X10moves 10mm in X direction from wherever the head is now.
Most slicers use absolute for XYZ and relative for E (extruder), or absolute for everything after G92 E0 (reset).
Coordinate Origin and Build Volume
The origin is typically the front-left corner of the print bed at Z=0 (bed surface). The build volume is defined in firmware: a Bambu Lab X1C has 256×256×256mm, a Creality Ender 3 has 220×220×250mm.
FDM 3D Printing Specific G-code
Extrusion Calculation
The E value controls how much filament is pushed through the extruder. In absolute mode, E accumulates across the print:
G1 X100 Y50 E5.2 F1200 ; Move to (100,50), extrude to E=5.2mm total
G1 X150 Y50 E8.7 F1200 ; Continue to (150,50), extrude to E=8.7mm total (3.5mm more)
Filament volume formula:
Volume (mm³) = π × (filament_diameter/2)² × E_distance
For 1.75mm filament: Volume = π × 0.875² × E ≈ 2.405 × E mm³
Retraction
Retraction pulls filament back before non-print moves to prevent stringing:
G1 E-4.5 F3600 ; Retract 4.5mm at 60mm/s (rapid retract)
G0 X80 Y80 ; Rapid travel move
G1 E0 F3600 ; Prime (push back) 4.5mm before next extrusion
Layer Changes
; Layer 12
;LAYER:12
G0 F9000 X44.5 Y44.5 Z1.4 ; Move to new layer height Z=1.4mm (layer 12 × 0.12mm layer height + first layer)
Slicers and G-code Generation
Slicers convert STL/3MF/OBJ files to G-code:
| Slicer | Supports | Notable Features |
|---|---|---|
| Ultimaker Cura | FDM (most brands) | Plugin ecosystem, profile library |
| PrusaSlicer | FDM, SLA (Prusa, generic) | Multi-material, paint-on supports |
| Bambu Studio | Bambu Lab printers | AMS multi-color, AI supports |
| Simplify3D | FDM (commercial) | Fine control, multi-process |
| OrcaSlicer | FDM (generic) | Calibration tools, advanced settings |
| LightBurn | Laser cutters | Vector/raster laser paths |
| Fusion 360 CAM | CNC machining | Professional toolpath generation |
STL to G-code workflow
STL/3MF file → Slicer → G-code → USB/SD card/WiFi → Printer → Physical part
Key slicer settings that affect G-code:
- Layer height: Resolution (0.1–0.4mm typical)
- Print speed: Line/travel speed (30–300 mm/s)
- Infill: Internal structure density and pattern
- Support structures: For overhangs beyond 45°
- Temperature profile: Per-material extruder and bed temps
- Retraction: Distance and speed to prevent stringing
Inspecting and Editing G-code
Text editors
G-code is plain text — any text editor works. VSCode has G-code syntax highlighting extensions.
# View first 30 lines of G-code
head -30 print.gcode
# Count layers
grep -c "^;LAYER:" print.gcode
# Extract all temperatures set
grep "^M109\|^M104" print.gcode
# Find layer 50
grep -n "LAYER:50" print.gcode
G-code simulators / visualizers
- Repetier-Host: Desktop G-code host + visualizer
- Pronterface (Printrun): G-code sender with 2D preview
- gCodeViewer.com: Online 3D G-code visualizer
- NCViewer.com: Online CNC G-code viewer
G-code Dialects
Different firmware and machines use different G-code dialects:
| Dialect | Used By |
|---|---|
| Marlin | Most open-source FDM printers (Ender, Prusa original) |
| Klipper | High-speed printers (Voron, Bambu architecture) |
| RepRap G-code | Original RepRap specification |
| Fanuc G-code | Industrial CNC machines |
| Grbl | Hobbyist CNC routers, laser engravers |
| LinuxCNC | Open-source industrial CNC |
| Smoothieware | Smoothieboard controllers |
Klipper uses macros in printer.cfg and passes G-code through Python for advanced motion planning. Marlin handles commands directly in firmware. A G-code file valid for Marlin may need modification for Klipper.
Converting STL to G-code
# CuraEngine command-line (Cura's slicer engine)
CuraEngine slice -j printer_profile.json -l model.stl -o output.gcode
# PrusaSlicer command-line
prusa-slicer --slice --load config.ini model.stl --output output.gcode
# Inspect the result
wc -l output.gcode # count lines (a complex print = millions of lines)
G-code's plain-text format, its 70-year lineage, and its universality across industries from dental implants to automotive prototyping to aerospace tooling — all from the same fundamental command set — makes it one of the most durable technical formats in manufacturing history.
Related conversions
Frequent conversions across the catalogue: