If you've ever looked at a file on your 3D printer's SD card and seen lines of text starting with G and M, you've already seen G-code. These short commands are the instructions your 3D printer reads to move its motors, heat the nozzle, and lay down filament layer by layer. Understanding what G-code commands are and how they work gives you more control over your prints, helps you troubleshoot problems, and lets you fine-tune results that slicing software alone might not get right.

What Exactly Are G-Code Commands?

G-code is a plain-text programming language used by most CNC machines, including 3D printers. Each line tells the printer to do one specific thing: move to a coordinate, set a temperature, push filament through the extruder, or wait for a signal. Think of it like a recipe written in shorthand every command is one step, and the printer follows them in order from top to bottom.

The "G" in G-code stands for "geometry," and these commands control movement. "M" commands handle machine functions like fan speed, temperature, and motor control. Together, they form the complete set of instructions needed to produce a physical object from a digital model.

Your slicer translates your 3D model into G-code automatically, but the raw commands are just plain text files with the .gcode extension. You can open them in any text editor to read or modify them.

How Does G-Code Actually Control a 3D Printer?

When you send a print job to your printer, its firmware (usually Marlin, Klipper, or Repetier) reads the G-code file line by line. Each command is parsed and converted into electrical signals that drive stepper motors, heating elements, and fans.

A typical line of G-code looks like this:

G1 X50 Y30 Z0.3 F1500 E0.5

That single line tells the printer to move in a straight line to X position 50, Y position 30, at a height of 0.3mm, at a feed rate of 1500mm/min, while extruding 0.5mm of filament. Multiply that by thousands of lines, and you have a complete 3D print.

What Are the Most Common G-Code Commands You'll See?

Here are the commands that show up in almost every 3D print file. Knowing what they do helps you understand what your printer is doing at any point in a print.

Movement Commands

  • G0 Rapid move. The printer moves as fast as possible to a position without extruding. Used for travel moves between printed sections.
  • G1 Linear move. The printer moves to a position while extruding filament. This is the workhorse command for most of your print.
  • G28 Home the printer. Moves all axes to their endstop positions (usually 0,0,0) to establish a known starting point.
  • G29 Auto bed leveling. Runs the leveling probe sequence if your printer supports it.

Temperature and Heating Commands

  • M104 Set hotend temperature (non-blocking). The printer starts heating and immediately moves to the next command.
  • M109 Set hotend temperature and wait. The printer won't do anything else until the target temperature is reached.
  • M140 Set bed temperature (non-blocking).
  • M190 Set bed temperature and wait.

Extruder and Filament Commands

  • M82 Set extruder to absolute mode. Filament position is measured from zero.
  • M83 Set extruder to relative mode. Each extrusion value is added to the current position.
  • G92 Set position. Often used to reset the extruder counter to zero at the start of a print.

Fan and Machine Control

  • M106 Turn part cooling fan on. You can set the speed from 0 (off) to 255 (full speed).
  • M107 Turn part cooling fan off.
  • M84 Disable stepper motors. Used at the end of a print so you can move the axes freely by hand.

Where Does G-Code Come From Do You Write It Yourself?

Almost nobody writes G-code by hand for full prints. Slicing software like Cura, PrusaSlicer, or OrcaSlicer does this automatically. You import your STL or 3MF file, adjust print settings like layer height, infill, and temperature, and the slicer generates a complete G-code file tailored to your printer profile.

That said, many experienced users edit G-code snippets especially start G-code and end G-code. These are short blocks of commands that run before and after every print. A typical start G-code block heats the bed and nozzle, homes the printer, and does a purge line. End G-code retracts filament, turns off heaters, and homes the X and Y axes.

Customizing these blocks is one of the easiest ways to improve print consistency. For example, adding a nozzle wipe or adjusting the purge line position can reduce first-layer issues.

Why Would You Want to Edit G-Code Directly?

