What Is G-Code?

G-code (formally known as RS-274) is the standardized programming language used to control CNC machines. When your CAM software generates a toolpath, it exports that toolpath as a G-code file — a plain text file filled with short, cryptic-looking lines of code. Each line gives the machine a specific instruction: move here, turn on the spindle, change the feed rate, stop.

While most modern CNC work is done with CAM software doing the heavy lifting, understanding G-code is invaluable. It lets you edit programs on the fly, troubleshoot errors, write quick utility programs by hand, and truly understand what your machine is doing.

The Structure of a G-Code Line

Each line in a G-code program is called a block. A typical block looks like this:

N010 G01 X2.500 Y1.250 Z-0.125 F15.0

  • N010 — Line number (optional, used for reference)
  • G01 — A G-code command (in this case, linear interpolation / controlled feed move)
  • X2.500 Y1.250 Z-0.125 — Target coordinates
  • F15.0 — Feed rate (15 inches per minute)

Essential G-Codes

These are the commands you'll encounter in virtually every CNC program:

CodeNameDescription
G00Rapid PositioningMoves the tool at maximum speed. Use for non-cutting moves only.
G01Linear InterpolationStraight-line cutting move at a specified feed rate.
G02Circular Interpolation (CW)Arcing move in the clockwise direction.
G03Circular Interpolation (CCW)Arcing move in the counter-clockwise direction.
G17XY Plane SelectionSets circular interpolation to the XY plane (most common).
G20 / G21Inch / Metric ModeSets units for all coordinate values.
G28Return to Machine HomeSends the machine to its home position.
G40Cancel Cutter CompTurns off tool radius compensation.
G90Absolute PositioningAll moves are relative to the work zero point.
G91Incremental PositioningAll moves are relative to the current position.

Essential M-Codes

M-codes control machine functions rather than movement:

CodeFunction
M03Spindle ON — Clockwise
M04Spindle ON — Counter-Clockwise
M05Spindle OFF
M06Tool Change
M08Coolant ON
M09Coolant OFF
M30End of Program (rewinds to start)

A Simple Example Program

Here's a complete G-code program that cuts a 1-inch square pocket to a depth of 0.1 inches:


G21         (Metric mode)
G90         (Absolute positioning)
G00 Z5.0    (Rapid to safe height)
M03 S12000  (Spindle on at 12,000 RPM)
G00 X0 Y0   (Rapid to start position)
G01 Z-2.54 F300  (Plunge to depth)
G01 X25.4 F600   (Cut along X)
G01 Y25.4        (Cut along Y)
G01 X0           (Return on X)
G01 Y0           (Return on Y)
G00 Z5.0    (Retract to safe height)
M05         (Spindle off)
M30         (End program)

Tips for Writing and Editing G-Code

  • Always start with a safe retract height — Never begin a program with the tool at cutting depth.
  • Use G90 (absolute) by default — Incremental mode (G91) is easy to lose track of and cause crashes.
  • Comment your code — Most controllers accept comments in parentheses ( ) or after a semicolon. Use them.
  • Verify with a simulator — Tools like NC Viewer or CAMotics let you visualize G-code before sending it to the machine.