Know a company with years of records? Refer it and earn up to $100K
CNC programming reference

G-Code List: Common G and M Codes With an Example Program

G-code list with the common G and M codes, what F, S, T, H and D mean, a commented drilling program, where Fanuc, Haas and LinuxCNC differ, and AI value.

Short answerChecked

G-code is the language CNC machines run, with G words that set motion and modes and M words that switch machine functions such as the spindle, coolant and tool changes. The codes used most are G0 rapid, G1 feed, G2 and G3 arcs, G17 to G19 planes, G20 and G21 units, G54 work offsets, G43 tool length, G81 to G84 drilling and tapping cycles, G90 and G91 distance modes, and M3, M5, M6, M8 and M30. Each control has its own dialect.

Digital calipers on a quality inspection report on a factory workbench
Key points
  • 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.

CodeWhat it doesNotesSource
G0Rapid positioning move at the maximum rapid rateNot for cutting; path can differ by controlLinuxCNC; Haas
G1Straight-line move at the current feed rate (F)Coordinated on all axesLinuxCNC
G2 / G3Clockwise / counterclockwise arc at the feed rateCenter with I, J, K or radius with RLinuxCNC
G4Dwell: all axes stop for the P valueP is seconds in LinuxCNCLinuxCNC
G17 / G18 / G19Select the XY, ZX or YZ plane for arcs and compensationG17 is the defaultLinuxCNC
G20 / G21Inch / millimeter unitsState units at the top of every programLinuxCNC
G28Return to home or a reference positionBehaves differently by control; see belowLinuxCNC; Haas
G30Like G28, using a second stored positionLinuxCNC parameters 5181 to 5189LinuxCNC
G40 / G41 / G42Cancel / start cutter compensation left / rightNeeds a D word and a lead-in moveLinuxCNC
G43 / G49Apply / cancel tool length offsetG43 takes an H wordLinuxCNC
G54 to G59Select work coordinate systems 1 to 6G54 is coordinate system 1LinuxCNC
G80Cancel canned cycleAny other motion code in group 1 also cancels itLinuxCNC
G81 / G82 / G83Drill / drill with dwell / peck drillQ sets the peck depth in G83LinuxCNC
G84Right-hand tapping cycleFeed must match spindle speed and pitchLinuxCNC
G90 / G91Absolute / incremental distance modeIncremental moves count from the current positionLinuxCNC
G94 / G95Feed per minute / feed per revolutionG95 is common on lathesLinuxCNC
G96 / G97Constant surface speed / constant spindle speedLathes; S is surface speed under G96LinuxCNC
G98 / G99Canned cycle retract to the starting Z / to the R planeSet before a run of cyclesLinuxCNC

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

CodeWhat it doesSource wording
M0Program stop"pause a running program temporarily"
M1Optional stop"pause a running program temporarily if the optional stop switch is on"
M2End of program"end the program"
M3Spindle on, clockwise, at the S speed"start the selected spindle clockwise at the S speed"
M4Spindle on, counterclockwise"start the selected spindle counterclockwise at the S speed"
M5Spindle stop"stop the selected spindle"
M6Tool change to the last T number"stop the spindle and prompt the user to change the tool"
M7Mist coolant on"turn mist coolant on"
M8Flood coolant on"turn flood coolant on"
M9Coolant off"turn both M7 and M8 off"
M30End 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):

LetterMeaning
NLine (block) number
OProgram number; Haas requires the line after the opening % to begin with O
G, MPreparatory codes and machine functions
X, Y, Z (and A, B, C)Axis positions or distances
I, J, KArc center offsets from the start point in X, Y and Z
RArc radius, or the retract plane of a canned cycle
FFeed rate
SSpindle speed
TTool selection
HTool length offset number, used with G43
DTool radius compensation number, used with G41 and G42
PDwell time in G4 and canned cycles
QPeck 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.

Peck drilling four holes with G83. Comments are in parentheses; the % lines mark the start and end of the file.
%
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:

TopicHaas (Fanuc-style)LinuxCNCSources
G0 pathAxes "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

What is your company’s data worth?

AI companies pay $100K to $5M for company records. Six questions, no files.