CNC G-Code Commands List: Essential G & M Codes Guide

A practical reference table of the most important CNC milling and routing G-codes and M-codes (GRBL, Fanuc, LinuxCNC), safe header templates, and browser simulation tips.

Essential CNC G-Code Commands Reference (GRBL, Fanuc & LinuxCNC)

G-code (RS-274) is the plain-text instruction language that tells CNC mills, routers, lathes, and laser cutters where to move, how fast to feed, and when to turn the spindle or coolant on. Every line (block) contains letter-address words such as G1 (linear feed move), X50.0 (target coordinate), F1000 (feed rate), and S12000 (spindle RPM).

You can paste any snippet below straight into our G-Code Viewer & Simulator to step through toolpath lines visually before running your machine.

Core Motion & Arc G-Codes

Code Meaning Example Practical Shop Note
G0 / G00 Rapid positioning (non-cutting) G0 X0 Y0 Z5 Moves at machine max speed—always retract Z to safe clearance first
G1 / G01 Linear interpolation (cutting feed) G1 X80 Y0 F1200 Moves straight at programmed feed rate F
G2 / G02 Clockwise circular arc G2 X20 Y20 I10 J0 I, J are incremental offsets from start point to arc center
G3 / G03 Counter-clockwise circular arc G3 X0 Y0 I-10 J0 Standard climb-milling direction for internal holes
G4 / G04 Dwell (pause) G4 P2.0 Pauses motion (seconds in GRBL/LinuxCNC; check Fanuc P ms vs X sec)

Plane, Units & Positioning Mode G-Codes

Code Meaning What It Sets
G17 / G18 / G19 Arc plane selection G17 = XY plane (standard 3-axis milling/routing), G18 = XZ, G19 = YZ
G20 Imperial units Coordinates in inches, feed F in inches/min (IPM)
G21 Metric units Coordinates in millimeters, feed F in mm/min
G90 Absolute positioning All X, Y, Z coordinates measured from active work zero (G54)
G91 Incremental (relative) positioning Moves relative to current tool position—cancel back to G90 promptly
G54 – G59 Work Coordinate Systems (WCS) Selects saved fixture/part zero offsets (G54 is default)
G28 / G53 Machine home / machine coordinates G53 G0 Z0 retracts spindle to top of Z travel safely

Essential Machine Control M-Codes

Code Meaning Typical Use
M3 / M03 Spindle ON clockwise (or laser ON) M3 S14000 starts spindle at 14,000 RPM
M4 / M04 Spindle ON counter-clockwise (or GRBL dynamic laser) Used for LH tools or GRBL laser power scaling
M5 / M05 Spindle / laser OFF Always include before tool changes or program end
M6 / M06 Tool change T1 M6 prompts controller/ATC for Tool #1
M8 / M9 Flood coolant ON / Coolant OFF M7 = mist coolant (if wired)
M0 / M1 Program pause / Optional stop Pauses for clamp moves or inspection until Cycle Start
M30 Program end & rewind Stops spindle/coolant and resets program to line 1

Anatomy of a Safe Metric CNC Header & Footer

Always start CNC programs with an explicit safety line so leftover modal states (like G91 incremental mode or G20 inches) never cause a crash:

; Safe Metric Milling Header (6mm 2F Carbide in Aluminum 6061)
G21 G90 G17 G54      ; Metric, Absolute, XY Plane, Work Offset 1
G0 Z10.0             ; Rapid to safe Z clearance
M3 S12000            ; Start spindle CW at 12,000 RPM
G0 X0.0 Y0.0         ; Rapid to start XY
G1 Z-1.5 F350        ; Controlled plunge feed
G1 X60.0 F1100       ; Cutting pass
G0 Z15.0             ; Retract clear of workholding
M5                   ; Stop spindle
M30                  ; End program

Verify Speeds, Feeds & Toolpaths Before Cutting

  1. Compute safe spindle S and feed F values for your cutter and material in our CNC Feeds and Speeds Calculator—it even generates a copy-ready header snippet.
  2. Drilling threaded fastener holes? Look up exact pre-drill diameters in the Tap Drill Chart and circular hole coordinates in our Bolt Circle Calculator.
  3. Load your .nc or .gcode file into our G-Code Viewer & Simulator to inspect rapid moves, Z depths, bounding extents, and estimated cycle time locally before pressing Cycle Start.

Frequently Asked Questions

What is the difference between G-codes and M-codes?

G-codes (Preparatory commands) control geometry and motion—such as G0 rapid move, G1 linear feed, G2/G3 arcs, G21 millimeters, and G90 absolute positioning. M-codes (Miscellaneous commands) control machine hardware—such as M3 spindle on, M5 spindle off, M6 tool change, M8 coolant, and M30 program end.

What is the difference between G0 and G1 in CNC G-code?

G0 (G00) moves the tool as fast as the machine allows between cuts and must never be used while inside material. G1 (G01) moves in a straight line at the programmed cutting feed rate (F) while removing material.

What do G90 and G91 mean in G-code?

G90 switches to Absolute coordinates (every X, Y, Z value is measured from your part origin G54). G91 switches to Incremental coordinates (each value is added to the tool's current position). Always include G90 in your program safety header.

Can I preview and simulate CNC G-code online without installing software?

Yes. Open our G-Code Viewer & Simulator (/gcode-viewer) and drag in any .nc, .gcode, or .tap file to inspect XY/Z toolpaths, step through lines, check bounding extents, and estimate runtime 100% locally in your browser.