All resources

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.

G01

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 →

CodeFunctionWhat it does
G00Rapid positioningMoves one or more axes at the machine's rapid rate. Normally used for non-cutting moves.
G01Linear interpolationMoves in a straight line at the programmed feed rate.
G02Clockwise arcCuts a clockwise circular or helical move in the active plane.
G03Counterclockwise arcCuts a counterclockwise circular or helical move in the active plane.
G04DwellPauses 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 →

CodeFunctionWhat it does
G17XY plane selectionSelects the XY plane for arcs, cutter compensation, and some canned cycles.
G18ZX plane selectionSelects the ZX plane.
G19YZ plane selectionSelects the YZ plane.
G20Inch unitsInterprets programmed dimensions in inches.
G21Metric unitsInterprets programmed dimensions in millimeters.
G28Return to reference positionReturns 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.
G53Machine coordinatesMakes the move in the machine coordinate system rather than the active work offset.
G54–G59Work coordinate offsetsSelects one of the standard work coordinate systems.
G90Absolute positioningPositions are measured from the active work-coordinate origin.
G91Incremental positioningEach position is measured from the current location.
G94Feed per minutePrograms feed as distance traveled per minute.
G95Feed per revolutionPrograms feed as distance traveled per spindle revolution.

Tool compensation

Swipe the table to see descriptions →

CodeFunctionWhat it does
G40Cancel cutter compensationCancels G41 or G42 cutter-radius compensation.
G41Cutter compensation leftOffsets the toolpath to the left of the programmed contour, viewed in the direction of travel.
G42Cutter compensation rightOffsets the toolpath to the right of the programmed contour, viewed in the direction of travel.
G43Tool-length compensationApplies the selected positive tool-length offset, commonly with an H address.
G49Cancel tool-length compensationCancels the active tool-length compensation.

Common canned cycles

Swipe the table to see descriptions →

CodeFunctionWhat it does
G80Cancel canned cycleCancels the active drilling, tapping, or boring cycle.
G81Drilling cycleFeeds to depth and rapids out without a dwell.
G82Drilling with dwellFeeds to depth, dwells, and then rapids out.
G83Peck drilling cycleDrills in repeated pecks to help clear chips from a deep hole.
G84Tapping cycleCoordinates spindle direction and axial feed for tapping.
G85Boring cycleFeeds into and out of the hole without stopping the spindle.

Common M-codes

Swipe the table to see descriptions →

CodeFunctionWhat it does
M00Program stopStops the program until the operator restarts it.
M01Optional stopStops only when Optional Stop is enabled on the control.
M03Spindle clockwiseStarts the spindle clockwise at the programmed S speed.
M04Spindle counterclockwiseStarts the spindle counterclockwise at the programmed S speed.
M05Spindle stopStops spindle rotation.
M06Tool changeCommands an automatic tool change on equipped machines.
M07Mist coolant onTurns on mist or auxiliary coolant when supported.
M08Flood coolant onTurns on flood coolant.
M09Coolant offTurns off active coolant outputs.
M19Spindle orientationOrients the spindle to a fixed angular position when supported.
M30Program end and resetEnds the program, resets it, and commonly rewinds to the beginning.
M98Call subprogramCalls a subprogram, often using P for the program number and L for repeat count.
M99Return from subprogramReturns 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 demo

Frequently 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.