Understanding G-Code: The Commands Behind Every Print
This article may contain affiliate links. If you make a purchase through these links, we may earn a small commission at no extra cost to you. This helps us keep creating free content.
When you hit "print" in your slicer, the software converts your 3D model into a text file containing thousands of simple instructions. These instructions, G-code, tell your printer exactly where to move the nozzle, how fast to travel, what temperature to set, and how much filament to extrude. Every 3D print, whether it's a simple calibration cube or a complex cosplay helmet, is nothing more than a long list of these commands executed sequentially.
You don't need to write G-code by hand (your slicer does that), but understanding what the commands mean gives you genuine power. You can troubleshoot failed prints, add custom start/end sequences, manually adjust settings mid-print, and understand why your slicer makes the choices it does.
What Is G-Code?
G-code (short for "Geometric Code") originated in CNC machining in the 1950s and was adopted by 3D printers as the standard instruction format. A G-code file is a plain text file (typically .gcode extension) where each line contains one command. The commands fall into two categories: G commands (movement and geometry) and M commands (machine functions like temperature, fans, and motors).
Open any .gcode file in a text editor and you'll see thousands of lines that look like this:
G28 ; Home all axes M104 S200 ; Set hotend to 200°C M140 S60 ; Set bed to 60°C M109 S200 ; Wait for hotend to reach 200°C M190 S60 ; Wait for bed to reach 60°C G1 X50 Y50 Z0.2 F3000 ; Move to start position G1 X100 E10 F1500 ; Extrude while moving to X100
Everything after a semicolon is a comment, the printer ignores it. The actual commands are short and structured: a letter-number code followed by parameters.
Essential G-Code Commands
You don't need to memorize hundreds of commands. These 10 cover 95% of what your printer executes:
Movement Commands
G0, Rapid move (travel without extruding). Moves the nozzle to coordinates as fast as possible. Used for non-printing travel moves between features.
G1, Linear move (the workhorse). Moves the nozzle in a straight line at a specified feed rate (speed) while optionally extruding filament. Parameters: X, Y, Z (position), E (extrusion amount), F (feed rate in mm/min). Almost every printing move is a G1 command.
G28, Home axes. Moves the printhead to the endstops to establish the zero position. Usually runs at the start of every print.
G29, Bed leveling probe. Runs the automatic bed leveling sequence (if your printer has a probe like BLTouch or inductive sensor). Creates a mesh map of the bed surface to compensate for warping.
Temperature Commands
M104 Sxxx, Set hotend temperature without waiting. The printer continues executing commands while heating. M104 S200 sets the target to 200°C and immediately moves on.
M109 Sxxx, Set hotend temperature AND wait. The printer pauses all commands until the hotend reaches the target temperature. This is what your start G-code uses to ensure the nozzle is ready before extruding.
M140 Sxxx, Set bed temperature without waiting (same concept as M104).
M190 Sxxx, Set bed temperature and wait (same concept as M109).
Other Important Commands
M106 Sxxx, Set part cooling fan speed. S0 = off, S255 = full speed. S128 = roughly 50%. Your slicer varies this by layer, typically full speed after the first few layers for PLA.
G92 E0, Reset the extruder position to zero. Used to prevent the E value from becoming astronomically large during long prints. Your slicer inserts this periodically.
Practical Uses: Editing G-Code
Understanding G-code lets you do things your slicer's UI might not offer:
Custom start G-code: Most slicers let you edit the start sequence. You can add a nozzle purge line, a bed-leveling probe (G29), or a custom homing sequence. For example, adding a purge line before every print prevents the first layer from starting with under-extrusion.
Mid-print modifications: Need to change temperature at a specific layer? Search the G-code file for the layer number (slicers add comments like ;LAYER:50) and insert an M104 command. This is how you do manual filament color changes, insert an M600 (filament change) command at the desired layer.
Reading G-Code for Troubleshooting
When a print fails, the G-code file contains clues. Some examples:
- First layer too fast? Search for the first G1 commands after the start sequence and check the F value. If it's above F2400 (40 mm/s), your first layer might be too fast for adhesion
- Temperature wrong? Search for M104/M109 commands. Sometimes slicer profiles override your manual temperature settings via the start G-code
- Retraction issues? Search for G1 commands with negative E values. The magnitude tells you your retraction distance
- Print time way off? Check our slicer print time guide, the G-code's F values directly determine actual print speed
G-Code Viewers
Reading raw G-code text is tedious. G-code viewers visualize the toolpath so you can see exactly what the printer will do. Most slicers have built-in preview (Cura's layer view is excellent), but dedicated tools like gCodeViewer (web-based, free) and the G-code preview in OctoPrint let you inspect files from any slicer.
G-code is the language your printer actually speaks. Your slicer is just a translator between your 3D model and these simple movement commands. Understanding even the basics, G1 for moves, M104/M109 for temperatures, G28 for homing, makes you a more effective 3D printer operator. When something goes wrong, the G-code file is your first diagnostic tool.
Published by the 3D Printer Stuff editorial team. Published August 2, 2026.
Editorial responsibility: see Imprint.
Spotted an error or have something to add? corrections@3dprinterstuff.com
Explore more
All articles on 3D Printer Stuff →
Maker Tips, Delivered
New guides, filament tests, and project ideas — every week in your inbox.
🎁 Free bonus: 3D Printing Starter Checklist (PDF)
You might also like
Cura vs PrusaSlicer vs Bambu Studio vs OrcaSlicer: 2026 Slicer Showdown
A head-to-head comparison of the four major FDM slicers with benchmark prints, feature analysis, and recommendations based on your printer and workflow.
Klipper Firmware Setup: The Complete Installation and Configuration Guide
Step-by-step guide to installing Klipper firmware on your 3D printer with a Raspberry Pi, including configuration, calibration, and the performance gains you can expect.