- G words set motion and modes; M words run machine functions; letters such as F, S, T, H and D carry the values.
- Only one code from each modal group can be active at a time; a new one cancels the old.
- Controls differ: G28 and G0 behave differently on Haas and LinuxCNC, and Haas allows one M code per block.
- Start every program with a safe line that sets units, plane, distance mode and cancels compensation and cycles.
G-code list: the common G codes and what they do
The codes below are the core of milling and turning programs. Meanings are taken from the LinuxCNC documentation and the Haas programming workbook; where the two differ, the later sections say so.
| Code | What it does | Notes | Source |
|---|---|---|---|
| G0 | Rapid positioning move at the maximum rapid rate | Not for cutting; path can differ by control | LinuxCNC; Haas |
| G1 | Straight-line move at the current feed rate (F) | Coordinated on all axes | LinuxCNC |
| G2 / G3 | Clockwise / counterclockwise arc at the feed rate | Center with I, J, K or radius with R | LinuxCNC |
| G4 | Dwell: all axes stop for the P value | P is seconds in LinuxCNC | LinuxCNC |
| G17 / G18 / G19 | Select the XY, ZX or YZ plane for arcs and compensation | G17 is the default | LinuxCNC |
| G20 / G21 | Inch / millimeter units | State units at the top of every program | LinuxCNC |
| G28 | Return to home or a reference position | Behaves differently by control; see below | LinuxCNC; Haas |
| G30 | Like G28, using a second stored position | LinuxCNC parameters 5181 to 5189 | LinuxCNC |
| G40 / G41 / G42 | Cancel / start cutter compensation left / right | Needs a D word and a lead-in move | LinuxCNC |
| G43 / G49 | Apply / cancel tool length offset | G43 takes an H word | LinuxCNC |
| G54 to G59 | Select work coordinate systems 1 to 6 | G54 is coordinate system 1 | LinuxCNC |
| G80 | Cancel canned cycle | Any other motion code in group 1 also cancels it | LinuxCNC |
| G81 / G82 / G83 | Drill / drill with dwell / peck drill | Q sets the peck depth in G83 | LinuxCNC |
| G84 | Right-hand tapping cycle | Feed must match spindle speed and pitch | LinuxCNC |
| G90 / G91 | Absolute / incremental distance mode | Incremental moves count from the current position | LinuxCNC |
| G94 / G95 | Feed per minute / feed per revolution | G95 is common on lathes | LinuxCNC |
| G96 / G97 | Constant surface speed / constant spindle speed | Lathes; S is surface speed under G96 | LinuxCNC |
| G98 / G99 | Canned cycle retract to the starting Z / to the R plane | Set before a run of cycles | LinuxCNC |
Sources: LinuxCNC G-codes and Haas Mill Programming Workbook, PDF. The language descends from RS274; NIST notes that "the most recent standard version of RS274 is RS274-D, which was completed in 1979" (NIST, PDF), and the international standard is ISO 6983-1, which "specifies requirements and makes recommendations for a data format for positioning, line motion and contouring control systems" (ISO 6983-1:2009 preview, PDF).
M-code list: which machine functions do M codes switch?
M codes switch things on and off. These are the ones nearly every mill and lathe uses, with LinuxCNC's wording (LinuxCNC M-codes).
| Code | What it does | Source wording |
|---|---|---|
| M0 | Program stop | "pause a running program temporarily" |
| M1 | Optional stop | "pause a running program temporarily if the optional stop switch is on" |
| M2 | End of program | "end the program" |
| M3 | Spindle on, clockwise, at the S speed | "start the selected spindle clockwise at the S speed" |
| M4 | Spindle on, counterclockwise | "start the selected spindle counterclockwise at the S speed" |
| M5 | Spindle stop | "stop the selected spindle" |
| M6 | Tool change to the last T number | "stop the spindle and prompt the user to change the tool" |
| M7 | Mist coolant on | "turn mist coolant on" |
| M8 | Flood coolant on | "turn flood coolant on" |
| M9 | Coolant off | "turn both M7 and M8 off" |
| M30 | End of program and rewind (pallet exchange on machines that have one) | "exchange pallet shuttles and end the program" |
Machine builders add their own M codes for chip conveyors, probes, doors and pallet changers, so check the machine's manual before using any code not listed here.
What do the letters in a G-code program mean?
Every word in a block is a letter followed by a number. The letter says what the number is (LinuxCNC overview):
| Letter | Meaning |
|---|---|
| N | Line (block) number |
| O | Program number; Haas requires the line after the opening % to begin with O |
| G, M | Preparatory codes and machine functions |
| X, Y, Z (and A, B, C) | Axis positions or distances |
| I, J, K | Arc center offsets from the start point in X, Y and Z |
| R | Arc radius, or the retract plane of a canned cycle |
| F | Feed rate |
| S | Spindle speed |
| T | Tool selection |
| H | Tool length offset number, used with G43 |
| D | Tool radius compensation number, used with G41 and G42 |
| P | Dwell time in G4 and canned cycles |
| Q | Peck increment in G73 and G83 |
On the program number, the Haas workbook says the next line "must have a program number beginning with the letter O (not zero)" (Haas, PDF).
What does a G-code program look like? A commented drilling example
An invented example for a vertical mill with a Fanuc-style control: four holes peck-drilled in a plate, with the safe start line, tool change, work and length offsets, a canned cycle and a clean ending. Do not run it without checking it against your machine, tooling and part.
%
O1001 (BRACKET, DRILL 4 HOLES, EXAMPLE ONLY)
N10 G21 G17 G40 G49 G80 G90 (MM, XY PLANE, CANCEL COMP AND CYCLES, ABSOLUTE)
N20 T1 M6 (6.8 MM DRILL)
N30 G54 G0 X10. Y10. S1800 M3 (WORK OFFSET 1, RAPID TO FIRST HOLE, SPINDLE CW)
N40 G43 H1 Z25. M8 (TOOL LENGTH OFFSET 1, FLOOD COOLANT ON)
N50 G99 G83 X10. Y10. Z-18. R2. Q3. F150. (PECK DRILL TO 18 MM DEEP, 3 MM PECKS)
N60 X50.
N70 Y40.
N80 X10.
N90 G80 G0 Z25. M9 (CANCEL CYCLE, COOLANT OFF)
N100 M5 (SPINDLE STOP)
N110 G91 G28 Z0. (FANUC AND HAAS STYLE: Z TO MACHINE ZERO)
N120 G90
N130 M30 (END AND REWIND)
%N10 is the safe start line: units, plane, compensation, length offset, cycles and distance mode all set before any motion.
N30 and N40 bring in the work offset and the tool length offset before the tool approaches the part.
N50 starts the cycle; G99 returns to the R plane between holes, and N60 to N80 only give the new hole positions because the cycle is modal.
N90 cancels the cycle with G80 before any other move; N110 uses the Fanuc and Haas idiom for sending Z home, which LinuxCNC reads differently.
Write a decimal point on dimension words: on many Fanuc-style controls a number without one is read in the smallest input unit.
CAM programs like this one often start from a STEP model of the part; what a STEP file is covers how those models are exchanged.
What are modal groups, and why do programs fail on another machine?
Most G codes stay in force until something replaces them. NIST explains that "modal commands are arranged in sets called 'modal groups', and only one member of a modal group may be in force at any given time" (NIST, PDF). So G90 stays active until a G91, and a canned cycle stays active until G80 or another motion code: "G80 is part of modal group 1, so programming any other G-code from modal group 1 will also cancel the canned cycle" (LinuxCNC G-codes). LinuxCNC adds that "two G words from the same modal group may not appear on the same line" (LinuxCNC overview).
Modal state is also why a program that works on one machine can crash another. NIST's own report warns that "a program that runs on one machine probably will not run on one from a different maker", and the institute built its interpreter around "Fanuc flavored RS274/NGC code" (NIST, PDF). Post-processors exist to bridge exactly this gap.
Fanuc vs Haas vs LinuxCNC: which G codes behave differently?
Most of the list above is shared, but a few codes and rules differ enough to scrap a part. The ones documented in the sources:
| Topic | Haas (Fanuc-style) | LinuxCNC | Sources |
|---|---|---|---|
| G0 path | Axes "will not necessarily complete each axis move at the same time", so the path can dog-leg | "Coordinated motion to the destination point" | Haas workbook; LinuxCNC |
| G28 | "Used to return to the machine zero position on all axes" | Goes to a position stored with G28.1 (parameters 5161 to 5169) | Haas workbook; LinuxCNC |
| M codes per block | "Only one M code may be programmed per block" | "A line may have zero to four M words" | Haas workbook; LinuxCNC |
LinuxCNC's advice on G28: "Only use G28 when your machine is homed to a repeatable position and the desired G28 position has been stored with G28.1" (LinuxCNC G-codes). Sources: Haas, PDF and LinuxCNC overview. Fanuc's own manuals are not freely available, so check the control's programming manual for anything not listed here.
Which G-code mistakes crash machines or scrap parts?
Most crashes trace to the modal state, not to the move that hit something:
- No safe start line, so the program inherits units, plane or incremental mode from the last job.
- A canned cycle left active, so the next positioning move drills a hole where none was wanted.
- G43 without the right H number, or a tool change with the length offset of the previous tool.
- Cutter compensation started without a lead-in move longer than the tool radius.
- A program posted for one control run on another without re-posting.
- Rapid moves near clamps on a control whose G0 path is not a straight line.
- Several M codes in one block on a control that executes only one.
Can your company get paid by AI companies for its CNC programs?
Your company can get paid for these records. General-purpose AI models still write poor CNC code, and their makers know why. A 2025 study found that a general model "may lack comprehensive CNC machining datasets, especially for specialised processes or proprietary control syntax", and that one model kept "frequently skipping lines" when explaining code (APEM, 2025, PDF). The GLLM project had to train on "a G-code dataset extracted from the 'Stack' dataset of public GitHub repositories" (arXiv 2501.17584). A 2026 dataset holds "approximately 50k geometrically verified synthetic machining flows and 800 held-out real CNC records" (arXiv 2609.39738).
AI data buyers publish payouts of $100K to $5M for established companies' records, and most ask for 30 or more staff. Real programs that cut real parts, with their setup sheets, tool lists, the CAD and CAM files they came from and the inspection results, are exactly what those numbers say is missing. Our guide to what a machine shop's CNC and CAM files are worth to AI companies covers who owns programs written for a customer's part, and the contract manufacturer guide covers customer designs.
We help you license your data. Origin Data Partners is an independent data partner that helps business owners value and license their company records: the company pays Origin nothing, and a buyer may pay Origin when a deal completes. Describe the archive first: how many programs, which controls and CAM system, how many years, and which parts are your own products rather than customer work. Then see what your company's records could be worth. Not the owner? Advisers can introduce the company.
How we checked this
We read every source on this page on 21 November 2026: NIST's RS274NGC interpreter report, the LinuxCNC G-code, M-code and overview documentation, the Haas Mill Programming Workbook published by Haas, the ISO 6983-1 preview, and the research papers cited on language models and CNC code.
What we could not verify: Fanuc's programming manuals, which are not freely available, and the current Haas web pages, which refused our checker, so Haas behavior is quoted from its workbook. Coolant M codes and other machine-builder codes vary by machine. The example program is invented and is not a tested program.
Common questions
What is the difference between G-code and M-code?
G codes are preparatory commands: they set how the machine moves and in which mode, such as rapid or feed moves, arcs, units, planes, offsets and canned cycles. M codes are miscellaneous machine functions: spindle on and off, tool changes, coolant, program stops and the end of the program. A block can hold both, plus values such as feed and speed.
What do G90 and G91 mean in CNC programming?
G90 is absolute distance mode: every coordinate is measured from the active work zero. G91 is incremental mode: every coordinate is a distance from the current position. Both are modal, so the mode stays until the other is programmed, which is why a safe start line sets G90 and why a forgotten G91 is a classic cause of crashes.
What is G54 in CNC programming?
G54 selects work coordinate system 1, the first fixture or work offset, which tells the control where part zero is on the table. G55 to G59 select systems 2 to 6, so one program can machine several parts or fixtures. The offset values are set on the machine, usually by probing or touching off, not in the program.
What does G43 H1 do?
G43 applies a tool length offset, and H1 tells the control to use the length stored in offset register 1. It lets the program use part coordinates while the control adds each tool's length. Call it after the tool change and before the tool approaches the part, and match the H number to the tool in the spindle.
How does G83 peck drilling work?
G83 drills in steps: the drill feeds down by the Q increment, retracts to clear chips, then returns and feeds the next increment until it reaches the Z depth. R sets the plane the cycle starts and retracts to. It suits deep holes and stringy materials. The cycle stays active for each new X and Y position until G80 cancels it.
Are G codes the same on every CNC machine?
The core codes are shared, but controls differ in the details: how G0 moves, what G28 does, how many M codes a block may hold, and many machine-specific codes. NIST noted that a program for one machine probably will not run on another maker's. Post programs for the specific control, and check its manual for anything unusual.
Can a machine shop license its CNC programs to AI companies?
Yes, for programs the shop owns and parts it may disclose. Proven programs with their setup sheets, CAD and CAM files and inspection results are scarce training data for AI that writes or checks CNC code. Programs for customer parts usually depend on the customer's drawing rights, so sort the archive into own products and customer work first.
Sources and further reading
- NIST: The NIST RS274NGC Interpreter, Version 3 (NISTIR 6556, PDF)
- LinuxCNC documentation: G-code overview
- LinuxCNC documentation: G-codes
- LinuxCNC documentation: M-codes
- Haas Automation: Mill Programming Workbook (PDF)
- ISO 6983-1:2009, numerical control of machines, program format (preview, PDF)
- Advances in Production Engineering and Management (2025): language models and CNC code (PDF)
- arXiv 2501.17584: GLLM, self-corrective G-code generation with language models
- arXiv 2609.39738: CNCGEN, a dataset of machining flows
