G-code is the language your 3D printer understands: a list of text instructions that tell it where to move, how much to extrude and what temperature to heat to. In this guide you’ll see how a file is organized, what the most common commands do and how to read your slicer’s start and end G-code.
The examples follow the syntax of Marlin, the most widely used firmware. Klipper and RepRapFirmware accept most of these commands.
In short
- G commands are movements; M commands are machine functions (temperature, fan, motors).
- The letters that go with a command are parameters:
X,Y,Z,E(extruder),F(speed) andS(value).- The start G-code heats up, homes and purges; the end G-code turns off the heaters, moves the print head out of the way and disables the motors.
What G-code is
The slicer (Cura, PrusaSlicer, OrcaSlicer…) turns a 3D model into thousands of lines of G-code. The printer runs them from top to bottom, one by one.
Each line looks like this:
G1 X50 Y25.3 E0.42 F1500 ; optional comment
G1: the command (linear move).X50 Y25.3: destination in millimeters.E0.42: filament to extrude during the move.F1500: speed in mm/min (1,500 mm/min is 25 mm/s).;: everything after it is a comment and the printer ignores it.
Structure of a G-code file
A typical file has four blocks:
- Header: slicer comments with the profile, estimated time and filament.
- Start G-code: heat up, home, level and purge the nozzle.
- Body: layer by layer, with comments like
;LAYER:0or;TYPE:WALL-OUTER. - End G-code: turn everything off and leave the part accessible.

You define the start and end G-code yourself in the slicer’s printer settings. The body is generated by the slicer.
Movement commands
| Command | What it does | Example |
|---|---|---|
G0 | Rapid move (no extrusion) | G0 X10 Y10 F6000 |
G1 | Linear move, with or without extrusion | G1 X60 E2.5 F1200 |
G28 | Home: sends the axes to their endstops | G28 or G28 Z |
G29 | Automatic bed leveling (mesh probing) | G29 |
G90 | Absolute coordinates | G90 |
G91 | Relative coordinates | G91 |
G92 | Sets the current position without moving | G92 E0 |
G0 and G1
In Marlin, G0 and G1 behave practically the same. Slicers usually use G0 for travel moves and G1 for extruding lines, which makes the file easier to read.
G90 and G91: absolute vs. relative
With G90, G1 Z10 means “go to Z = 10 mm”. With G91, the same command means “move up 10 mm from wherever you are”.

It’s very handy in the end G-code to lift the nozzle without knowing at what height the part finished:
G91 ; relative mode
G1 Z10 F600 ; move up 10 mm from the current position
G90 ; back to absolute mode
The extruder has its own mode: M82 (absolute) and M83 (relative). Your slicer must use the same one that the start G-code declares.
G92 E0
Resets the extruder counter to zero. You’ll see it before every purge and, in many slicers, at the start of every layer.
Temperature commands
| Command | What it does | Waits? |
|---|---|---|
M104 S200 | Sets the hotend to 200 °C | No |
M109 S200 | Sets the hotend to 200 °C | Yes, until it gets there |
M140 S60 | Sets the bed to 60 °C | No |
M190 S60 | Sets the bed to 60 °C | Yes, until it gets there |
According to the Marlin documentation, M109 with S only waits while heating. With R it also waits if the temperature has to drop, which is useful when switching materials.
A common trick is to heat both at once and wait afterward:
M140 S60 ; bed to 60 without waiting
M104 S200 ; hotend to 200 without waiting
M190 S60 ; now wait for the bed
M109 S200 ; and then for the hotend
Fan, motors and memory
| Command | What it does |
|---|---|
M106 S255 | Part cooling fan at 100% (the value ranges from 0 to 255) |
M106 S128 | Fan at roughly 50% |
M107 | Turns off the part cooling fan |
M84 | Disables the stepper motors |
M500 | Saves the current settings to EEPROM |
M501 | Loads the saved settings |
M503 | Shows the current settings |
M420 S1 | Enables the saved leveling mesh |
M500 matters: changes such as steps per millimeter (M92) or the Z offset (M851) are lost when you power off if you don’t save them. In Klipper, the equivalent is SAVE_CONFIG.
Sample start G-code
A typical start sequence for an Ender 3-style printer with a leveling sensor:
G90 ; absolute coordinates
M82 ; extruder in absolute mode
M140 S60 ; bed to 60 °C
M104 S150 ; hotend to 150 °C so it doesn't ooze while probing
M190 S60 ; wait for the bed
G28 ; home all axes
G29 ; probe the mesh (or M420 S1 if it's already saved)
M109 S210 ; hotend to printing temperature
G92 E0 ; reset extruder counter
G1 Z2.0 F3000 ; lift the nozzle a little
G1 X0.1 Y20 Z0.3 F5000 ; go to the start of the purge line
G1 X0.1 Y200 Z0.3 F1500 E15 ; first purge line
G1 X0.4 Y200 Z0.3 F5000 ; move to the side
G1 X0.4 Y20 Z0.3 F1500 E30 ; second purge line
G92 E0 ; reset counter again
G1 Z2.0 F3000 ; lift before starting the part

Why each step:
- Hotend at 150 °C during probing: that way it doesn’t ooze plastic onto the bed or the sensor.
- G28 before G29: probing needs to know where the origin is.
- Purge line: fills the nozzle with fresh plastic before the first layer.
Keep in mind that G28 disables leveling in Marlin by default. If you don’t run G29 on every print, you need M420 S1 after homing.
Sample end G-code
G91 ; relative mode
G1 E-2 F2700 ; retract 2 mm of filament
G1 Z10 F600 ; move up 10 mm
G90 ; absolute mode
G1 X0 Y220 F3000 ; bring the bed forward to remove the part
M106 S0 ; turn off the part cooling fan
M104 S0 ; turn off the hotend
M140 S0 ; turn off the bed
M84 X Y E ; disable motors (keep Z locked)
Keeping the Z motor enabled prevents the axis from dropping under its own weight on some printers.
How to send G-code to the printer
- Printer screen: many let you run single commands from a menu.
- Pronterface or the OctoPrint terminal: connect over USB and type commands one at a time. See how to control the Ender 3 with OctoPrint.
- Mainsail or Fluidd on printers running Klipper.
When you send M503, the printer replies with its settings, which is handy for knowing what values you have saved.
Common mistakes
- Forgetting that F is in mm/min.
F50moves the print head at less than 1 mm/s. - Mixing absolute and relative extruder modes. The printer extrudes absurd amounts on the first layer.
- Not saving with M500. Your settings disappear when you power off.
- Moving Z with G91 and not going back to G90. The rest of the print is interpreted incorrectly.
- Waiting for the hotend before leveling. It oozes onto the bed during probing.