There are several practical reasons to look at and modify G-code files:

  • Fixing a failed print mid-file. If a print failed at layer 200, you can sometimes edit the file to restart from that point instead of reprinting from scratch.
  • Adding pauses for inserts. The M600 command (on supported firmware) pauses the print so you can drop in magnets, nuts, or other hardware.
  • Changing temperatures for specific layers. Some filaments benefit from a higher temperature for the first layer and lower for the rest.
  • Troubleshooting. If your printer behaves oddly, reading the G-code can reveal whether the problem is in the file or the hardware. Understanding common G-code syntax errors can save you hours of debugging.

What Mistakes Do People Make with G-Code?

A few issues come up regularly, especially when users start customizing their G-code:

  • Forgetting G28 or G92 at the start. Without homing or resetting coordinates, the printer doesn't know where the nozzle is, which can lead to crashes or prints starting in the wrong position.
  • Mixing absolute and relative extrusion. If your slicer outputs absolute E values but you switch to relative mode (or vice versa), you'll get massive over-extrusion or no extrusion at all.
  • Wrong temperature commands. Using M104 instead of M109 before a print starts means the printer may begin printing before the hotend is fully heated.
  • Missing the filament retract at end. Without a retract command at the end of the print, filament can ooze and create a blob on your finished part.
  • Using firmware-specific commands without checking compatibility. Not all printers support the same M-codes. M600 for filament change, for instance, needs to be enabled in Marlin configuration.

How Can You Read and Understand a G-Code File?

Opening a .gcode file in a text editor shows you every command the printer will execute. Lines starting with ; are comments usually added by the slicer to label sections like layer changes, temperature settings, and print time estimates.

A few things to look for when reading through a file:

  • The start G-code block (usually the first 10–30 lines) sets up the print environment.
  • Layer change comments (like ;LAYER:5) mark where each new layer begins.
  • Temperature commands near the top tell you what the slicer set for hotend and bed.
  • The end G-code block (last few lines) shuts everything down.

For a deeper walkthrough, check out our guide on how to read G-code on a 3D printer.

Do Different Printers Use Different G-Code?

The core G and M commands are fairly standardized, but there are differences between firmware. Marlin is the most widely used firmware and supports the broadest set of commands. Klipper uses a slightly different approach it runs the heavy computation on a separate computer (like a Raspberry Pi) and sends simplified commands to the printer board. Some commands are Klipper-specific and won't work on Marlin, and vice versa.

Prusa printers use PrusaSlicer, which generates G-code optimized for their firmware and hardware. Bambu Lab printers use their own flavor of G-code with proprietary commands for features like the AMS (Automatic Material System). Always make sure your G-code matches your printer's firmware.

Tips for Working with G-Code on Your 3D Printer

  • Always back up your slicer's default start and end G-code before making changes. One wrong command can crash your nozzle into the bed.
  • Use a G-code viewer (like the one built into Cura or online tools like gcode.ws) to visualize toolpaths before printing.
  • Test small edits on cheap filament first. A temperature or speed change in G-code can have unexpected results.
  • Keep notes on what custom commands work for your setup. A simple text file with your favorite start/end G-code snippets saves time when switching slicers or profiles.
  • Learn your firmware's documentation. Marlin's G-code reference on their official site is thorough and specific: Marlin G-code documentation.

When you're designing parts that include engraved text or embossed labels, the font you choose matters for both aesthetics and printability. Clean, bold typefaces like Bebas Neue or Montserrat tend to slice well because their letterforms have consistent thickness, which translates into cleaner G-code toolpaths.

Quick Checklist Before You Edit G-Code

  1. Know which firmware your printer runs (Marlin, Klipper, Repetier, etc.).
  2. Back up your current slicer profile, including start and end G-code.
  3. Open the file in a plain text editor (Notepad++, VS Code, or Sublime Text not Word).
  4. Search for the specific layer or command you want to change using the line number or comment markers.
  5. Make one change at a time and test with a small print before committing to a long job.
  6. Use a G-code viewer to verify your edits look correct visually.
  7. Save with the .gcode extension and double-check that your printer can read the file format (some printers need .gco or .g).