G-Code Reference for CNC Machines

G-code and M-code command reference for CNC milling, turning, and 3D printing machines. Includes motion, work offsets, spindle, coolant, and common program structure.

Reference Reference Updated Apr 24, 2026
Reference

G-code (ISO 6983 / RS-274) is the canonical language for CNC controllers. Commands are grouped into modal groups — once set, a G-command stays active until another command in the same group replaces it. Exact dialects vary between controllers (Fanuc, Haas, Mazak, LinuxCNC, GRBL, Marlin); this page covers the common core.

Motion (G-codes, modal group 1)

Code Name What it does
G00 Rapid move Traverse at max machine feed, no cutting. Non-linear path on most controllers.
G01 Linear feed Straight line at programmed feedrate (F). Used for cutting.
G02 CW arc Clockwise circular interpolation. Needs I/J/K (center offset) or R (radius).
G03 CCW arc Counter-clockwise circular interpolation. Same params as G02.
G04 Dwell Pause for P seconds or X seconds. Used after spindle commands.
G05.1 Look-ahead Fanuc AI contour / high-speed. Q1 on, Q0 off.
G33 Thread cutting Lathes — synchronised feed to spindle. K = thread pitch.
G73 Peck drill (high speed) Partial retract cycle. Fast chip clearing.
G76 Fine bore Boring cycle with oriented spindle stop and shift.
G80 Cancel canned cycle Ends drill/tap/bore cycles (G81–G89).
G81 Drill canned cycle Simple drill-to-Z-then-retract.
G82 Drill + dwell Counterbore: drill, dwell P, retract.
G83 Peck drill Retract-to-R-plane between pecks for deep holes.
G84 Tapping cycle Synchronised tap. Requires rigid-tap-capable spindle.
G85 Boring Feed in, feed out (no dwell).

Plane / units / mode (modal groups 2, 6, 3)

Code Group Meaning
G17 Plane XY plane (mill default). Arcs use I/J.
G18 Plane XZ plane (lathe default). Arcs use I/K.
G19 Plane YZ plane. Arcs use J/K.
G20 Units Inches.
G21 Units Millimetres.
G90 Distance Absolute positioning (X = absolute coord).
G91 Distance Incremental positioning (X = distance from current).
G93 Feed mode Inverse time — F = 1 / minutes per move.
G94 Feed mode Feed per minute (in/min or mm/min). Default on mills.
G95 Feed mode Feed per revolution. Standard on lathes.

Tool & cutter compensation (groups 7, 8)

Code What it does
G40 Cancel cutter-radius comp.
G41 Cutter comp left of programmed path (climb milling, RH tool).
G42 Cutter comp right of programmed path (conventional milling, RH tool).
G43 Tool-length comp positive (H word gives offset number).
G44 Tool-length comp negative (rare).
G49 Cancel tool-length comp.

Work coordinate systems (group 14)

Code Meaning
G53 Machine-zero coordinates (absolute, non-modal).
G54 Work offset 1 (default on power-up for most controllers).
G55 Work offset 2.
G56 Work offset 3.
G57 Work offset 4.
G58 Work offset 5.
G59 Work offset 6.
G54.1 P1..P48 Extended work offsets (Fanuc).

M-codes (miscellaneous / machine functions)

Code What it does
M00 Program stop (wait for operator).
M01 Optional stop (runs only if "optional stop" is on).
M02 End of program (no rewind).
M03 Spindle on, clockwise. Usually with S (rpm).
M04 Spindle on, counter-clockwise.
M05 Spindle stop.
M06 Tool change (typically preceded by T#).
M07 Mist coolant on.
M08 Flood coolant on.
M09 Coolant off.
M19 Spindle orient (for boring/indexing).
M30 End of program + rewind.
M41/M42 Low/high spindle gear (lathes).
M48/M49 Feed/speed override enable / disable.
M98/M99 Call sub-program / return.

Address letters (parameters)

Letter Typical use
X Y Z Linear axes.
A B C Rotary axes around X, Y, Z.
U V W Secondary linear axes (also incremental in some dialects).
I J K Arc centre offset from start point (IJ for G17, IK for G18, JK for G19).
R Arc radius, also retract plane in canned cycles.
F Feedrate (in/min or mm/min; per rev with G95).
S Spindle speed (rpm) or surface speed (with CSS mode).
T Tool number (plus M06 to execute change).
H / D Tool length offset number (H) / diameter offset number (D).
N Block/sequence number.
P Q Parameters — dwell time (P in G04), subprogram number, cycle peck.
L Loop / subprogram repeat count.

Minimal mill program — face a rectangle

%
O0001 (FACE 60X40)
G21 G17 G40 G49 G80 G94           ; mm, XY, clear comps
G90 G54                           ; absolute, WCS 1
T1 M06                            ; face mill, tool change
G43 H1                            ; tool length comp
S3000 M03                         ; spindle on CW 3000 rpm
M08                               ; flood on
G0 X-10 Y0                        ; rapid to start
Z5                                ; safe Z
G1 Z-0.5 F200                     ; plunge
X70 F600                          ; cut pass 1
Y20                               ; step over
X-10                              ; pass 2
Y40
X70                               ; pass 3
G0 Z50                            ; rapid up
M09                               ; coolant off
M05                               ; spindle off
G28 G91 Z0                        ; home Z
G90
M30                               ; end
%

Common dialect differences

  • Fanuc: most industrial CNC controllers (Mazak, Haas, Doosan, DMG Mori). Uses %…% program delimiters, O#### program numbers.
  • Haas: Fanuc-compatible plus Haas-specific G/M codes (G150 general pocket, M95/96, etc.).
  • LinuxCNC / Mach3 / Mach4: hobby/retrofit controllers, close to Fanuc but with quirks (support arc R only for < 180°, etc.).
  • GRBL: Arduino-based, subset of RS-274. No tool changer, no fixtures beyond G54, limited canned cycles.
  • 3D printers (Marlin, Klipper): use G0/G1 + extruder E axis; G28 homing, G29 bed mesh; M104/M109 hotend, M140/M190 bed, M106/M107 fan.
  • Always consult the machine builder's manual for exact behaviour — especially of G05.x look-ahead, canned cycles, and macro variables (#100+).

Last updated: