CNC programming reference
Common G-codes and M-codes, explained.
A practical reference for the CNC commands machinists use most—from motion and work offsets to canned cycles, spindle control, and coolant.
Linear interpolation
G01 X2.000 Y1.000 F20.0
Controller behavior varies. This reference covers common Fanuc-style mill programming conventions, but codes and parameters can differ across Haas, Siemens, Heidenhain, Mazak, Okuma, LinuxCNC, and machine-builder implementations. Verify every program against your machine manual, simulate it, and use safe prove-out procedures.
G-codes define the path
They set motion, planes, units, offsets, compensation, and machining cycles.
M-codes control the machine
They handle spindle, coolant, tool changes, stops, and program flow.
Many G-codes are modal: once called, they remain active until another command in the same group replaces or cancels them. A safe program start explicitly sets the critical modes instead of assuming what the previous program left active.
Motion codes
Swipe the table to see descriptions →
| Code | Function | What it does |
|---|---|---|
G00 | Rapid positioning | Moves one or more axes at the machine's rapid rate. Normally used for non-cutting moves. |
G01 | Linear interpolation | Moves in a straight line at the programmed feed rate. |
G02 | Clockwise arc | Cuts a clockwise circular or helical move in the active plane. |
G03 | Counterclockwise arc | Cuts a counterclockwise circular or helical move in the active plane. |
G04 | Dwell | Pauses motion for a programmed amount of time.Note: The P address may represent milliseconds, seconds, or a decimal value depending on the control. |
Setup and coordinate codes
Swipe the table to see descriptions →
| Code | Function | What it does |
|---|---|---|
G17 | XY plane selection | Selects the XY plane for arcs, cutter compensation, and some canned cycles. |
G18 | ZX plane selection | Selects the ZX plane. |
G19 | YZ plane selection | Selects the YZ plane. |
G20 | Inch units | Interprets programmed dimensions in inches. |
G21 | Metric units | Interprets programmed dimensions in millimeters. |
G28 | Return to reference position | Returns an axis to machine reference through an intermediate point.Note: An unintended intermediate move can cause a collision; many shops prefer an explicit G53 move. |
G53 | Machine coordinates | Makes the move in the machine coordinate system rather than the active work offset. |
G54–G59 | Work coordinate offsets | Selects one of the standard work coordinate systems. |
G90 | Absolute positioning | Positions are measured from the active work-coordinate origin. |
G91 | Incremental positioning | Each position is measured from the current location. |
G94 | Feed per minute | Programs feed as distance traveled per minute. |
G95 | Feed per revolution | Programs feed as distance traveled per spindle revolution. |
Tool compensation
Swipe the table to see descriptions →
| Code | Function | What it does |
|---|---|---|
G40 | Cancel cutter compensation | Cancels G41 or G42 cutter-radius compensation. |
G41 | Cutter compensation left | Offsets the toolpath to the left of the programmed contour, viewed in the direction of travel. |
G42 | Cutter compensation right | Offsets the toolpath to the right of the programmed contour, viewed in the direction of travel. |
G43 | Tool-length compensation | Applies the selected positive tool-length offset, commonly with an H address. |
G49 | Cancel tool-length compensation | Cancels the active tool-length compensation. |
Common canned cycles
Swipe the table to see descriptions →
| Code | Function | What it does |
|---|---|---|
G80 | Cancel canned cycle | Cancels the active drilling, tapping, or boring cycle. |
G81 | Drilling cycle | Feeds to depth and rapids out without a dwell. |
G82 | Drilling with dwell | Feeds to depth, dwells, and then rapids out. |
G83 | Peck drilling cycle | Drills in repeated pecks to help clear chips from a deep hole. |
G84 | Tapping cycle | Coordinates spindle direction and axial feed for tapping. |
G85 | Boring cycle | Feeds into and out of the hole without stopping the spindle. |
Common M-codes
Swipe the table to see descriptions →
| Code | Function | What it does |
|---|---|---|
M00 | Program stop | Stops the program until the operator restarts it. |
M01 | Optional stop | Stops only when Optional Stop is enabled on the control. |
M03 | Spindle clockwise | Starts the spindle clockwise at the programmed S speed. |
M04 | Spindle counterclockwise | Starts the spindle counterclockwise at the programmed S speed. |
M05 | Spindle stop | Stops spindle rotation. |
M06 | Tool change | Commands an automatic tool change on equipped machines. |
M07 | Mist coolant on | Turns on mist or auxiliary coolant when supported. |
M08 | Flood coolant on | Turns on flood coolant. |
M09 | Coolant off | Turns off active coolant outputs. |
M19 | Spindle orientation | Orients the spindle to a fixed angular position when supported. |
M30 | Program end and reset | Ends the program, resets it, and commonly rewinds to the beginning. |
M98 | Call subprogram | Calls a subprogram, often using P for the program number and L for repeat count. |
M99 | Return from subprogram | Returns from a subprogram; behavior in a main program varies by control. |
A complete program example
This simple inch-mode milling example selects Tool 1, activates the G54 work offset, applies tool-length compensation, makes one feed move, and returns the Z axis in machine coordinates before ending.
%
O1001 (SIMPLE LINEAR CUT)
G20 G17 G40 G49 G80 G90
T01 M06
G54
S2500 M03
G00 X0.000 Y0.000
G43 H01 Z0.100
M08
G01 Z-0.125 F8.0
G01 X2.000 F20.0
G00 Z0.500
M09
M05
G53 G00 Z0.
M30
%This is an instructional example, not a ready-to-run production program. Tooling, workholding, offsets, clearances, feeds, speeds, and controller behavior must be validated for the actual machine.
What G-code cannot tell you
G-code describes what the controller was asked to do. It does not prove what happened at the machine. The program alone cannot show that a tool dulled halfway through a run, spindle load drifted upward, a cycle waited on an operator, or the machine sat idle between jobs.
The useful distinction
G-code controls the machine. Helio shows what the machine actually did.
Connect cycle time, spindle load, alarms, utilization, and operator context to see where production diverges from the plan.
Request a demoFrequently asked questions
Are G-codes the same on every CNC machine?
No. Core motion commands such as G00, G01, G02, and G03 are broadly shared, but syntax, canned cycles, parameters, and machine-specific M-codes vary by controller and machine builder. Always verify a program against the exact machine manual.
What is the difference between a G-code and an M-code?
G-codes generally define geometry, motion, units, coordinate systems, and machining cycles. M-codes generally control machine functions such as the spindle, coolant, tool changes, stops, and subprogram flow.
What does modal mean in G-code?
A modal command stays active until another command in the same modal group replaces or cancels it. For example, G01 remains active for subsequent coordinate blocks until another motion mode such as G00, G02, or G03 is programmed.
Controller documentation
Use this page as a quick reference, then confirm syntax in the official documentation for your control. Useful starting points include the Haas mill G-code list, Haas mill M-code list, and the LinuxCNC G-code documentation.