G-code explained: reading and editing printer commands
A G-code file is a plain-text list of commands, one per line, that the printer's firmware executes in order: move here, push this much filament, heat to this temperature, turn the fan on. The slicer writes almost all of it, but the start and end sections are yours, and knowing a dozen commands is enough to read any file and change it safely.
Anatomy of a line
Each line is a command letter and number followed by parameters, with an optional comment after a semicolon:
G1 X85.2 Y40.6 E0.0371 F4800 ; outer wall
- G commands are motion and geometry: moves, homing, units, positioning mode.
- M commands are machine functions: heaters, fans, motors, messages.
- Parameters are a letter and a value: X, Y and Z are positions in millimetres, E is filament, F is feed rate, S is a value such as a temperature.
- Comments after
;are ignored by the printer. Slicers write useful ones: layer markers (;LAYER:12in Cura,;LAYER_CHANGEin PrusaSlicer and OrcaSlicer), feature types and the settings used, usually at the end of the file.
Turning a model into this list is the slicer's job, covered in STL to G-code. This page is about reading and changing the result.
Moves, extrusion and feed rate
G1 is a straight move to the given position. G0 is a rapid move; on 3D printer firmware it is normally treated exactly like G1, and slicers use it for travel to make files easier to read. Any axis left out stays where it is, so G1 Z0.4 only moves Z.
E is how much filament the extruder pushes, in millimetres of filament, not of printed line. The slicer computes it from the volume of the line: E = line length × line width × layer height ÷ filament cross-section. 1.75 mm filament has a cross-section of π × 0.875² ≈ 2.405 mm². A 10 mm line at 0.45 mm wide and 0.2 mm high is 0.9 mm³, so E ≈ 0.374 mm. Negative E is a retraction.
F is speed in millimetres per minute and stays in force until changed: F3000 is 50 mm/s, F6000 is 100 mm/s, F12000 is 200 mm/s. Firmware acceleration limits mean the requested speed is a maximum that short moves may never reach.
The commands you will meet most
| Command | What it does | Notes |
|---|---|---|
| G28 | Home all axes (or only those named) | Needed after power-on before any move |
| G29 | Probe the bed (Marlin) | Klipper uses a mesh macro instead |
| G90 / G91 | Absolute / relative positioning | Slicers use G90 for X, Y and Z |
| M82 / M83 | Absolute / relative extrusion | See the next section |
| G92 E0 | Set the current extruder position to 0 | Used before purge lines and after resets |
| M104 S210 | Set hotend to 210 °C, continue at once | M104 S0 turns it off |
| M109 S210 | Set hotend and wait until reached | Use before the first extrusion |
| M140 S60 / M190 S60 | Set bed / set bed and wait | Same pattern as M104 / M109 |
| M106 S255 | Part-cooling fan on, S0–255 | S128 ≈ 50 %; M107 turns it off |
| G4 P500 | Pause for 500 ms | G4 S2 waits 2 seconds |
| M84 | Disable stepper motors | End of print; axes can then move freely |
| M600 | Filament change (Marlin, if enabled) | Klipper usually uses a PAUSE macro |
| M117 | Show a message on the display | Handy for marking sections |
| M220 / M221 | Speed factor / flow factor in % | What the "speed" and "flow" knobs on the printer change |
Klipper accepts these standard commands and adds its own named commands and macros, such as SET_PRESSURE_ADVANCE. Marlin sets pressure advance with M900 K. Calibration of that value is covered in the pressure advance guide.
Absolute and relative extrusion
With absolute extrusion (M82), each E value is the total filament position since the last reset: E102.5 then E102.9 means push 0.4 mm. With relative extrusion (M83), each E value is the amount for that move alone: E0.4.
The difference matters as soon as you edit a file. In absolute mode, deleting a block of moves makes the next move extrude the whole missing amount at once — a large blob or a grinding extruder. In relative mode, deleted moves simply do not extrude. Many current printer profiles use relative mode for that reason. Whichever mode a file uses, the slicer's setting and the start G-code must agree, or the first line can extrude nothing or far too much.
Start and end G-code
The printer profile holds a block that runs before the first layer and one that runs after the last. A typical start for a Marlin printer, with placeholders the slicer fills in:
| Line | Purpose |
|---|---|
M140 S[first_layer_bed_temperature] | Start heating the bed |
M104 S150 | Warm the nozzle without oozing |
G28 | Home all axes |
M190 S[first_layer_bed_temperature] | Wait for the bed |
M109 S[first_layer_temperature] | Wait for the nozzle |
G92 E0 | Reset the extruder position |
G1 Z0.3 F3000 | Lower the nozzle to purge height |
G1 X5 Y20 F6000 | Travel to the start of the purge line |
G1 Y120 E15 F1500 | Purge line: 100 mm with 15 mm of filament |
G92 E0 | Reset again so the first layer starts from 0 |
The square-bracket names are PrusaSlicer placeholders; Cura uses names such as {material_print_temperature_layer_0} and {material_bed_temperature_layer_0}, and OrcaSlicer and Bambu Studio list theirs in the G-code editor. Klipper printers often reduce the whole block to a single macro call, commonly named PRINT_START or START_PRINT, with the temperatures passed as parameters.
Check the purge with the same E formula: 15 mm of filament is 15 × 2.405 ≈ 36 mm³ spread over a 100 mm line, 0.36 mm² of cross-section. At 0.3 mm height that is a line about 1.2 mm wide: deliberately fat, so the nozzle is fully primed. Heating the bed and nozzle in parallel, or warming the nozzle only partly during homing and probing, saves time and reduces ooze on the bed.
If the start block contains no temperature commands, slicers generally insert their own before it, which can heat things twice or in the wrong order. Writing them yourself with placeholders gives you control. A typical end block retracts a few millimetres, lifts Z, moves the bed forward, turns off heaters (M104 S0, M140 S0) and the fan (M107) and disables motors (M84). Keep the Z lift within the printer's height, and do not remove the heater-off lines.
Arc moves: G2 and G3
G2 (clockwise) and G3 (counter-clockwise) draw an arc to the end point, with the centre given by I and J, its X and Y offset from the start point, or by a radius R. One arc command can replace dozens of short G1 segments on a curved wall, which makes files smaller and helps printers whose controller struggles to process many tiny moves quickly, especially over a serial connection.
Slicers call the feature arc fitting (PrusaSlicer, OrcaSlicer, Bambu Studio); for Cura a post-processing plugin does the same. The firmware must support arcs: Marlin needs arc support compiled in, and Klipper needs a [gcode_arcs] section in its configuration. If it does not, arcs are skipped or rejected and the print comes out with missing curves. Both Marlin and Klipper split arcs back into short segments internally, so the gain is in file size and data transfer, not in the shape itself; on printers that already print curves smoothly, arc fitting is optional.
Editing G-code safely
Most edits are better done in the slicer: custom G-code at a layer from the layer slider, colour changes and pauses, or a post-processing script. When you must edit the text:
- Find the place by comment. Search for the layer marker and insert your command right after it, not in the middle of a wall.
- Prefer non-waiting commands mid-print.
M104to change temperature andM106to change the fan continue printing;M109stops on the part and leaves a blob. - Never edit E or Z values by hand in a block of moves. In absolute extrusion, a wrong E sends a burst of filament; a wrong Z can drive the nozzle into the print.
- Insert pauses with the slicer's own command for your firmware (M600 or a PAUSE macro), so the printer parks, lets you work and returns to the right height — the method used for colour swaps in multicolour without a multi-material unit.
- Save under a new name and preview the edited file in the G-code viewer or the slicer's own viewer, then watch the first minutes of the print with a hand near the power switch.
Firmware refuses to extrude below a minimum temperature and watches the heaters for thermal runaway. Edits that set a temperature to 0 before an extrusion move will stop the print; that is the protection working. Never work around it by changing firmware settings to make an edited file run.
To get a printable shape back from a G-code file whose model is lost, G-code to STL rebuilds the outer walls of each layer into a solid. The slicer preview guide shows what to check in the toolpaths themselves.
Frequently asked questions
Can I convert G-code back to STL?
Partly. A G-code file contains toolpaths, not a model, but the outer walls of each layer can be stacked back into a solid shape. Fine details, internal structure and anything that was dropped while slicing are lost, so the result is good for reference and simple reprints rather than editing.
What does F mean in G-code?
F is the feed rate, the speed of the move in millimetres per minute. F3000 is 50 mm/s. It stays in effect for following moves until another F value appears, so many lines have none.
Is G-code the same for every printer?
The basic commands are shared, but firmware differs in details and extras. Marlin, Klipper and RepRapFirmware all understand G1, G28, M104 and M106; bed levelling, filament change and pressure advance commands differ. That is why the slicer has a G-code flavour setting and why a file sliced for one printer should not be sent to another.
Why does my print start with a line at the edge of the bed?
That is the purge or prime line from the start G-code. It pushes plastic through the nozzle until the flow is steady and wipes off any ooze, so the first real line starts cleanly. You can shorten it, move it or replace it with a skirt, but some form of priming is worth keeping.
Can I change the temperature in the middle of a G-code file?
Yes. Insert an M104 S-value at the start of the layer where it should change; the hotend adjusts while the print continues. M109 would stop and wait, leaving a blob. Most slicers can also add custom G-code at a layer from the layer slider, which avoids editing the file by hand.